List All Foreign Keys Referencing A Table In SQL Server

There may be times that you need to get a list of tables that have foreign key constraints back to a table that you’re working on.  Fortunately, SQL Server has a built in system procedure that makes this extremely simple.  Let me introduce you to sp_fkeys.  This sweet little proc returns information about foreign key relationships based off of the parameters passed to it.

Getting started with sp_fkeys couldn’t be simpler, just specify the table name you want to look up the foreign keys for!  So, for example, if you wanted to look up all of the tables within your database that have foreign keys back to a users table, you would use the following:

Pretty simple, right?  You can also pass in other parameters besides the table name, see the MSDN doc for the full list.

(Visited 7,615 times, 1 visits today)