Click here to Skip to main content
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 Maciej Los 469
1 OriginalGriff 379
2 CPallini 205
3 Sergey Alexandrovich Kryukov 170
4 Sharmanuj 109
0 OriginalGriff 5,649
1 Sergey Alexandrovich Kryukov 4,783
2 Peter Leow 2,954
3 Maciej Los 2,689
4 DamithSL 2,465


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