给你个例子 --------------------表A :ID name SEX表B :ID name sex 要求对一个表A进行了增删改后,表B就能增删改,要求就写一个触发器,不能是多个。----- 作者:wufeng4552 -------------------------------------- set nocount on if object_id('ta')is not null drop table ta go create table ta(ID int ,[Name] varchar(10)) if object_id('tb')is not null drop table tb go create table tb(ID int ,[Name] varchar(10),Sex varchar(2)) go if object_id('tri_Modi')is not null drop trigger tri_Modi go----------------------------------------------- create trigger tri_Modi on ta for delete ,insert ,update as if not exists(select 1 from deleted) insert tb(ID,[name])select * from inserted --insert else if exists(select 1 from inserted)and exists(select 1 from deleted) update tb set tb.[name]=inserted.[name] from inserted where tb.id=inserted.id --update else delete b from tb b ,deleted d where b.id=d.id --delete go insert ta select 1,'a' insert ta select 2,'b' select * from tb /* ID Name Sex ----------- ---------- ---- 1 a NULL 2 b NULL */ update ta set [name]='c' where id=2 select * from tb /* ID Name Sex ----------- ---------- ---- 1 a NULL 2 c NULL */ delete ta where id=1 select * from tb /* ID Name Sex ----------- ---------- ---- 2 c NULL */本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/js_szy/archive/2009/07/28/4386332.aspx
create trigger tri_taskname1 on taskname1 for insert,update,delete as if not exists(select 1 from inserted) --delete begin delete taskname2 from deleted where taskname2.id=deleted.id end if not exists(select 1 from deleted) --insert begin insert taskname2 select * from inserted end else --update begin update t2 set t2.name=i.name from taskname2 t2,inserted i where t2.id=i.id end
给你个例子
--------------------表A :ID name SEX表B :ID name sex 要求对一个表A进行了增删改后,表B就能增删改,要求就写一个触发器,不能是多个。----- 作者:wufeng4552
--------------------------------------
set nocount on
if object_id('ta')is not null drop table ta
go
create table ta(ID int ,[Name] varchar(10))
if object_id('tb')is not null drop table tb
go
create table tb(ID int ,[Name] varchar(10),Sex varchar(2))
go
if object_id('tri_Modi')is not null drop trigger tri_Modi
go-----------------------------------------------
create trigger tri_Modi on ta
for delete ,insert ,update
as
if not exists(select 1 from deleted)
insert tb(ID,[name])select * from inserted --insert
else if exists(select 1 from inserted)and exists(select 1 from deleted)
update tb set tb.[name]=inserted.[name] from inserted where tb.id=inserted.id --update
else
delete b from tb b ,deleted d where b.id=d.id --delete
go
insert ta select 1,'a'
insert ta select 2,'b'
select * from tb
/*
ID Name Sex
----------- ---------- ----
1 a NULL
2 b NULL
*/
update ta set [name]='c' where id=2
select * from tb
/* ID Name Sex ----------- ---------- ---- 1 a NULL 2 c NULL */
delete ta where id=1
select * from tb
/*
ID Name Sex
----------- ---------- ----
2 c NULL
*/本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/js_szy/archive/2009/07/28/4386332.aspx
on taskname1
for insert,update,delete
as
if not exists(select 1 from inserted) --delete
begin
delete taskname2 from deleted where taskname2.id=deleted.id
end
if not exists(select 1 from deleted) --insert
begin
insert taskname2 select * from inserted
end
else --update
begin
update t2
set t2.name=i.name
from taskname2 t2,inserted i
where t2.id=i.id
end