星期四, 11月 15, 2012

Trigger insert, update, and delete

 

CREATE TRIGGER YourTrigger ON YourTable
AFTER INSERT,UPDATE,DELETE
AS
  IF EXISTS (SELECT * FROM INSERTED)
  BEGIN
    IF EXISTS (SELECT * FROM DELETED)
    BEGIN
      --UPDATE
    END
    ELSE
    BEGIN
       --INSERT
    END
    --handle insert or update data
  END
  ELSE IF EXISTS(SELECT * FROM DELETED)
  BEGIN
    --DELETE
    --handle delete data, insert into both the history and the log tables 
 
  END


參考資料  http://stackoverflow.com/questions/4391393/how-to-copy-an-inserted-updated-deleted-row-in-a-sql-server-triggers