Advertisement
kromm77

TRIGGER SQL SERVER

May 15th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.55 KB | None | 0 0
  1. -- ================================================
  2. -- Template generated from Template Explorer using:
  3. -- Create Trigger (New Menu).SQL
  4. --
  5. -- Use the Specify Values for Template Parameters
  6. -- command (Ctrl-Shift-M) to fill in the parameter
  7. -- values below.
  8. --
  9. -- See additional Create Trigger templates for more
  10. -- examples of different Trigger statements.
  11. --
  12. -- This block of comments will not be included in
  13. -- the definition of the function.
  14. -- ================================================
  15. SET ANSI_NULLS ON
  16. GO
  17. SET QUOTED_IDENTIFIER ON
  18. GO
  19.  
  20. DROP  TRIGGER dbo.TestBase_change
  21. GO
  22.  
  23. -- =============================================
  24. -- Author:      RDEM
  25. -- Create date:
  26. -- Description:
  27. -- =============================================
  28. CREATE TRIGGER dbo.TestBase_change
  29.    ON  dbo.TestBase
  30.    AFTER INSERT,DELETE,UPDATE
  31. AS
  32. BEGIN
  33.     -- SET NOCOUNT ON added to prevent extra result sets from
  34.     -- interfering with SELECT statements.
  35.     SET NOCOUNT ON;
  36.    
  37.     -- Insert statements for trigger here
  38.     INSERT INTO dbo.TestBaseVersioningExp(row_type,Id,NewDescription,OldDescription,CreatedAt)
  39.     SELECT 'inserted',a.Id,a.Description,'',a.CreatedAt
  40.     FROM inserted a
  41.     WHERE NOT EXISTS (SELECT * FROM deleted WHERE id =a.Id )
  42.     UNION ALL
  43.     SELECT 'deleted',a.Id,'' ,a.Description,a.CreatedAt
  44.     FROM deleted a
  45.     WHERE NOT EXISTS (SELECT * FROM inserted WHERE id =a.Id )
  46.     UNION ALL
  47.     SELECT 'updated',a.Id,a.Description,b.Description,a.CreatedAt
  48.     FROM inserted  a JOIN deleted b ON a.Id=b.Id
  49.     WHERE EXISTS (SELECT * FROM deleted WHERE id =a.Id )
  50. END
  51. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement