SQL Script to alter ALL Foreign Keys to add ON DELETE CASCADE

后端 未结 7 1801
轻奢々
轻奢々 2020-12-04 08:21

I have a SQL 2005 database with approx 250 tables.

I want to temporarily enable ON DELETE CASCADE to all of the Foreign Keys so that I can do a bulk delete easily.

7条回答
  •  死守一世寂寞
    2020-12-04 09:08

    The answer from @Andomar worked for me but it was a bit manual - you have to run it, then copy the results and run them. I needed to use this as part of my automated test setup so needed it to run in one query automatically.

    I have come up with the following which gets all the SQL to run in order to modify the foreign key constraints, then actually runs it all in one go:

        IF Object_id('tempdb..#queriesForContraints') IS NOT NULL 
          BEGIN 
              DROP TABLE #queriesForContraints
          END 
    
        DECLARE @ignoreTablesCommaSeparated VARCHAR(1000)
    
        SELECT 'ALTER TABLE [' 
               + ForeignKeys.foreigntableschema + '].[' 
               + ForeignKeys.foreigntablename 
               + '] DROP CONSTRAINT [' 
               + ForeignKeys.foreignkeyname + ']; ' 
               + 'ALTER TABLE [' 
               + ForeignKeys.foreigntableschema + '].[' 
               + ForeignKeys.foreigntablename 
               + '] WITH CHECK ADD CONSTRAINT [' 
               + ForeignKeys.foreignkeyname 
               + '] FOREIGN KEY([' 
               + ForeignKeys.foreigntablecolumn 
               + ']) REFERENCES [' 
               + Schema_name(sys.objects.schema_id) + '].[' 
               + sys.objects.[name] + ']([' 
               + sys.columns.[name] 
               + ']) ON DELETE CASCADE; ' AS query
        INTO #queriesForContraints
        FROM   sys.objects 
               INNER JOIN sys.columns 
                       ON ( sys.columns.[object_id] = sys.objects.[object_id] ) 
               INNER JOIN (SELECT sys.foreign_keys.[name]                      AS 
                                  ForeignKeyName, 
                                  Schema_name(sys.objects.schema_id)           AS 
                      ForeignTableSchema, 
                                  sys.objects.[name]                           AS 
                      ForeignTableName, 
                                  sys.columns.[name]                           AS 
                      ForeignTableColumn, 
                                  sys.foreign_keys.referenced_object_id        AS 
                      referenced_object_id, 
                                  sys.foreign_key_columns.referenced_column_id AS 
                      referenced_column_id 
                           FROM   sys.foreign_keys 
                                  INNER JOIN sys.foreign_key_columns 
                                          ON ( 
                                  sys.foreign_key_columns.constraint_object_id = 
                                  sys.foreign_keys.[object_id] ) 
                                  INNER JOIN sys.objects 
                                          ON ( sys.objects.[object_id] = 
                                             sys.foreign_keys.parent_object_id ) 
                                  INNER JOIN sys.columns 
                                          ON ( sys.columns.[object_id] = 
                                               sys.objects.[object_id] ) 
                                             AND ( sys.columns.column_id = 
        sys.foreign_key_columns.parent_column_id )) 
        ForeignKeys 
        ON ( ForeignKeys.referenced_object_id = sys.objects.[object_id] ) 
        AND ( ForeignKeys.referenced_column_id = sys.columns.column_id ) 
        WHERE  ( sys.objects.[type] = 'U' ) 
               AND ( sys.objects.[name] NOT IN ( 
                    'sysdiagrams' --add more comma separated table names here if required
                ) ) 
    
        DECLARE @queryToRun NVARCHAR(MAX)
    
        SELECT  @queryToRun = STUFF(
                (SELECT query + ''
                 FROM #queriesForContraints
                 FOR XML PATH (''))
             , 1, 0, '')
    
        EXEC sp_executesql @statement = @queryToRun
    
        IF Object_id('tempdb..#queriesForContraints') IS NOT NULL 
          BEGIN 
              DROP TABLE #queriesForContraints
          END 
    

提交回复
热议问题