Monday, August 22, 2011

SQL SERVER : Query to DROP all the Foreign Key Constraints in the database in one go

USE your database name
GO

DECLARE @tblFKConstraints TABLE (RecordID INT IDENTITY(1,1), DropScript VARCHAR(500))
DECLARE
@SQL VARCHAR(500),
@MaxID INT,
@MinID INT

INSERT INTO @tblFKConstraints
SELECT 'ALTER TABLE ' + TABLE_NAME + ' Drop Constraint ' + CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
    WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'



SELECT @MinID = MIN(RecordID), @MaxID = MAX(RecordID) FROM @tblFKConstraints

WHILE @MinID <= @MaxID
BEGIN
SELECT @SQL = DropScript FROM @tblFKConstraints WHERE RecordID = @MinID
EXEC (@SQL)
SET @MinID = @MinID + 1
END

1 comment:

  1. Do you need to play video slots however don't know the place to start? When I began taking part in} in casinos in these days of yore referred to as the Nineteen Eighties, just about all slot machines were three-reel steppers with mechanical reels and handles on the edges. Further necessities for this casino underneath its licences include include correct payouts for winning 온라인카지노 clients and safeguarding players’ private data and knowledge. Thanks to encrypted 128-bit SSL know-how, we will to} safely say that there's positively no Videoslots rip-off afoot. In other words, hackers are wasting their time ought to they try to break into this operator’s system. The playthrough fee within the new participant bonus of 35x can also be|can be} in line with the trade norm.

    ReplyDelete