日期:2014-05-18 浏览次数:20540 次
create tr_ins_tab1 on tab1 after insert as begin update tab2 set times = times +1 from tab2 a inner join inserted b on a.name1 = b.name1 and a.name2= b.name2 insert into tab2 select a.name1,a.name2,1 from inserted a where not exists (select 1 from tab2 b a.name1 = b.name1 and a.name2= b.name2 ) end
------解决方案--------------------
CREATE TRIGGER trg_test ON tb1 FOR INSERT AS DECLARE @name1 VARCHAR(10),@name2 varchar(10) SELECT @name1=name1,@name2=name2 FROM INSERTED IF EXISTS(SELECT 1 FROM tb2 WHERE name1=@name1 AND name2=@name2) BEGIN UPDATE tb2 SET times=times+1 WHERE name1=@name1 AND name2=@name2 END ELSE BEGIN INSERT INTO tb2(name1,name2,times) VALUES(@name1,@name2,1) END
------解决方案--------------------
go if object_id('tri_test')is not null drop trigger tri_test go create trigger tri_test on 表1 for insert as if exists(select 1 from inserted i where not exists(select 1 from t2 t where t.name1=i.name and t.name2=i.name2)) begin update t2 set times=times+1 where name1 in(select name1 from inserted) and name2 in(select name2 from inserted) end