一尘不染

查询以确定是否在数据库中的其他任何地方引用了外键

sql

我想返回此表中的所有内容+一列,该列表示是否在其他任何地方将记录作为外键引用。

IE:

Animal_Id    Name  
    1        Cat
    2        Dog
    3        Parrot

我要退货:

AnimalId    Name     Referenced 
    1        Cat         true
    2        Dog         false
    3        Parrot      true

“引用”是指是否在数据库的任何其他表中将特定的Animal_Id作为外键引用了。我想我首先可以查询information_schema并找出哪些表包含此外键,然后使用循环和动态sql执行
select count(*) from eachTable where AnimalID = 1

有人有摘录该怎么做吗?


阅读 127

收藏
2021-05-23

共1个答案

一尘不染

这应该做到这一点:

SELECT OO.Animal_ID, OO.Name, CASE WHEN XX.REFERENCED IS NULL THEN 'false' ELSE 'true' END Referenced
FROM   Animal OO
       OUTER APPLY (SELECT SUM(1) REFERENCED
                    FROM   (SELECT FkAnimal_ID FROM AnimalRef1 RR WHERE RR.FkAnimal_ID = OO.Animal_ID UNION ALL
                            SELECT FkAnimal_ID FROM AnimalRef2 RR WHERE RR.FkAnimal_ID = OO.Animal_ID UNION ALL
                            SELECT FkAnimal_ID FROM AnimalRef3 RR WHERE RR.FkAnimal_ID = OO.Animal_ID) II) XX

如果您不了解所有FK表,则可以使用系统元数据表将UNION ALL查询的集合生成到表中,然后可以将其复制并粘贴到查询中:

WITH AKT AS ( SELECT f.name AS ForeignKey
                    ,OBJECT_NAME(f.parent_object_id) AS TableName
                    ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName
                    ,OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName
                    ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
              FROM   sys.foreign_keys AS f
                     INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
              WHERE  f.referenced_object_id = object_id('Animal'))
SELECT 'SELECT ' + ColumnName + ' FROM ' + TableName + ' WHERE  RR.' + ColumnName + ' = OO.' + ReferenceColumnName + ' UNION ALL'
FROM   AKT

对于使用递归CTE的单个查询中的全部内容:

DECLARE @QUERY NVARCHAR(MAX)

WITH AKT AS ( SELECT ROW_NUMBER() OVER (ORDER BY f.name) RN, f.name AS ForeignKey
                    ,OBJECT_NAME(f.parent_object_id) AS TableName
                    ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName
                    ,SCHEMA_NAME(oo.schema_id) SchemaName
                    ,OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName
                    ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
              FROM   sys.foreign_keys AS f
                     INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
                     INNER JOIN sys.objects oo ON oo.object_id = fc.referenced_object_id
              WHERE  f.referenced_object_id = object_id('Animal'))

    ,bs AS (SELECT AKT.RN
                  ,'SELECT ' + ColumnName + ' FROM ' + SchemaName + '.' + TableName + ' WHERE ' + ColumnName + ' = OO.' + ReferenceColumnName  SubQuery
            FROM   AKT)
    ,re AS (SELECT bs.RN, CAST(RTRIM(bs.SubQuery) AS VARCHAR(MAX)) Joined
            FROM   bs
            WHERE  bs.RN = 1
            UNION  ALL
            SELECT bs2.RN, CAST(re.Joined + ' UNION ALL ' + ISNULL(RTRIM(bs2.SubQuery), '') AS VARCHAR(MAX)) Joined
            FROM   re, bs bs2 
            WHERE  re.RN = bs2.RN - 1 )
    ,fi AS (SELECT ROW_NUMBER() OVER (ORDER BY RN DESC) RNK, Joined
            FROM   re)
SELECT @QUERY  = 'SELECT OO.Animal_ID, OO.Name, CASE WHEN XX.REFERENCED IS NULL THEN ''No'' ELSE ''Yes'' END Referenced
FROM   Animal OO
       OUTER APPLY (SELECT SUM(1) REFERENCED
                    FROM   (' + Joined + ') II) XX'
FROM   fi
WHERE  RNK = 1

EXEC (@QUERY)
2021-05-23