:::: MENU ::::

Drop ALL foreign keys in all tables in a database

In SSMS, open a new Query windows, choose the right database, VERY IMPORTANT!!!!

Paste in this script and run it, it will immediately find all FK and drop them.

 

-- begin script
DECLARE @cmd nvarchar(1000)
DECLARE @fk_table_name nvarchar(1000)
DECLARE @fk_name nvarchar(1000)

DECLARE cursor_fkeys CURSOR FOR
   SELECT  OBJECT_NAME(fk.parent_object_id) AS fk_table_name,
            fk.name as fk_name
   FROM    sys.foreign_keys fk  JOIN  
           sys.tables tbl ON tbl.OBJECT_ID = fk.referenced_object_id 

OPEN cursor_fkeys
FETCH NEXT FROM cursor_fkeys 
    INTO @fk_table_name, @fk_name

WHILE @@FETCH_STATUS=0
BEGIN
    -- build alter table statement
    SET @cmd = 'ALTER TABLE [' + @fk_table_name + '] DROP CONSTRAINT [' + @fk_name + ']' 
    -- execute it
    exec dbo.sp_executesql @cmd

    FETCH NEXT FROM cursor_fkeys 
    INTO @fk_table_name, @fk_name
END 
CLOSE cursor_fkeys
DEALLOCATE cursor_fkeys

//R

So, what do you think ?