![]() |
|
|
By: Greg Robidoux | Read Comments (3) | Print Greg is the President of Edgewood Solutions and a co-founder of MSSQLTips.com. Related Tips: 1 | 2 | 3 | 4 | 5 | 6 | More |
|
Problem
SQL Server Enterprise Manager and Management Studio are pretty good tools for giving you information about a particular object, but when you want to get information across your entire database or server this becomes a bit of a challenge. There are several system stored procedures and now dynamic management views in SQL 2005 to provide some of the information, but there is still a bit of missing functionality if you want to get data across the database or server. One of these recent challenges was getting foreign key information across the entire database. Management Studio has some nice additions where you can see the FK constraints on a table by table basis, but getting all of the foreign keys is a bit more a challenge. There is a stored procedure called sp_fkeys, but this procedure requires you to include at least the primary key table name. So how can you get a complete list?
Solution
There is a wealth of information in the system stored procedures and this is another good way of learning how things work behind the scenes. By taking a look at the stored procedure sp_fkeys for SQL Server 2005 I noticed that it is not very complicated at all. When you pass in the parameters it basically uses the parameters to filter your selection. By making a minor tweak to the SELECT statement we are able to get a list of all FKs in the entire database. Here is the modified query for SQL Server 2005.
SQL Server 2005 Version
|
|
SQL Server 2005 - Version 2
This is another version that was contributed by one of our readers Troy Ketsdever. This uses the INFORMATION_SCHEMA views.
SELECT C.TABLE_CATALOG [PKTABLE_QUALIFIER], |
SQL Server 2000 Version
With SQL Server 2000 it is not as straight forward as using a simple SQL statement as shown above. In order to get this to work for SQL Server 2000 an adjustment was made to the sp_fkeys SP to cursor through all of the user tables. It is not the prettiest method, but it does seem to do the job.
click here for the SQL 2000 version
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Monday, October 13, 2008 - 11:24:47 AM - jgonte | Read The Tip |
|
Hi, Great article! I think there is an error in version 2 of the script where the information of the primary key corresponds to the foreign key and viceversa.
|
|
| Monday, March 22, 2010 - 4:19:48 PM - phillips_jim | Read The Tip |
|
That may explain why the two scripts produce the same number of output rows with the same columns but return different results. |
|
| Thursday, December 01, 2011 - 6:46:36 AM - Atif | Read The Tip |
|
I use my following script for analyzing foreign key constraints in any database
select fkey.name, object_name(fkey.parent_object_id) FKTable,cols.name FKColumn, Hope it would also work fine. |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |