现在我有列出所有引用表的代码,而且我可以一一计算表中的引用。
查询一:
SELECT t.NAME AS TableWithForeignKey,
c.NAME AS ForeignKeyColumn
FROM sys.foreign_key_columns AS fk
INNER JOIN sys.tables AS t
ON fk.parent_object_id = t.object_id
INNER JOIN sys.columns AS c
ON fk.parent_object_id = c.object_id
AND fk.parent_column_id = c.column_id
WHERE fk.referenced_object_id = (
SELECT object_id
FROM sys.tables
WHERE NAME = 'MAIN_TABLE'
)
查询 2:
SELECT COUNT(MAIN_ID)
FROM MAIN_TABLE
INNER JOIN REF_TABLE_1
ON MAIN_TABLE.ID = REF_TABLE_1.MAIN_ID
查询 3:
SELECT COUNT(MAIN_ID)
FROM MAIN_TABLE
INNER JOIN REF_TABLE_2
ON MAIN_TABLE.ID = REF_TABLE_2.MAIN_ID
等等
是否可以在一个查询中使用它?