SQL Server – Remove foreign keys, primary keys, and tables

USE []
GO

/**
* Remove foreign keys, primary keys, and tables.
*/

/* Change to desired schema */
DECLARE @schema VARCHAR(254)
select @schema = ‘dbo’

DECLARE @name VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)

/* Drop all Foreign Key constraints */
SELECT @name = (SELECT TOP 1 TABLENAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME() AND CONSTRAINTTYPE = ‘FOREIGN KEY’
AND TABLESCHEMA=@schema ORDER BY TABLENAME)
WHILE @name is not null
BEGIN
SELECT @constraint = (SELECT TOP 1 CONSTRAINTNAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME()
AND CONSTRAINT
TYPE = ‘FOREIGN KEY’ AND TABLENAME = @name ORDER BY CONSTRAINTNAME)
WHILE @constraint IS NOT NULL
BEGIN
SELECT @SQL = ‘ALTER TABLE [‘ + @schema + ‘].[‘ + RTRIM(@name) +’] DROP CONSTRAINT [‘ + RTRIM(@constraint) +’]’
EXEC (@SQL)
PRINT ‘Dropped FK Constraint: ‘ + @constraint + ‘ on ‘ + @name
SELECT @constraint = (SELECT TOP 1 CONSTRAINTNAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME()
AND CONSTRAINT
TYPE = ‘FOREIGN KEY’ AND CONSTRAINTNAME <> @constraint AND TABLENAME = @name AND TABLESCHEMA=@schema
ORDER BY CONSTRAINT
NAME)
END
SELECT @name = (SELECT TOP 1 TABLENAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME() AND CONSTRAINTTYPE = ‘FOREIGN KEY’
AND TABLESCHEMA=@schema ORDER BY TABLENAME)
END

/* Drop all Primary Key constraints */
SELECT @name = (SELECT TOP 1 TABLENAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME() AND CONSTRAINTTYPE = ‘PRIMARY KEY’
AND TABLESCHEMA=@schema ORDER BY TABLENAME)
WHILE @name IS NOT NULL
BEGIN
SELECT @constraint = (SELECT TOP 1 CONSTRAINTNAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME()
AND CONSTRAINT
TYPE = ‘PRIMARY KEY’ AND TABLENAME = @name ORDER BY CONSTRAINTNAME)
WHILE @constraint is not null
BEGIN
SELECT @SQL = ‘ALTER TABLE [‘ + @schema + ‘].[‘ + RTRIM(@name) +’] DROP CONSTRAINT [‘ + RTRIM(@constraint)+’]’
EXEC (@SQL)
PRINT ‘Dropped PK Constraint: ‘ + @constraint + ‘ on ‘ + @name
SELECT @constraint = (SELECT TOP 1 CONSTRAINTNAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME()
AND CONSTRAINT
TYPE = ‘PRIMARY KEY’ AND CONSTRAINTNAME <> @constraint AND TABLENAME = @name AND TABLESCHEMA=@schema
ORDER BY CONSTRAINT
NAME)
END
SELECT @name = (SELECT TOP 1 TABLENAME FROM INFORMATIONSCHEMA.TABLECONSTRAINTS WHERE constraintcatalog=DBNAME() AND CONSTRAINTTYPE = ‘PRIMARY KEY’
AND TABLESCHEMA=@schema ORDER BY TABLENAME)
END

/* Drop all Tables */
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = ‘U’ AND category = 0 ORDER BY [name])
WHILE @name IS NOT NULL
BEGIN
SELECT @SQL = ‘DROP TABLE [‘ + @schema + ‘].[‘ + RTRIM(@name) +’]’
EXEC (@SQL)
PRINT ‘Dropped Table: ‘ + @name
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = ‘U’ AND category = 0 AND [name] > @name ORDER BY [name])
END
GO