SqlServer触发器实现表的级联插入、级联更新
首先建立两张表,分别为test1与test2,期望在更改test1的时候,test2的相关记录能够同时做出更改。假定test1与test2的表结构相同,如下表所示
name | age |
触发器实现级联插入:
create trigger test3 on test1 after insert as insert into test2 select name,age from inserted
触发器实现级联更新:
create trigger test4 on test1 after update as begin update test2 set age = a.age from test2,inserted as a where a.name = test2.name end
执行:
insert into test1 values ('merle','99');
输出:
select * from test1;【merle 99】 select * from test2;【merle 99】