求“MSSQL建一个触发器可以实现两个表的添加,删除,修改同步”的例子
如有表:A,B
需要建一触发器,可以在A添加,删除,修改一个记录时在B表中实现同步,有原理我也知道,有没有一个例子看一下!
------解决方案--------------------create table A(id int, name varchar(10))
go
create table B(id int, name varchar(10))
go
create trigger tr_insert on A
for insert
as
insert B (id, name)
select id, name from inserted
go
create trigger tr_delete on A
for delete
as
delete B
from deleted A
where A.id=B.id and A.name=B.name
go
create trigger tr_update on A
for update
as
update B set id=inserted.id, name=inserted.name
from deleted, inserted
where B.id=deleted.id and B.name=deleted.name
go
insert A select 1, 'AA '
union all select 2, 'BB '
union all select 3, 'CC '
delete A where id= '2 '
update A set name= 'WW '
where id= '3 '
select * from A
select * from B