Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL Select Delete

Just curious... is there a command in TSQL which will delete rows I just selected from a table?

In pseudo SQL, something like

SELECT * FROM TableA THEN DELETE

Or

SELECT * FROM TableA WITH (DELETEROWS)

NOTE: I'm not just looking to delete rows, I want to return the rows to a temptable and then delete them, in the same statement

like image 923
roryok Avatar asked Apr 16 '26 16:04

roryok


1 Answers

You can get the rows that were just deleted using the output clause:

DELETE FROM TableA
OUTPUT deleted.* --TODO name columns, identify table to insert into, etc

So, overall should be the same, just the order of operations is reversed.

like image 137
Damien_The_Unbeliever Avatar answered Apr 18 '26 07:04

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!