
Checking if a delete will succeed (without doing the delete)
Quote:
> Thanks for the reply. Essentially I'm checking some business rules to see
> what can be deleted, and some of these business rules are in the form of
> foreign key constraints. Obviously I don't want to duplicate them in code.
It all depends. If it is sufficient for you to give the user the error
message: "You cannnot delete this records, because it is being referenced",
an attempted DELETE is certainly the easiest way to go.
But if the user wants to know what is referencing, it's another matter.
You can of course parse the error message and then translate the table
name to something which makes sense to the user. But if the user wants
to know exactly which record that is referencing, then you need to do
the checks yourself.
Personally, I'm lean towards the position that the constraints in the
database mainly for checking that the client developers are doing
things right.
--
Erland Sommarskog, Abaris AB
SQL Server MVP