Tuesday, February 25, 2014

Query To Drop All Constraints From Database

DECLARE @STR VARCHAR(MAX)
DECLARE CUR CURSOR FOR

SELECT 'ALTER TABLE ' + '[' + S.[NAME] + '].[' + T.NAME + '] DROP CONSTRAINT ['+ C.NAME + ']'
FROM SYS.OBJECTS C, SYS.OBJECTS T, SYS.SCHEMAS S
WHERE C.TYPE IN ('C', 'F', 'PK', 'UQ', 'D')
 AND C.PARENT_OBJECT_ID=T.OBJECT_ID AND T.TYPE='U' AND T.SCHEMA_ID = S.SCHEMA_ID
ORDER BY C.TYPE

OPEN CUR
FETCH NEXT FROM CUR INTO @STR
WHILE (@@FETCH_STATUS = 0) BEGIN
 PRINT @STR
 EXEC (@STR)
 FETCH NEXT FROM CUR INTO @STR
END

CLOSE CUR
DEALLOCATE CUR

No comments:

Post a Comment