Created
July 26, 2017 19:50
-
-
Save philipmat/57c18c179a0d265c50335f630e7218f9 to your computer and use it in GitHub Desktop.
MSSQL - Drop FK
This file contains 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
SET NOCOUNT ON | |
GO | |
DECLARE Fkeys CURSOR FOR | |
SELECT 'ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']' | |
FROM information_schema.table_constraints | |
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY' | |
OPEN Fkeys | |
DECLARE @Statement NVARCHAR(500) | |
FETCH NEXT FROM Fkeys INTO @statement | |
WHILE (@@FETCH_STATUS = 0) | |
BEGIN | |
PRINT N'RUNNING ' + @statement | |
EXEC sp_executesql @statement | |
PRINT CHAR(13) + CHAR(13) | |
FETCH NEXT FROM Fkeys INTO @statement | |
END | |
CLOSE Fkeys | |
DEALLOCATE Fkeys | |
GO | |
SET NOCOUNT OFF | |
GO | |
select * FROM information_schema.table_constraints | |
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment