Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
CREATE TRIGGER [dbo].[triggername] ON [dbo].[tablename] AFTER INSERT
AS

BEGIN

INSERT INTO  tablename_history
SELECT * FROM INSERTED


PRINT 'AFTER INSERT Trigger fired.'
END





SQL
CREATE TRIGGER [dbo].[triggername] ON [dbo].[tablename] FOR UPDATE
AS

BEGIN

INSERT INTO tablename_history

SELECT * FROM DELETED

PRINT 'FOR UPDATE Trigger fired.'
END
Posted
Updated 18-Mar-14 22:40pm
v2
Comments
Andrius Leonavicius 19-Mar-14 5:18am    
Hi,

You have already asked a similar question. I have already answered to your question, haven't I?
http://www.codeproject.com/Answers/745934/I-have-created-trigger-for-after-insert-now-how-to#answer2

Note: Ask in the comments if something is not clear about the given answer. If you want to edit or improve your question, you can do that by clicking on "Improve Question" link.
Andrius Leonavicius 19-Mar-14 5:35am    
By the way, I just noticed you're using AFTER INSERT, but FOR UPDATE trigger. AFTER and FOR triggers do the same thing, there is no difference. An INSTEAD OF trigger is different than FOR/AFTER.
So use AFTER INSERT, UPDATE or FOR INSERT, UPDATE...

1 solution

You can add INSERT and UPDATE in the same create statement separated by a comma. Something like below

For more info on Create TRIGGER refer : http://msdn.microsoft.com/en-us/library/ms189799.aspx


SQL
CREATE TRIGGER [dbo].[triggername] ON [dbo].[tablename] AFTER INSERT, UPDATE
AS

BEGIN

IF EXISTS(SELECT * FROM DELETED)
  BEGIN
    SELECT * FROM DELETED;
  END

ELSE 

  BEGIN
    INSERT INTO  tablename_history;
    SELECT * FROM INSERTED;
  END 

 

PRINT 'AFTER INSERT/UPDATE Trigger fired.';
END
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900