You can use the OBJECTPROPERTY()
function in SQL Server to check whether or not a table is referenced by a foreign key.
To do this, pass the table’s object ID as the first argument, and TableHasForeignRef
as the second argument. The function returns a 1
or a 0
depending on whether or not it is referenced by a foreign key.
A return value of 1
means that the table is referenced by a foreign key, and a value of 0
means that it’s not.
Note that the examples presented here don’t list out the foreign keys or their tables or anything like that. They simply return a true/false value that you can use to test whether or not a table is referenced by a foreign key. If you need to list out all foreign keys that reference a given table, see Return All Foreign Keys that Reference a Given Table in SQL Server. The examples in that article list out each foreign key, as well as the foreign key table/s, and the primary key table.
Continue reading →