How to find Table relationship in SQL SERVER 2005 / 2008

/************************************************************
 * Code formatted by SoftTree SQL Assistant © v4.7.11
 * Time: 6/1/2011 2:46:16 PM
 ************************************************************/

DECLARE @tablename VARCHAR(MAX)
SET @tablename = 'Cou_Course_Mst'
SELECT OBJECT_NAME(rkeyid) Parent_Table,
       OBJECT_NAME(fkeyid) Child_Table,
       OBJECT_NAME(constid) FKey_Name,
       c1.name FKey_Col,
       c2.name Ref_KeyCol
FROM   sys.sysforeignkeys s
       INNER JOIN sys.syscolumns c1
            ON  (s.fkeyid = c1.id AND s.fkey = c1.colid)
       INNER JOIN syscolumns c2
            ON  (s.rkeyid = c2.id AND s.rkey = c2.colid)
WHERE  OBJECT_NAME(rkeyid) = @tablename
ORDER BY
       Parent_Table,
       Child_Table
Ebook Download
View all
Learn
View all