Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeremykdev/9896332 to your computer and use it in GitHub Desktop.
Save jeremykdev/9896332 to your computer and use it in GitHub Desktop.
Generate SQL statements to drop all foreign keys in a Microsoft SQL Server database
SELECT 'ALTER TABLE ' + QUOTENAME(S.name) + '.' + QUOTENAME(T.name) + ' DROP CONSTRAINT ' + QUOTENAME(FK.name) + ';'
FROM sys.foreign_keys AS FK
INNER JOIN sys.tables AS T
ON ( Fk.parent_object_id = T.object_id )
INNER JOIN sys.schemas AS S
ON ( T.schema_id = S.schema_id )
ORDER BY S.name, T.name, FK.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment