首页 > 代码库 > Delete all tables on sqlserver
Delete all tables on sqlserver
/* 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 nullBEGIN 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])ENDGO/* 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 NULLBEGIN 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])ENDGO/* 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 NULLBEGIN 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])ENDGO/* 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 nullBEGIN 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) ENDSELECT @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)ENDGO/* 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 NULLBEGIN 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) ENDSELECT @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)ENDGO/* 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 NULLBEGIN 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])ENDGO
From : http://stackoverflow.com/questions/536350/drop-all-the-tables-stored-procedures-triggers-constraints-and-all-the-depend
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。