/ Published in: SQL
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
SELECT CAST(f.name AS VARCHAR(255)) AS foreign_key_name , r.keycnt , CAST(c.name AS VARCHAR(255)) AS foreign_table , CAST(fc.name AS VARCHAR(255)) AS foreign_column_1 , CAST(fc2.name AS VARCHAR(255)) AS foreign_column_2 , CAST(p.name AS VARCHAR(255)) AS primary_table , CAST(rc.name AS VARCHAR(255)) AS primary_column_1 , CAST(rc2.name AS VARCHAR(255)) AS primary_column_2 FROM sysobjects f INNER JOIN sysobjects c ON f.parent_obj = c.id INNER JOIN sysreferences r ON f.id = r.constid INNER JOIN sysobjects p ON r.rkeyid = p.id INNER JOIN syscolumns rc ON r.rkeyid = rc.id AND r.rkey1 = rc.colid INNER JOIN syscolumns fc ON r.fkeyid = fc.id AND r.fkey1 = fc.colid LEFT JOIN syscolumns rc2 ON r.rkeyid = rc2.id AND r.rkey2 = rc.colid LEFT JOIN syscolumns fc2 ON r.fkeyid = fc2.id AND r.fkey2 = fc.colid WHERE f.type = 'F' -- ORDER BY cast(p.name as varchar(255)) ORDER BY primary_table