How can I list all foreign keys referencing a given table in SQL Server?

后端 未结 26 2637
梦毁少年i
梦毁少年i 2020-11-22 07:13

I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the foreign key constraints I will need to remove in order to drop the tabl

相关标签:
26条回答
  • 2020-11-22 08:07

    The following solution work for me:

    --Eliminar las llaves foraneas
    declare @query varchar(8000)
    declare cursorRecorrerTabla cursor for
    
    SELECT  'ALTER TABLE [PoaComFinH].['+sch.name+'].['+referencingTable.Name+'] DROP CONSTRAINT ['+foreignKey.name+']' 'query'
    FROM PoaComFinH.sys.foreign_key_columns fk
    JOIN PoaComFinH.sys.tables referencingTable ON fk.parent_object_id = referencingTable.object_id
    JOIN PoaComFinH.sys.schemas sch ON referencingTable.schema_id = sch.schema_id
    JOIN PoaComFinH.sys.objects foreignKey ON foreignKey.object_id = fk.constraint_object_id
    JOIN PoaComFinH.sys.tables referencedTable ON fk.referenced_object_id = referencedTable.object_id
    
    
    --3ro. abrir el cursor.
    open cursorRecorrerTabla
    fetch next from cursorRecorrerTabla
    into @query
    while @@fetch_status = 0
    begin
    --inicio cuerpo del cursor
        print @query
        exec(@query)
    --fin cuerpo del cursor
    fetch next from cursorRecorrerTabla
    into @query
    end
    --cerrar cursor
    close cursorRecorrerTabla
    deallocate cursorRecorrerTabla
    
    0 讨论(0)
  • 2020-11-22 08:10

    There is how to get count of all responsibilities for selected Id. Just change @dbTableName value, @dbRowId value and its type (if int you need to remove '' in line no 82 (..SET @SQL = ..)). Enjoy.

    DECLARE @dbTableName varchar(max) = 'User'
    DECLARE @dbRowId uniqueidentifier = '21d34ecd-c1fd-11e2-8545-002219a42e1c'
    
    DECLARE @FK_ROWCOUNT int
    DECLARE @SQL nvarchar(max)
    
    DECLARE @PKTABLE_QUALIFIER sysname
    DECLARE @PKTABLE_OWNER sysname
    DECLARE @PKTABLE_NAME sysname
    DECLARE @PKCOLUMN_NAME sysname
    DECLARE @FKTABLE_QUALIFIER sysname
    DECLARE @FKTABLE_OWNER sysname
    DECLARE @FKTABLE_NAME sysname
    DECLARE @FKCOLUMN_NAME sysname
    DECLARE @UPDATE_RULE smallint
    DECLARE @DELETE_RULE smallint
    DECLARE @FK_NAME sysname
    DECLARE @PK_NAME sysname
    DECLARE @DEFERRABILITY sysname
    
    IF OBJECT_ID('tempdb..#Temp1') IS NOT NULL
        DROP TABLE #Temp1;
    CREATE TABLE #Temp1 ( 
        PKTABLE_QUALIFIER sysname,
        PKTABLE_OWNER sysname,
        PKTABLE_NAME sysname,
        PKCOLUMN_NAME sysname,
        FKTABLE_QUALIFIER sysname,
        FKTABLE_OWNER sysname,
        FKTABLE_NAME sysname,
        FKCOLUMN_NAME sysname,
        UPDATE_RULE smallint,
        DELETE_RULE smallint,
        FK_NAME sysname,
        PK_NAME sysname,
        DEFERRABILITY sysname,
        FK_ROWCOUNT int
        );
    DECLARE FK_Counter_Cursor CURSOR FOR
        SELECT PKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()),
           PKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O1.SCHEMA_ID)),
           PKTABLE_NAME = CONVERT(SYSNAME,O1.NAME),
           PKCOLUMN_NAME = CONVERT(SYSNAME,C1.NAME),
           FKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()),
           FKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O2.SCHEMA_ID)),
           FKTABLE_NAME = CONVERT(SYSNAME,O2.NAME),
           FKCOLUMN_NAME = CONVERT(SYSNAME,C2.NAME),
           -- Force the column to be non-nullable (see SQL BU 325751)
           --KEY_SEQ             = isnull(convert(smallint,k.constraint_column_id), sysconv(smallint,0)),
           UPDATE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.OBJECT_ID,'CnstIsUpdateCascade') 
                                            WHEN 1 THEN 0
                                            ELSE 1
                                          END),
           DELETE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.OBJECT_ID,'CnstIsDeleteCascade') 
                                            WHEN 1 THEN 0
                                            ELSE 1
                                          END),
           FK_NAME = CONVERT(SYSNAME,OBJECT_NAME(F.OBJECT_ID)),
           PK_NAME = CONVERT(SYSNAME,I.NAME),
           DEFERRABILITY = CONVERT(SMALLINT,7)   -- SQL_NOT_DEFERRABLE
        FROM   SYS.ALL_OBJECTS O1,
               SYS.ALL_OBJECTS O2,
               SYS.ALL_COLUMNS C1,
               SYS.ALL_COLUMNS C2,
               SYS.FOREIGN_KEYS F
               INNER JOIN SYS.FOREIGN_KEY_COLUMNS K
                 ON (K.CONSTRAINT_OBJECT_ID = F.OBJECT_ID)
               INNER JOIN SYS.INDEXES I
                 ON (F.REFERENCED_OBJECT_ID = I.OBJECT_ID
                     AND F.KEY_INDEX_ID = I.INDEX_ID)
        WHERE  O1.OBJECT_ID = F.REFERENCED_OBJECT_ID
               AND O2.OBJECT_ID = F.PARENT_OBJECT_ID
               AND C1.OBJECT_ID = F.REFERENCED_OBJECT_ID
               AND C2.OBJECT_ID = F.PARENT_OBJECT_ID
               AND C1.COLUMN_ID = K.REFERENCED_COLUMN_ID
               AND C2.COLUMN_ID = K.PARENT_COLUMN_ID
               AND O1.NAME = @dbTableName
    OPEN FK_Counter_Cursor;
    FETCH NEXT FROM FK_Counter_Cursor INTO @PKTABLE_QUALIFIER, @PKTABLE_OWNER, @PKTABLE_NAME, @PKCOLUMN_NAME, @FKTABLE_QUALIFIER, @FKTABLE_OWNER, @FKTABLE_NAME, @FKCOLUMN_NAME, @UPDATE_RULE, @DELETE_RULE, @FK_NAME, @PK_NAME, @DEFERRABILITY;
    WHILE @@FETCH_STATUS = 0
       BEGIN
            SET @SQL = 'SELECT @dbCountOut = COUNT(*) FROM [' + @FKTABLE_NAME + '] WHERE [' + @FKCOLUMN_NAME + '] = ''' + CAST(@dbRowId AS varchar(max)) + '''';
            EXECUTE sp_executesql @SQL, N'@dbCountOut int OUTPUT', @dbCountOut = @FK_ROWCOUNT OUTPUT;
            INSERT INTO #Temp1 (PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME, FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY, FK_ROWCOUNT) VALUES (@FKTABLE_QUALIFIER, @PKTABLE_OWNER, @PKTABLE_NAME, @PKCOLUMN_NAME, @FKTABLE_QUALIFIER, @FKTABLE_OWNER, @FKTABLE_NAME, @FKCOLUMN_NAME, @UPDATE_RULE, @DELETE_RULE, @FK_NAME, @PK_NAME, @DEFERRABILITY, @FK_ROWCOUNT)
          FETCH NEXT FROM FK_Counter_Cursor INTO @PKTABLE_QUALIFIER, @PKTABLE_OWNER, @PKTABLE_NAME, @PKCOLUMN_NAME, @FKTABLE_QUALIFIER, @FKTABLE_OWNER, @FKTABLE_NAME, @FKCOLUMN_NAME, @UPDATE_RULE, @DELETE_RULE, @FK_NAME, @PK_NAME, @DEFERRABILITY;
       END;
    CLOSE FK_Counter_Cursor;
    DEALLOCATE FK_Counter_Cursor;
    GO
    SELECT * FROM #Temp1
    GO
    
    0 讨论(0)
提交回复
热议问题