有两个表,一个订单主表,一个订单明细表
主表A 订单号(orderNo) 数量(Amount) 金额 TotalPrice
明细表B 订单号(orderNo) 数量(Amt) 单价(UnitPrice)
A表中的订单号对应B表中的订单号
当向明细表插入数量时,自动更新主表中的数量和金额!
请各位大虾帮忙!

解决方案 »

  1.   

    create trigger a on 订单明细表 after insert as
    update 订单主表 set Amount=Amount+Amt,TotalPrice=TotalPrice+Amt*UnitPrice
    from inserted where 订单明细表.orderNo=订单主表.orderNo
      

  2.   

    --sorry--
    create trigger a on 订单明细表 after insert as
    update 订单主表 set Amount=Amount+Amt,TotalPrice=TotalPrice+Amt*UnitPrice
    from inserted where inserted.orderNo=订单主表.orderNo
      

  3.   

    测试过的,也贴出来吧
    create table 订单明细表(orderNo int,Amt int,UnitPrice money)
    create table 订单主表(orderNo int,Amount int,TotalPrice money)
    insert 订单主表 select 1,5,20
    insert 订单主表 select 2,1000,10000
    insert 订单明细表 select 1,10,100
    select * from 订单明细表
    select * from 订单主表
    orderNo     Amt         UnitPrice             
    ----------- ----------- --------------------- 
    1           10          100.0000(所影响的行数为 1 行)orderNo     Amount      TotalPrice            
    ----------- ----------- --------------------- 
    1           15          1020.0000
    2           1000        10000.0000(所影响的行数为 2 行)