|
Hi, Is there any way I can copy the deleted records into a New table, FOR EXAMPLE delete from Table where column '1' IS POSSIBLE THAT I CAN DO SOMETHING LIKE THIS delete from Table into Temptable where column '1' I tired this in SQL but it did not work. It shows me this error: Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'into'.
(comments are locked)
|
|
Try this (assumes source and target tables have the same structure):
If the table structures are different you need to list the column names individually. That's funny. Got me by 29 seconds.
Feb 16 '10 at 03:53 PM
Grant Fritchey ♦♦
(comments are locked)
|
|
You can capture the result after deletiong using below query. create table #del (id int not null) insert into #del select 1 insert into #del select 2 insert into #del select 3 insert into #del select 4 DECLARE @MyTab table ( ID int NOT NULL); DELETE #del OUTPUT DELETED.id INTO @MyTab FROM #del d WHERE d.id BETWEEN 2 and 3; SELECT Id FROM @MyTab GO
(comments are locked)
|
|
What about using the OUTPUT statement like this: +1 Nice idea Grant!
Feb 16 '10 at 04:02 PM
Ian Roke
Thanks, but dportas had it first, by 29 seconds.
Feb 16 '10 at 04:30 PM
Grant Fritchey ♦♦
(comments are locked)
|
|
+1 to FatherJack, but just wanted to show the trigger syntax, which won't fit in a comment:
(comments are locked)
|
|
You can either do it in two stages within a transaction:
OR you can use a ON DELETE trigger that INSERTS the data into the new location as a reaction to the delete command that you execute. The best option will depend on other factors - table size, index complexity, frequency of occurrence, etc [Edit] The first option assumes this is a once only job and the new table doesnt exist. If its an ongoing requirement then you need something like Be careful! Unless you are using SERIALIZABLE isolation you may delete something which you have not copied. DELETE with OUTPUT is a better method for that reason.
Feb 16 '10 at 05:51 PM
David 1
As I understand it, within this transaction changes could be made to cause 'phantom' records but other transactions would be waiting. Therefore, unless some step is introduced in this script to alter data between the INSERT and the DELETE then this should work. Unless I have misunderstood the READ COMMITTED section here : http://msdn.microsoft.com/en-us/library/ms173763.aspx ???
Feb 16 '10 at 06:18 PM
Fatherjack ♦♦
But if new rows are inserted on another connection between the INSERT and DELETE then they will get deleted but not copied to the new table. To avoid that use SERIALIZABLE isolation or a TABLOCK and HOLDLOCK hint.
Feb 17 '10 at 10:43 AM
David 1
(comments are locked)
|

