SQL SERVER – Listing Foreign Key Relationships of Table with Stored Procedure sp_fkeys

A very cool trick which I have previously not shared on my blog regarding how to list all the foreign key relationship of table with the help of Stored Procedure sp_fkeys.

Here is a quick script how you can use stored procedure sp_fkeys to list all the foreign key relationship. This stored procedure takes a minimum of two parameters a) name of the table, b) name of the schema.

USE AdventureWorks2012;
GO
EXEC sp_fkeys @pktable_name = N'Employee',
@pktable_owner = N'HumanResources';

Here is the output generated from the script listed above:

Here is an alternate way to list the keys as well – Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database.

Reference: Pinal Dave (http://blog.sqlauthority.com)

About these ads

One thought on “SQL SERVER – Listing Foreign Key Relationships of Table with Stored Procedure sp_fkeys

  1. Pingback: SQL SERVER – Listing Primary Key of Table with Stored Procedure sp_pkeys | Journey to SQL Authority with Pinal Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s