Click here to Skip to main content
13,041,887 members (61,904 online)
Rate this:
Please Sign up or sign in to 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 15-Dec-12 1:24am
Updated 15-Dec-12 1:28am
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

CREATE TRIGGER [dbo].[TR_Insert_Simple] ON [dbo].[Simple] AFTER INSERT
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

Hope this helps you.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100