Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Insert a record that has been deleted

Question posted by: raghulvarma (Member) on June 18th, 2008 04:19 PM
I need to insert the records in table2 that have been deleted in table1 how should I proceed? Will trigger help me out?
If so some one plz help how to do that
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Delerna's Avatar
Delerna
Expert
541 Posts
June 19th, 2008
05:14 AM
#2

Re: Insert a record that has been deleted
A trigger would be one way

Another way would be to put the delete into a stored proc
Prior to deleting the records run a query that selects the records that are about to be deleted and inserts them into the other table.

Reply
ck9663's Avatar
ck9663
Expert
1,251 Posts
June 19th, 2008
09:53 PM
#3

Re: Insert a record that has been deleted
These two options are good.

Just make sure to use transactions to make sure that when one of the t-sql fails, do a rollback.

-- CK

Reply
dbpros's Avatar
dbpros
Newbie
15 Posts
June 23rd, 2008
11:31 PM
#4

Re: Insert a record that has been deleted
Yes... if you use a stored procedure with multiple delete statements, use begin trans and commit trans to ensure the entire batch processes together.

DELETED URL

Last edited by RedSon : June 24th, 2008 at 06:06 PM. Reason: No free advertising
Reply
RedSon's Avatar
RedSon
Site Moderator
2,840 Posts
June 24th, 2008
06:08 PM
#5

Re: Insert a record that has been deleted
Quote:
Originally Posted by dbpros
Yes... if you use a stored procedure with multiple delete statements, use begin trans and commit trans to ensure the entire batch processes together.

DELETED URL


dbpros, I know you want to be helpful but putting a base link to a website is not allowed, it is considered advertising spam. It is okay to link to a technical article as long as it won't be considered an advertisement. If you want to link to your own website you can PM the person and suggest to them to visit.

Reply
b0010100's Avatar
b0010100
Newbie
13 Posts
June 30th, 2008
12:21 AM
#6

Re: Insert a record that has been deleted
You're right, a trigger would be the best way to deal with this.

you will need to write an INSTEAD OF trigger to replace the default DELETE action. Inside the trigger, you will have access to the "deleted" object which will contain the deleted row(s) information. From there you can INSERT them into your new table.
Code: ( text )
  1. CREATE TRIGGER [dbo].[trg_MyDeleteTrigger]
  2.    ON  [dbo].[tbl_myTable]
  3.    AFTER DELETE
  4. AS
  5. BEGIN
  6.  
  7.      INSERT INTO myNewTable
  8.      (Field1,
  9.       Field2,
  10.       Field3)
  11.       VALUES
  12.        (inserted.ID,
  13.         inserted.Title,
  14.         inserted.Code);
  15.  
  16. END

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,335 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Microsoft SQL Server Contributors