将所有表,存储过程,触发器,约束和所有依赖关系放在一个sql语句中

有没有什么办法可以在SQl Server 2005中清理所有的表,并删除存储过程,触发器,约束和所有的依赖项在一个SQL语句?

申请理由:

我想要一个数据库脚本来清理不在使用的现有数据库,而不是创build新的数据库,特别是当您必须向数据库pipe理员发出请求并等待一段时间才能完成时!

此脚本清除所有视图,SPS,函数PK,FK和表。

/* 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 /* 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 /* 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 /* 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 /* 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 /* 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 

我正在使用Adam Anderson的脚本,更新后支持其他模式中的对象而不是dbo。

 declare @n char(1) set @n = char(10) declare @stmt nvarchar(max) -- procedures select @stmt = isnull( @stmt + @n, '' ) + 'drop procedure [' + schema_name(schema_id) + '].[' + name + ']' from sys.procedures -- check constraints select @stmt = isnull( @stmt + @n, '' ) + 'alter table [' + schema_name(schema_id) + '].[' + object_name( parent_object_id ) + '] drop constraint [' + name + ']' from sys.check_constraints -- functions select @stmt = isnull( @stmt + @n, '' ) + 'drop function [' + schema_name(schema_id) + '].[' + name + ']' from sys.objects where type in ( 'FN', 'IF', 'TF' ) -- views select @stmt = isnull( @stmt + @n, '' ) + 'drop view [' + schema_name(schema_id) + '].[' + name + ']' from sys.views -- foreign keys select @stmt = isnull( @stmt + @n, '' ) + 'alter table [' + schema_name(schema_id) + '].[' + object_name( parent_object_id ) + '] drop constraint [' + name + ']' from sys.foreign_keys -- tables select @stmt = isnull( @stmt + @n, '' ) + 'drop table [' + schema_name(schema_id) + '].[' + name + ']' from sys.tables -- user defined types select @stmt = isnull( @stmt + @n, '' ) + 'drop type [' + schema_name(schema_id) + '].[' + name + ']' from sys.types where is_user_defined = 1 exec sp_executesql @stmt 

资料来源: http : //blog.falafel.com/Blogs/AdamAnderson/09-01-06/T-SQL_Drop_All_Objects_in_a_SQL_Server_Database.aspx

最好的办法是“ 生成拖放脚本

select数据库 – > 右键单击 – > 任务 – > 生成脚本 – 将打开用于生成脚本的向导

在select“设置脚本”选项中的对象后,单击“ 高级button”

  • – >设置选项'创build脚本'为true(想创build)
  • – >将选项“ 脚本放置 ”设置为true(想要放下)
  • – >selectcheckbox来select希望创build脚本的对象
  • – >select写入脚本(File,New window,Clipboard)
  • 它包含了依赖对象(默认情况下会首先删除约束)

    执行脚本

这样我们可以自定义我们的脚本。

删除所有表格:

 exec sp_MSforeachtable 'DROP TABLE ?' 

当然,这将会删除所有约束,触发器等,除了存储过程之外的所有其他事情。

对于存储过程,恐怕需要另外一个存储在master存储过程。

我会做两个语句: DROP DATABASE ???

然后CREATE DATABASE ???

备份一个完全空的数据库。 而不是删除所有的对象,只是恢复备份。

这是我曾经试过的:

 SELECT 'DROP TABLE [' + SCHEMA_NAME(schema_id) + '].[' + name + ']' FROM sys.tables 

它将输出什么,只要复制所有,并粘贴在新的查询,然后按执行。 这将删除所有表格。

我在这里尝试了一些脚本,但他们不适合我,因为我在模式中有我的表格。 所以我把以下几点放在一起。 注意这个脚本需要一个模式列表,然后按顺序删除。 您需要确保您的模式中有完整的sorting。 如果有任何循环依赖,那么它将失败。

 PRINT 'Dropping whole database' GO ------------------------------------------ -- Drop constraints ------------------------------------------ DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR SET @Cursor = CURSOR FAST_FORWARD FOR SELECT DISTINCT sql = 'ALTER TABLE ['+tc2.CONSTRAINT_SCHEMA+'].[' + tc2.TABLE_NAME + '] DROP [' + rc1.CONSTRAINT_NAME + ']' FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME OPEN @Cursor FETCH NEXT FROM @Cursor INTO @Sql WHILE (@@FETCH_STATUS = 0) BEGIN PRINT @Sql Exec (@Sql) FETCH NEXT FROM @Cursor INTO @Sql END CLOSE @Cursor DEALLOCATE @Cursor GO ------------------------------------------ -- Drop views ------------------------------------------ DECLARE @sql VARCHAR(MAX) = '' , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ; SELECT @sql = @sql + 'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(v.name) +';' + @crlf FROM sys.views v PRINT @sql; EXEC(@sql); GO ------------------------------------------ -- Drop procs ------------------------------------------ PRINT 'Dropping all procs ...' GO DECLARE @sql VARCHAR(MAX) = '' , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ; SELECT @sql = @sql + 'DROP PROC ' + QUOTENAME(SCHEMA_NAME(p.schema_id)) + '.' + QUOTENAME(p.name) +';' + @crlf FROM [sys].[procedures] p PRINT @sql; EXEC(@sql); GO ------------------------------------------ -- Drop tables ------------------------------------------ PRINT 'Dropping all tables ...' GO EXEC sp_MSForEachTable 'DROP TABLE ?' GO ------------------------------------------ -- Drop sequences ------------------------------------------ PRINT 'Dropping all sequences ...' GO DECLARE @DropSeqSql varchar(1024) DECLARE DropSeqCursor CURSOR FOR SELECT DISTINCT 'DROP SEQUENCE ' + s.SEQUENCE_SCHEMA + '.' + s.SEQUENCE_NAME FROM INFORMATION_SCHEMA.SEQUENCES s OPEN DropSeqCursor FETCH NEXT FROM DropSeqCursor INTO @DropSeqSql WHILE ( @@FETCH_STATUS <> -1 ) BEGIN PRINT @DropSeqSql EXECUTE( @DropSeqSql ) FETCH NEXT FROM DropSeqCursor INTO @DropSeqSql END CLOSE DropSeqCursor DEALLOCATE DropSeqCursor GO ------------------------------------------ -- Drop Schemas ------------------------------------------ DECLARE @schemas as varchar(1000) = 'StaticData,Ird,DataImport,Collateral,Report,Cds,CommonTrade,MarketData,TypeCode' DECLARE @schemasXml as xml = cast(('<schema>'+replace(@schemas,',' ,'</schema><schema>')+'</schema>') as xml) DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR SET @Cursor = CURSOR FAST_FORWARD FOR SELECT sql = 'DROP SCHEMA ['+schemaName+']' FROM (SELECT CAST(T.schemaName.query('text()') as VARCHAR(200)) as schemaName FROM @schemasXml.nodes('/schema') T(schemaName)) as X JOIN information_schema.schemata S on S.schema_name = X.schemaName OPEN @Cursor FETCH NEXT FROM @Cursor INTO @Sql WHILE (@@FETCH_STATUS = 0) BEGIN PRINT @Sql Exec (@Sql) FETCH NEXT FROM @Cursor INTO @Sql END CLOSE @Cursor DEALLOCATE @Cursor GO 

我今晚意外地在主数据库上运行了一个db init脚本。 无论如何,我很快跑到这个线程。 我使用:exec sp_MSforeachtable“DROP TABLE?” 答案,但不得不多次执行它,直到它没有错误(依赖关系。)之后,我偶然发现了一些其他线程,并拼凑在一起放弃所有的存储过程和函数。

 DECLARE mycur CURSOR FOR select O.type_desc,schema_id,O.name from sys.objects O LEFT OUTER JOIN sys.extended_properties E ON O.object_id = E.major_id WHERE O.name IS NOT NULL AND ISNULL(O.is_ms_shipped, 0) = 0 AND ISNULL(E.name, '') <> 'microsoft_database_tools_support' AND ( O.type_desc = 'SQL_STORED_PROCEDURE' OR O.type_desc = 'SQL_SCALAR_FUNCTION' ) ORDER BY O.type_desc,O.name; OPEN mycur; DECLARE @schema_id int; DECLARE @fname varchar(256); DECLARE @sname varchar(256); DECLARE @ftype varchar(256); FETCH NEXT FROM mycur INTO @ftype, @schema_id, @fname; WHILE @@FETCH_STATUS = 0 BEGIN SET @sname = SCHEMA_NAME( @schema_id ); IF @ftype = 'SQL_STORED_PROCEDURE' EXEC( 'DROP PROCEDURE "' + @sname + '"."' + @fname + '"' ); IF @ftype = 'SQL_SCALAR_FUNCTION' EXEC( 'DROP FUNCTION "' + @sname + '"."' + @fname + '"' ); FETCH NEXT FROM mycur INTO @ftype, @schema_id, @fname; END CLOSE mycur DEALLOCATE mycur GO 

尝试这个

 Select 'ALTER TABLE ' + Table_Name +' drop constraint ' + Constraint_Name from Information_Schema.CONSTRAINT_TABLE_USAGE Select 'drop Procedure ' + specific_name from Information_Schema.Routines where specific_name not like 'sp%' AND specific_name not like 'fn_%' Select 'drop View ' + table_name from Information_Schema.tables where Table_Type = 'VIEW' SELECT 'DROP TRIGGER ' + name FROM sysobjects WHERE type = 'tr' Select 'drop table ' + table_name from Information_Schema.tables where Table_Type = 'BASE TABLE' 

除了@伊凡的答案,types都需要包括在内

  /* Drop all Types */ DECLARE @name VARCHAR(128) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 [name] FROM sys.types where is_user_defined = 1 ORDER BY [name]) WHILE @name IS NOT NULL BEGIN SELECT @SQL = 'DROP TYPE [dbo].[' + RTRIM(@name) +']' EXEC (@SQL) PRINT 'Dropped Type: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sys.types where is_user_defined = 1 AND [name] > @name ORDER BY [name]) END GO 

没有一个可以用来实现这个目标的声明。

你当然可以创build一个你可以用来执行这些不同的pipe理任务的stored procedure

然后你可以使用这个单一的语句执行程序。

 Exec sp_CleanDatabases @DatabaseName='DBname' 

对我来说似乎是一个相当危险的特征。 如果你实现这样的事情,我会确保以一种你不能在每次事故中运行的方式来妥善保护它。

正如之前所build议的,你可以自己做一些存储过程。 在SQL Server 2005中,你可以看看这个系统表来确定并find你想要删除的对象。

 select * from sys.objects 

这里我发现了新的查询来删除所有的sp,函数和触发器

 declare @procName varchar(500) declare cur cursor for select [name] from sys.objects where type = 'p' open cur fetch next from cur into @procName while @@fetch_status = 0 begin exec('drop procedure ' + @procName) fetch next from cur into @procName end close cur deallocate cur 

要添加到Ivan的答案,我也有需要删除所有用户定义的types,所以我已经添加到脚本:

 /* Drop all user-defined types */ DECLARE @name VARCHAR(128) DECLARE @SQL VARCHAR(254) SELECT @name = (select TOP 1 [name] from sys.types where is_user_defined = 1) WHILE @name IS NOT NULL BEGIN SELECT @SQL = 'DROP TYPE [dbo].[' + RTRIM(@name) +']' EXEC (@SQL) PRINT 'Dropped Type: ' + @name SELECT @name = (select TOP 1 [name] from sys.types where is_user_defined = 1) END GO 
 DECLARE @name VARCHAR(255) DECLARE @type VARCHAR(10) DECLARE @prefix VARCHAR(255) DECLARE @sql VARCHAR(255) DECLARE curs CURSOR FOR SELECT [name], xtype FROM sysobjects WHERE xtype IN ('U', 'P', 'FN', 'IF', 'TF', 'V', 'TR') -- Configuration point 1 ORDER BY name OPEN curs FETCH NEXT FROM curs INTO @name, @type WHILE @@FETCH_STATUS = 0 BEGIN -- Configuration point 2 SET @prefix = CASE @type WHEN 'U' THEN 'DROP TABLE' WHEN 'P' THEN 'DROP PROCEDURE' WHEN 'FN' THEN 'DROP FUNCTION' WHEN 'IF' THEN 'DROP FUNCTION' WHEN 'TF' THEN 'DROP FUNCTION' WHEN 'V' THEN 'DROP VIEW' WHEN 'TR' THEN 'DROP TRIGGER' END SET @sql = @prefix + ' ' + @name PRINT @sql EXEC(@sql) FETCH NEXT FROM curs INTO @name, @type END CLOSE curs DEALLOCATE curs 

尝试这个….

 USE DATABASE GO DECLARE @tname VARCHAR(150) DECLARE @strsql VARCHAR(300) SELECT @tname = (SELECT TOP 1 [name] FROM sys.objects WHERE [type] = 'U' and [name] like N'TableName%' ORDER BY [name]) WHILE @tname IS NOT NULL BEGIN SELECT @strsql = 'DROP TABLE [dbo].[' + RTRIM(@tname) +']' EXEC (@strsql) PRINT 'Dropped Table : ' + @tname SELECT @tname = (SELECT TOP 1 [name] FROM sys.objects WHERE [type] = 'U' AND [name] like N'TableName%' AND [name] > @tname ORDER BY [name]) END