I've found some good directions from stack-overflow. Combining a few solutions, I could get the result that I wanted.
Query 1 :
Query 2 :
Query 1 :
SELECT RC.constraint_name AS ForeignKey,
KF.table_name AS TableName,
KF.column_name AS ColumnName,
KP.table_name AS ReferenceTableName,
KP.column_name PK_Column,
RC.match_option MatchOption,
RC.update_rule UpdateRule,
RC.delete_rule DeleteRule
FROM information_schema.referential_constraints RC
JOIN information_schema.key_column_usage KF
ON RC.constraint_name = KF.constraint_name
JOIN information_schema.key_column_usage KP
ON RC.unique_constraint_name = KP.constraint_name
KF.table_name AS TableName,
KF.column_name AS ColumnName,
KP.table_name AS ReferenceTableName,
KP.column_name PK_Column,
RC.match_option MatchOption,
RC.update_rule UpdateRule,
RC.delete_rule DeleteRule
FROM information_schema.referential_constraints RC
JOIN information_schema.key_column_usage KF
ON RC.constraint_name = KF.constraint_name
JOIN information_schema.key_column_usage KP
ON RC.unique_constraint_name = KP.constraint_name
SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
No comments:
Post a Comment