這應該給你你在找什麼:
SELECT t.name AS [TableName]
,c.name AS [ColumnName]
,CASE WHEN COALESCE(tr.name, tr2.name) IS NULL THEN 'No'
ELSE 'Yes' END AS IsColumnInRelation
,COALESCE(tr.name, tr2.name) AS [RelatedTable]
,COALESCE(cr.name, cr2.name) AS [RelatedColumn]
,CASE WHEN COALESCE(tr.name, tr2.name) IS NOT NULL THEN
CASE WHEN i.object_id IS NOT NULL THEN 'One'
ELSE 'Many' END
END AS RelationKind
,COALESCE(f.update_referential_action_desc, f2.update_referential_action_desc) AS [InsertUpdateAction]
,COALESCE(f.delete_referential_action_desc, f2.delete_referential_action_desc) AS [DeleteAction]
,c.is_nullable AS [IsNull]
,p.value AS Description
FROM sys.columns c
INNER JOIN sys.tables t
ON t.object_id= c.object_id
-- Used for the column description.
LEFT JOIN sys.extended_properties p
ON c.object_id = p.major_id
AND c.column_id = p.minor_id
AND p.name = 'MS_Description'
-- Used to identify if this is a primary key.
-- If it is, then this is on the "One" side of the relationship.
LEFT JOIN sys.index_columns ic
ON ic.object_id = c.object_id
AND ic.column_id = c.column_id
LEFT JOIN sys.indexes i
ON ic.object_id = i.object_id
AND i.is_primary_key = 1
-- Used to get the tables and columns that relate to this column.
LEFT JOIN sys.foreign_key_columns AS fc
ON c.object_id = fc.parent_object_id
AND c.column_id = fc.parent_column_id
LEFT JOIN sys.columns cr
ON cr.object_id = fc.referenced_object_id
AND cr.column_id = fc.referenced_column_id
LEFT JOIN sys.tables tr
ON cr.object_id = tr.object_id
-- Used to get the update/delete action for the [fc] relationship.
LEFT JOIN sys.foreign_keys f
ON f.object_id = fc.constraint_object_id
-- Used to get the tables and columns that this column relates to.
LEFT JOIN sys.foreign_key_columns AS fc2
ON c.object_id = fc2.referenced_object_id
AND c.column_id = fc2.referenced_column_id
LEFT JOIN sys.columns cr2
ON cr2.object_id = fc2.parent_object_id
AND cr2.column_id = fc2.parent_column_id
LEFT JOIN sys.tables tr2
ON cr2.object_id = tr2.object_id
-- Used to get the update/delete action for the [fc2] relationship.
LEFT JOIN sys.foreign_keys f2
ON f2.object_id = fc2.constraint_object_id
ORDER BY t.name, c.name, COALESCE(tr.name, tr2.name), COALESCE(cr.name, cr2.name)
這將返回我一些重複列 – Mohsen