我正在尝试改进这个query.link
但是系统抱怨“tp”(无效的对象名“tp”)
有没有办法从有行(不是空的)的表中获取外键。
SELECT
fk.name 'FK Name',
tp.name 'Parent table',
cp.name, cp.column_id,
tr.name 'Refrenced table',
cr.name, cr.column_id
FROM
sys.foreign_keys fk
INNER JOIN
sys.tables tp ON fk.parent_object_id = tp.object_id
INNER JOIN
sys.tables tr ON fk.referenced_object_id = tr.object_id
INNER JOIN
sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN
sys.columns cp ON fkc.parent_column_id = cp.column_id AND fkc.parent_object_id = cp.object_id
INNER JOIN
sys.columns cr ON fkc.referenced_column_id = cr.column_id AND fkc.referenced_object_id = cr.object_id
where (SELECT COUNT(*) FROM tp)>0 --HERE IS THE PROBLEM WITH tp
ORDER BY
tp.name, cp.column_id
1条答案
按热度按时间2ledvvac1#
使用
EXISTS
用于标识非空表的相关子查询。下面是一个使用sys.partitions
.