我需要列出所有相互依赖的表
例子
TableA 与 TableB 具有外键关系 TableB 与 TableA 具有外键关系
到目前为止,我得到了查找表的外键关系的脚本,但是如何过滤它们以仅获取交叉表引用之一。
我的脚本是:
SELECT CAST(p.name AS VARCHAR(255)) AS [Primary Table] ,
CAST(c.name AS VARCHAR(255)) AS [Foreign Table]
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))