有二个表,tableA 和 tableB,字段如下。
tableA
TAID TANAME TAPCS
001 A001 100
002 200
003 B001 150
004 A002 120
005 80
006 90
007 C001 300
008 C001 200
……
……
当在tableB中插入或修改时,当TANAME=TBNAME时,执行触发器将tableA 中的 TANAME 列相同的TAPCS进行SUM并 回写到tableB中的TBPCS,
当tableB中的TBNAME不存在于tableA 中的TANAME 列时,不执行触发器。
完成后的效果如下:
tableB
TBID TBNAME TBPCS
B001 A001 100
B002 R003
B003 C001 500
B004 G002
B005 A002 120
……
……以上触发器能怎么写,请教各位高手。
tableA
TAID TANAME TAPCS
001 A001 100
002 200
003 B001 150
004 A002 120
005 80
006 90
007 C001 300
008 C001 200
……
……
当在tableB中插入或修改时,当TANAME=TBNAME时,执行触发器将tableA 中的 TANAME 列相同的TAPCS进行SUM并 回写到tableB中的TBPCS,
当tableB中的TBNAME不存在于tableA 中的TANAME 列时,不执行触发器。
完成后的效果如下:
tableB
TBID TBNAME TBPCS
B001 A001 100
B002 R003
B003 C001 500
B004 G002
B005 A002 120
……
……以上触发器能怎么写,请教各位高手。
on tb2
for insert ,update
as
begin
update tb2
set TBPCS =(select sum(TAPCS ) from tb1 where TANAME=inserted.tname)
end
after insert ,update
as
begin
update tableb
set TBPCS=(select sum(TAPCS) from tablea where i.taname=tbname )
from inserted i end
after insert ,update
as
begin
update
tableb
set
TBPCS=(select sum(TAPCS) from tablea where inserted.taname=tbname )
end
from 后面少 Inserted 表
--try
create trigger tri_g on tablea
after of insert,update
as
begin
update a set a.tbpcs=b.tapcs from tablea a ,
(select tanmae,sum(tapcs) tapcs from inserted group by tanmae) b
where a.tanmae=b.tanmae
end