Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Created January 7, 2013 19:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevewithington/4477879 to your computer and use it in GitHub Desktop.
Save stevewithington/4477879 to your computer and use it in GitHub Desktop.
SQL Server Script to delete all FK in the DB and drop any table that starts with XXX
DECLARE @sqlForeignKeys VARCHAR(MAX)
SELECT @sqlForeignKeys = ISNULL(@sqlForeignKeys,'') +
'ALTER TABLE dbo.[' + OBJECT_NAME(FK.parent_object_id) + '] DROP CONSTRAINT [' + FK.name + '];' + CHAR(10)
FROM SYS.FOREIGN_KEYS FK
//PRINT(@sqlForeignKeys)
EXEC(@sqlForeignKeys)
DECLARE @sqlForeignKeys VARCHAR(MAX)
SELECT @sqlForeignKeys = ISNULL(@sqlForeignKeys,'') +
'DROP TABLE dbo.[' + NAME + '] ;' + CHAR(10)
FROM sysobjects
where name like 'XXXX%'
//PRINT(@sqlForeignKeys)
EXEC(@sqlForeignKeys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment