Click here to Skip to main content
11,648,960 members (82,665 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
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
Edited 15-Dec-12 1:28am
v2
Comments
OriginalGriff at 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
good
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
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.
  Permalink  

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

  Print Answers RSS
0 F-ES Sitecore 419
1 CPallini 410
2 Kornfeld Eliyahu Peter 330
3 OriginalGriff 324
4 Sergey Alexandrovich Kryukov 254
0 OriginalGriff 1,512
1 Sergey Alexandrovich Kryukov 1,022
2 jyo.net 994
3 DamithSL 986
4 CPallini 945


Advertise | Privacy | Mobile
Web02 | 2.8.150804.4 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2015
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