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

时间:2012-03-08 23:49:23

标签: sql sql-server sql-server-2008 foreign-keys

我有桌子动物。

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

I.E。:

Animal_Id    Name  
    1        Cat
    2        Dog
    3        Parrot

我想要归还:

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

by“referenced”我的意思是,如果特定的Animal_Id在数据库中的任何其他表中被引用为外键。我想我首先可以查询information_schema并找出哪些表包含这个外键然后使用循环和动态sql来执行
select count(*) from eachTable where AnimalID = 1

有没有人知道如何做到这一点?

1 个答案:

答案 0 :(得分:13)

这应该这样做:

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)