日期:2014-05-17 浏览次数:20510 次
create table test1(ffid int,ffno varchar(20), note varchar(30))
create table Test2(wwid int,wwno varchar(20), bak varchar(30))
create trigger dbo.trigger_test1_insert
on test1
for insert
as
insert into Test2(wwid,wwno)
select ffid,ffno
from inserted
go
create trigger dbo.trigger_test1_delete
on test1
for delete
as
delete Test2
from deleted d
where d.ffid = Test2.wwid
go
--1.插入
insert into test1
values(1,'aaa','xxx')
--数据也插入到了test2
select *
from Test2
/*
wwid wwno bak
1 aaa NULL
*/
--2.删除
delete from test1 where ffid = 1
--发现已经被删掉了
select *
from Test2
/*
wwid wwno bak
*/
use db1
go
if exists(select 1 from sys.triggers where name ='tr_test')
drop trigger tr_test
go
create trigger tr_test
on tb1
for insert,delete
as
if exists(select 1 from inserted) and not exists(select 1 from deleted)--插入操作
begin
insert db2..tb2
select .... from db1..tb1
end
if exists(select 1 from deleted) and not exists(select 1 from inserted)--如果删除操作
begin
delete from db2..tb2 where exists(select 1 from db1..tb1 where ....)
end