日期:2014-05-18  浏览次数:20533 次

急.问一个触发器的问题。在线等.马上结贴
我想把三个写在一起,方便管理.仔细看了一下好象不行.这样判断好象判断不出他是插入还是修改还是删除状态的.但不知道怎么改正。请帮忙
create   trigger   XXX   on     XXX   for   insert,update,delete
as  
IF   EXISTS   (   SELECT   *   FROM   INSERTED)   AND   NOT   EXISTS   (SELECT   *   FROM   DELETED)       --insert   时触发
IF   UPDATE(字段)     --   update时触发
IF   NOT   EXISTS   (   SELECT   *FROM   INSERTED)   AND   EXISTS   (SELECT   *   FROM   DELETED)
--DELETE   时触发

------解决方案--------------------
if EXISTS ( SELECT *FROM INSERTED) AND EXISTS (SELECT * FROM DELETED) --修改时触发

------解决方案--------------------
create trigger TR_XXX
on XXX
for insert,update,delete
as

IF EXISTS ( SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED) --insert 时触发

IF EXISTS ( SELECT *FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
-- update时触发

IF NOT EXISTS ( SELECT *FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
--DELETE 时触发

------解决方案--------------------
楼主把表的结构贴出来,和要实现的效果