Today, I am going to explain some quick solutions for SQL queries such as how to delete all the types of Stored Procedure, Views, functions PKs, FKs and tables.
Software developers always try to improve the performance of the application by improving design, coding and database development. For database development, I will provide some quick solutions.
How To Drop All Stored Procedures In MSSQL Database
- /* Drop all non-system stored procs */
- DECLARE @name VARCHAR(128)
- DECLARE @SQL VARCHAR(254)
-
- SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])
-
- WHILE @name is not null
- BEGIN
- SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'
- EXEC (@SQL)
- PRINT 'Dropped Procedure: ' + @name
- SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name])
- END
- GO
How To Drop All Views In MSSQL Database
- /* Drop all views */
- DECLARE @name VARCHAR(128)
- DECLARE @SQL VARCHAR(254)
-
- SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])
-
- WHILE @name IS NOT NULL
- BEGIN
- SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'
- EXEC (@SQL)
- PRINT 'Dropped View: ' + @name
- SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name])
- END
- GO
How To Drop All Tables From MSSQL Database
Firstly, use Information_Schema.Tables to view for getting the list of tables (If you want).
- DECLARE @sql NVARCHAR(max)=''
-
- SELECT @sql += ' Drop table '+TABLE_SCHEMA+'.'+ TABLE_NAME
- FROM INFORMATION_SCHEMA.TABLES
- WHERE TABLE_TYPE = 'BASE TABLE'
-
- Exec Sp_executesql @sql
Secondly, generate drop scripts in select statement and drop it using Dynamic SQL.
- SELECT ' Drop table ' + s.NAME + '.' + t.NAME
- FROM sys.tables t
- JOIN sys.schemas s
- ON t.[schema_id] = s.[schema_id]
- WHERE t.type = 'U'
-
- Exec sp_executesql @sql
Another way to drop all table from the database is:
- /* Drop all tables */
- DECLARE @name VARCHAR(128)
- DECLARE @SQL VARCHAR(254)
-
- 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 [dbo].[' + 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
How To Drop All Functions From MSSQL Database
- /* Drop all functions */
- DECLARE @name VARCHAR(128)
- DECLARE @SQL VARCHAR(254)
-
- SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])
-
- WHILE @name IS NOT NULL
- BEGIN
- SELECT @SQL = 'DROP FUNCTION [dbo].[' + 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') AND category = 0 AND [name] > @name ORDER BY [name])
- END
- GO
How To Drop All Foreign Keys From MSSQL Database
- /* Drop all Foreign Key constraints */
- DECLARE @name VARCHAR(128)
- DECLARE @constraint VARCHAR(254)
- DECLARE @SQL VARCHAR(254)
-
- SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)
-
- WHILE @name is not null
- 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 = @name ORDER BY CONSTRAINT_NAME)
- WHILE @constraint IS NOT NULL
- BEGIN
- SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']'
- EXEC (@SQL)
- PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name
- 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 <> @constraint AND TABLE_NAME = @name ORDER BY 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' ORDER BY TABLE_NAME)
- END
- GO
Another way to drop Foreign Key with (If exists)
- IF EXISTS (SELECT *
- FROM sys.foreign_keys
- WHERE object_id = OBJECT_ID(N'dbo.FK_TableName_TableName2')
- AND parent_object_id = OBJECT_ID(N'dbo.TableName')
- )
- ALTER TABLE [dbo.TableName] DROP CONSTRAINT [FK_TableName_TableName2]
How to Drop All Primary Keys From MSSQL Database
- /* Drop all Primary Key constraints */
- DECLARE @name VARCHAR(128)
- DECLARE @constraint VARCHAR(254)
- DECLARE @SQL VARCHAR(254)
-
- SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)
-
- WHILE @name IS NOT NULL
- 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 = @name ORDER BY CONSTRAINT_NAME)
- WHILE @constraint is not null
- BEGIN
- SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']'
- 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 <> @constraint AND TABLE_NAME = @name ORDER BY 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' ORDER BY TABLE_NAME)
- END
- GO