Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

Delete a row into a new table?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gerry Hickman - 21 Jul 2008 22:30 GMT
Hi,

Is it possible (using T-SQL) to delete a row from one table and insert
it into another? e.g. I have a table dbo.employee, an employee leaves,
so I delete their record, but want to place it into dbo.employee_old

Signature

Gerry Hickman (London UK)

Andrew J. Kelly - 21 Jul 2008 23:21 GMT
Sure but not in one statement.

BEGIN TRAN

   INSERT INTO dbo.employee_old (column list)
       SELECT Column list from dbo_employee WHERE Employee_ID = xxx

   DELETE FROM dbo.employee WHERE Employee_ID = xxx

COMMIT TRAN

Of course you may want to put some error logic in there but that is the
general idea.

Signature

Andrew J. Kelly    SQL MVP
Solid Quality Mentors

> Hi,
>
> Is it possible (using T-SQL) to delete a row from one table and insert it
> into another? e.g. I have a table dbo.employee, an employee leaves, so I
> delete their record, but want to place it into dbo.employee_old
Dan Guzman - 22 Jul 2008 01:40 GMT
> Is it possible (using T-SQL) to delete a row from one table and insert it
> into another? e.g. I have a table dbo.employee, an employee leaves, so I
> delete their record, but want to place it into dbo.employee_old

If you are using SQL 2005, you can use the OUTPUT clause.  For example:

DELETE FROM dbo.employee
   OUTPUT deleted.*
   INTO dbo.employee_old
WHERE employee_id = 1

Signature

Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

> Hi,
>
> Is it possible (using T-SQL) to delete a row from one table and insert it
> into another? e.g. I have a table dbo.employee, an employee leaves, so I
> delete their record, but want to place it into dbo.employee_old
Andrew J. Kelly - 22 Jul 2008 15:39 GMT
Good point Dan.

Signature

Andrew J. Kelly    SQL MVP
Solid Quality Mentors

>> Is it possible (using T-SQL) to delete a row from one table and insert it
>> into another? e.g. I have a table dbo.employee, an employee leaves, so I
[quoted text clipped - 12 lines]
>> into another? e.g. I have a table dbo.employee, an employee leaves, so I
>> delete their record, but want to place it into dbo.employee_old
Gerry Hickman - 22 Jul 2008 21:55 GMT
> Good point Dan.

Interesting!

Thanks for the ideas; I've just realized this database is on SQL 2000,
but hope to have it moved to SQL 2005 soon, and the OUTPUT clause looks
useful.

Signature

Gerry Hickman (London UK)

Sha Anand - 22 Jul 2008 06:41 GMT
Why not use triggers :)

- Sha Anand

> Hi,
>
> Is it possible (using T-SQL) to delete a row from one table and insert
> it into another? e.g. I have a table dbo.employee, an employee leaves,
> so I delete their record, but want to place it into dbo.employee_old
Gerry Hickman - 22 Jul 2008 21:56 GMT
> Why not use triggers :)

Thanks, I'll have a look at triggers in the BOL.

Signature

Gerry Hickman (London UK)

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.