Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete and insert in one statement...

Status
Not open for further replies.

SrkrReds

Programmer
Dec 17, 2001
36
0
0
US
Hi All..
Can someone help me with syntax on how to select a record from tableA(based on some criteria) and insert into tableB and then delete that record from tableA, all in one single statement in query analyzer. Do I use the memory resident DELETED table(can this even be used anywhere other than triggers) for this purpose and if so how?

THanx in advance....
 
Declare @ID as INTEGER;
SET @ID = (select the primary key of the table you want)

Insert into table2
select record from table1

delete from table1
where primarykeyid = @id

this work?
 
Thanx for the suggestion. I was doing something like that based on some criteria. I was wondering if all of it can be done in one sql statement (something like a subquery - selecting the deleted record from DELETED table) without declaring any variables.

Thanx again.
 
DELETED/INSETED pseudo-tables are available in triggers context only.
If you need to accomplish this in one statement executed from Query Analyzer, write a stored procedure or create a trigger for INSERT.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top