2

Is there an easy way to test whether the primary key of a record is referenced in any other table in the database without going and searching for said primary key in all the applicable tables?

I want to know before I get to the exception, and disable a delete button.

Carel
  • 2,063
  • 8
  • 39
  • 65
  • possible duplicate of [Checking for Referential Integrity Violation before deletion in ASP .NET MVC](http://stackoverflow.com/questions/7564542/checking-for-referential-integrity-violation-before-deletion-in-asp-net-mvc) – Neil Thompson Mar 25 '14 at 12:06
  • Check this http://stackoverflow.com/questions/960242/checking-referential-integrity-rule-violation-before-deletion – Prasanth V J Mar 25 '14 at 12:32

1 Answers1

1

The way I normally deal with this foreign keys and references are by using EXISTS (Transact-SQL). By doing it this way, you have to use one EXISTS for every foreign key you want to check.

Another way of dealing with it is just to catch the exception and handling it in the code.

Teis Lindemark
  • 352
  • 5
  • 16