I have a stored procedure that starts a transactions. Inside it, I disable some of the constraints. I modify the data. And then enable the constraints.
What I want is to rollback if the data in inconsistent in the end of the SP (prior to calling commit of course).
Currently I run DBCC CHECKCONSTRAINTS in the end to see what was broken.
But can I just rollback when DBCC CHECKCONSTRAINTS outputs any data?
Or better: Can the DBCC CHECKCONSTRAINTS raise error if any of the constraints are broken?
Instead of using DBCC CHECKCONSTRAINTS, when you enable the constraints again, use the
WITH CHECKoption.If there is a violation, you’ll get an error like:
Use the sample code below for a simple demonstration.