Uncategorized

Drop all foreign keys in database

DECLARE @Stmt NVARCHAR(MAX) = ''

SELECT @Stmt = @Stmt + 'ALTER TABLE [' + SCHEMA_NAME(o.schema_id) + '].[' + o.NAME + '] DROP CONSTRAINT [' + fk.name + '];' 
FROM sys.[foreign_keys] fk 
INNER JOIN sys.[objects] o ON fk.[parent_object_id] = o.[object_id] EXECUTE(@Stmt) GO
DECLARE @Stmt NVARCHAR(MAX)

SET @Stmt = ''

SELECT @Stmt +=
	'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + ' ' +
	'DROP CONSTRAINT ' + QUOTENAME(name) + ';'
FROM sys.foreign_keys;

EXECUTE (@Stmt)
GO