Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete rows from CTE in SQL SERVER

Tags:

I have a CTE which is a select statement on a table. Now if I delete 1 row from the CTE, will it delete that row from my base table?

Also is it the same case if I have a temp table instead of CTE?

like image 591
satyajit Avatar asked May 15 '11 18:05

satyajit


1 Answers

Checking the DELETE statement documentation, yes, you can use a CTE to delete from and it will affect the underlying table. Similarly for UPDATE statements...

Also is it the same case if I have a temp table instead of CTE?

No, deletion from a temp table will affect the temp table only -- there's no connection to the table(s) the data came from, a temp table is a stand alone object.

like image 80
OMG Ponies Avatar answered Oct 05 '22 10:10

OMG Ponies