Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 INSERT FROM TABLE

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-19 : 08:13:27
hi team, i need to write a proc that delete a row from table
and insert same row into another table without affecting current rows of that table.



challenge everything

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-19 : 08:16:10
If you are on SQL 2005 or higher, the simplest probably is to use the OUTPUT clause. It would be something like this:

DELETE YourOldTable
OUTPUT DELETED.Col1, DELETED.Col2 --,etc.
INTO YourNewTable
WHERE
-- appropriate where clausehere.

http://msdn.microsoft.com/en-us/library/ms177564.aspx
Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-19 : 23:10:33
is there any other way

challenge everything
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-20 : 00:07:32
quote:
Originally posted by pnpsql

is there any other way

challenge everything


other way is to use trigger. create a trigger on YourOldTable for DELETE and do insertin inside that
something like


CREATE TRIGGER Triggernamehere
ON YourOldTable
FOR DELETE
AS
BEGIN
INSERT INTO YourNewTable (Col1,Col2,..)
SELECT Col1,Col2,..
FROM DELETED
END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -