Technical Article

Foreign Key Lookup (Name, columns, tables)

,

This script will find all foreign key contraints in the database it is run on.  Includes.

Name of Foreign Key
Name of table FK is on
Name of column FK is on
Name of table FK references
Name of column FK references

select	so1.Name as FKConstraint, so.Name as FromTable, 
		sc.Name as FromColumn, so2.Name as ToTable, sc1.Name as ToColumn
From 	sysforeignkeys fk (nolock)
JOIN	sysobjects 	so (nolock) on so.[id] = fk.fkeyid
JOIN	sysobjects 	so1 (nolock) on fk.constid = so1.id
join	syscolumns 	sc (nolock) on fk.fkeyid = sc.id and fk.fkey = sc.colid
JOIN	sysobjects 	so2 (nolock) on fk.rkeyid = so2.id
join	syscolumns 	sc1 (nolock) on fk.rkeyid = sc1.id and fk.rkey = sc1.colid
--where	so.name = <Table Name>

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating