I'd like to be able to predict whether a DELETE will run into a constraint violation, without actually performing the delete.
What are my options for doing this? Is there a simple way to do a "dry run" of a DELETE?
|
I'd like to be able to predict whether a DELETE will run into a constraint violation, without actually performing the delete. What are my options for doing this? Is there a simple way to do a "dry run" of a DELETE? |
|||||||||||||
|
|
If your goal is to process all deletes only if they all succeed, why not just use TRY/CATCH:
If the goal is to allow all successful deletes to succeed even if one or more will fail, then you can use individual TRY/CATCH, e.g.
|
|||
|
|
|
One option is to begin a transaction, run your delete, and then always rollback:
|
|||||||||||||||||
|
|
I would like to improve the solution provided by Aaron Bertrand with some code, in case you want to try to add any element of a table, managing the exceptions to ignore fails or also stop the process afters errors. This one will select the records from the table and then tries to delete them without exceptions:
|
|||
|
|