表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 --del
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
*/