Created
November 21, 2013 10:11
-
-
Save szunyog/7579122 to your computer and use it in GitHub Desktop.
Lists all foreign keys (source and ref columns) in a MSSQL database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
t.name AS TableWithForeignKey, | |
c.name AS ForeignKeyColumn, | |
r.name AS ReferencedTable, | |
rc.name AS ReferencedColumnName | |
FROM sys.foreign_key_columns AS fk | |
inner join sys.tables AS t ON fk.parent_object_id = t.object_id | |
inner join sys.columns AS c ON (t.object_id = c.object_id AND fk.parent_column_id = c.column_id) | |
inner join sys.tables AS r ON fk.referenced_object_id = r.object_id | |
inner join sys.columns AS rc ON (r.object_id = rc.object_id AND rc.column_id = fk.referenced_column_id) | |
ORDER BY | |
TableWithForeignKey, | |
fk.constraint_column_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment