How do I preview a destructive SQL query?

I would use the OUTPUT clause present in SQL SERVER 2008 onwards…

OUTPUT Clause (Transact-SQL)

Something like…

BEGIN TRANSACTION

DELETE [table] OUTPUT deleted.* WHERE [woof] 

ROLLBACK TRANSACTION

INSERTs and UPDATEs can use the ‘inserted’ table too. The MSDN article covers it all.

EDIT:

This is just like other suggestions of SELECT then DELETE inside a transaction, except that it actually does both together. So you open a transaction, delete/insert/update with an OUTPUT clause, and the changes are made while ALSO outputting what was done. Then you can choose to rollback or commit.

Leave a Comment