Last active
April 13, 2016 11:58
-
-
Save idkw/30a9ff280df6bc3e89227d8c1144de64 to your computer and use it in GitHub Desktop.
Drop all constraints from a SQL Server schema (uncomment -- EXEC to apply changes)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------------------------------------- | |
-- SQL Server : DROP all constraints from SCHEMA_NAME | |
--------------------------------------------- | |
DECLARE @SCHEMA_NAME varchar(100) | |
SET @SCHEMA_NAME = 'my_schema' | |
DECLARE @cmd varchar(4000) | |
DECLARE MY_CURSOR CURSOR | |
LOCAL STATIC READ_ONLY FORWARD_ONLY | |
FOR | |
select 'ALTER TABLE ['+s.name+'].['+t.name+'] DROP CONSTRAINT [' + RTRIM(f.name) +'];' FROM sys.Tables t INNER JOIN sys.foreign_keys f ON f.parent_object_id = t.object_id INNER JOIN sys.schemas s ON s.schema_id = f.schema_id WHERE s.name = @SCHEMA_NAME | |
OPEN MY_CURSOR | |
FETCH NEXT FROM MY_CURSOR INTO @cmd | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
-- EXEC (@cmd) | |
PRINT @cmd | |
FETCH NEXT FROM MY_CURSOR INTO @cmd | |
END | |
CLOSE MY_CURSOR | |
DEALLOCATE MY_CURSOR | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment