/* Drop all non-system stored procs */ DECLARE @nameVARCHAR(128) DECLARE @SQLVARCHAR(254) DECLARE @schemaVARCHAR(128)
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P'ANDcategory = 0ORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.procedures WHERE [name] = @name)
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P'ANDcategory = 0ORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.procedures WHERE [name] = @name) END GO
/* Drop all database triggers */ DECLARE @nameVARCHAR(128) DECLARE @SQLVARCHAR(254)
SELECT @name = (SELECT TOP 1 T.name FROM sys.triggers AS T WHERE T.parent_id = 0/* database trigger */ORDERBY [name])
WHILE @nameisnotnull BEGIN SELECT @SQL = 'DROP TRIGGER [' + RTRIM(@name) +'] ON DATABASE' EXEC (@SQL) PRINT 'Dropped TRIGGER: ' + @name
SELECT @name = (SELECT TOP 1 T.name FROM sys.triggers AS T WHERE T.parent_id = 0/* database trigger */AND [name] > @nameORDERBY [name]) END GO
/* Drop all views */ DECLARE @nameVARCHAR(128) DECLARE @SQLVARCHAR(254) DECLARE @schemaVARCHAR(128)
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V'ANDcategory = 0ORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.views WHERE [name] = @name)
WHILE @nameISNOTNULL BEGIN SELECT @SQL = 'DROP VIEW [' + @schema + '].[' + RTRIM(@name) +']' EXEC (@SQL) PRINT 'Dropped View: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V'ANDcategory = 0AND [name] > @nameORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.views WHERE [name] = @name) END GO
/* Drop all Foreign Key constraints */ DECLARE @nameVARCHAR(128) DECLARE @constraintVARCHAR(254) DECLARE @SQLVARCHAR(254) DECLARE @schemaVARCHAR(128)
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY'ORDERBY TABLE_NAME) SELECT @schema = (SELECT TOP 1 CONSTRAINT_SCHEMA FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY'ORDERBY TABLE_NAME)
WHILE @nameisnotnull BEGIN SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY'AND TABLE_NAME = @nameAND CONSTRAINT_SCHEMA = @schemaORDERBY CONSTRAINT_NAME) WHILE @constraintISNOTNULL BEGIN SELECT @SQL = 'ALTER TABLE [' + @schema + '].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']' BEGIN TRY EXEC (@SQL) PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name END TRY BEGIN CATCH PRINT (@SQL) END CATCH SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY'AND CONSTRAINT_NAME <> @constraintAND TABLE_NAME = @nameAND CONSTRAINT_SCHEMA = @schemaORDERBY CONSTRAINT_NAME) END SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY'ORDERBY TABLE_NAME) SELECT @schema = (SELECT TOP 1 CONSTRAINT_SCHEMA FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY'ORDERBY TABLE_NAME) END GO
/* Drop all Primary Key constraints */ DECLARE @nameVARCHAR(128) DECLARE @constraintVARCHAR(254) DECLARE @SQLVARCHAR(254) DECLARE @schemaVARCHAR(128)
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY'ORDERBY TABLE_NAME) SELECT @schema = (SELECT TOP 1 CONSTRAINT_SCHEMA FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY'ORDERBY TABLE_NAME)
WHILE @nameISNOTNULL BEGIN SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY'AND TABLE_NAME = @nameAND CONSTRAINT_SCHEMA = @schemaORDERBY CONSTRAINT_NAME) WHILE @constraintisnotnull BEGIN SELECT @SQL = 'ALTER TABLE [' + @schema + '].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']' --PRINT @SQL EXEC (@SQL) PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY'AND CONSTRAINT_NAME <> @constraintAND TABLE_NAME = @nameAND CONSTRAINT_SCHEMA = @schemaORDERBY CONSTRAINT_NAME) END SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY'ORDERBY TABLE_NAME) SELECT @schema = (SELECT TOP 1 CONSTRAINT_SCHEMA FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY'ORDERBY TABLE_NAME) END GO
/* Drop all tables */ DECLARE @nameVARCHAR(128) DECLARE @SQLVARCHAR(254) DECLARE @schemaVARCHAR(128)
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U'ANDcategory = 0ORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.tables WHERE [name] = @name)
WHILE @nameISNOTNULL 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'ANDcategory = 0ORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.tables WHERE [name] = @name) END GO
/* Drop all functions */ DECLARE @nameVARCHAR(128) DECLARE @SQLVARCHAR(254) DECLARE @schemaVARCHAR(128)
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') ANDcategory = 0ORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.objects WHERE [name] = @name)
WHILE @nameISNOTNULL BEGIN SELECT @SQL = 'DROP FUNCTION [' + @schema + '].[' + RTRIM(@name) +']' EXEC (@SQL) PRINT 'Dropped Function: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') ANDcategory = 0AND [name] > @nameORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.objects WHERE [name] = @name) END GO
/* Drop all types */ DECLARE @nameVARCHAR(128) DECLARE @SQLVARCHAR(254) DECLARE @schemaVARCHAR(128)
SELECT @name = (SELECT TOP 1 [name] FROM sys.types WHERE is_user_defined = 1ORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.types WHERE [name] = @name)
WHILE @nameISNOTNULL BEGIN SELECT @SQL = 'DROP TYPE [' + @schema + '].[' + RTRIM(@name) +']' EXEC (@SQL) PRINT 'Dropped type: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sys.types WHERE is_user_defined = 1AND [name] > @nameORDERBY [name]) SELECT @schema = (SELECT TOP 1 schema_name(schema_id) FROM sys.types WHERE [name] = @name) END GO
You are not alone. One out of every four users who visits a Stack Overflow question copies something within five minutes of hitting the page.