create table testa (id int identity primary key,sname varchar(8),stime varchar(8)) go create table testb (id int identity primary key,sname varchar(8),click int) go create table testc (id int identity primary key,sname varchar(8),stime varchar(8)) go 按照要求创建触发器:create trigger tt on testa for insert as insert testc select sname,stime from inserted if exists (select b.sname from testb b join inserted s on b.sname=s.sname ) begin update testb set click=click+1 from testb b join inserted s on b.sname=s.sname end else begin insert testb select (select sname from inserted),1 end --------
create trigger tt on testa for insert as insert testc (sname,stime) select sname,stime from inserted --插入testc--处理testb if exists (select * from testb b, inserted i where b.sname=i.sname ) update b set b.click=b.click+1 from testb b,inserted i where b.sname=i.sname else insert testb(sname,stime ) select sname,1 from inserted
Create trigger trdtest On testa instead of insert As update testb set click=click+1 from inserted a,testb b where a.sname=b.sname insert into testb(sname,click) select sname,1 from inserted A WHERE NOT EXISTS(SELECT 1 FROM TESTB WHERE SNAME=A.SNAME) insert into testc(sname,stime) select sname,stime from insertedgo
go
create table testb (id int identity primary key,sname varchar(8),click int)
go
create table testc (id int identity primary key,sname varchar(8),stime varchar(8))
go
按照要求创建触发器:create trigger tt
on testa
for insert
as
insert testc select sname,stime from inserted
if exists (select b.sname from testb b join inserted s on b.sname=s.sname )
begin
update testb set click=click+1 from testb b join inserted s on b.sname=s.sname
end
else
begin
insert testb select (select sname from inserted),1
end --------
on testa
for insert
as insert testc (sname,stime) select sname,stime from inserted --插入testc--处理testb
if exists (select * from testb b, inserted i where b.sname=i.sname )
update b set b.click=b.click+1 from testb b,inserted i where b.sname=i.sname
else
insert testb(sname,stime ) select sname,1 from inserted
On testa
instead of insert
As
update testb set click=click+1 from inserted a,testb b where a.sname=b.sname insert into testb(sname,click)
select sname,1 from inserted A
WHERE NOT EXISTS(SELECT 1 FROM TESTB WHERE SNAME=A.SNAME)
insert into testc(sname,stime)
select sname,stime from insertedgo