Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want create trigger for two tables
My tables are simple and simplehistory
Simple history is duplicate table of simple table
My requirement is
When i insert the data into simple table then simple table is update with new record
And insert old records in simplehistory table

can anyone help me please i am stuck with that

[edit]Spurious code block removed, SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 15-Dec-12 1:28am
v2
Comments
OriginalGriff 15-Dec-12 7:28am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

1 solution

You need to create a simple trigger with AFTER INSERT, which means the trigger will run after insertion is done.

You will get the inserted values of your table from temporary INSERTED table in your trigger.

Below is the sample code.

SQL
CREATE TRIGGER [dbo].[TR_Insert_Simple] ON [dbo].[Simple] AFTER INSERT
AS
BEGIN

INSERT INTO SimpleHistory (column names) SELECT * FROM INSERTED

DELETE FROM [Simple] Where PK_ID = (SELECT PK_ID FROM [INSERTED]) --PK_ID is your primary key

END


Hope this helps you.
 
Share this answer
 

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