SQL Server检测一对一或一对多

时间:2015-10-24 00:09:15

标签: sql .net sql-server

我需要检测外键是1-1还是1。我怎么能在SQL Server中执行此操作?

我的用例是我正在扩展PetaPoco,以便它有额外的计算字段,指向相应的外键记录。问题是我有1-1和1多个表。我需要检测,以便附加字段可以是poco或List。

    SELECT tab1.name as [source_table],
col1.name AS [source_column],
 tab2.name AS [referenced_table],
    col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
    ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
    ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.columns col1
    ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
    ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
    ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id

0 个答案:

没有答案