SQL Server 2008删除特殊模式下的所有表
你好,我想知道是可以删除数据库中的所有表是什么创build自定义架构下例如DBO1 …与一个SQL查询或特殊脚本。
谢谢
这将为您生成所有的DROP TABLE语句,并将SQL语句打印出来。 然后,您可以在复制和执行之前validation它的内容。 只要确保你100%肯定…也许先备份:)
DECLARE @SqlStatement NVARCHAR(MAX) SELECT @SqlStatement = COALESCE(@SqlStatement, N'') + N'DROP TABLE [DBO1].' + QUOTENAME(TABLE_NAME) + N';' + CHAR(13) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DBO1' and TABLE_TYPE = 'BASE TABLE' PRINT @SqlStatement
我知道有点旧的线程,但我正在寻找这样的东西,发现原来的答案非常有帮助。 也就是说,该脚本还将尝试删除该架构中可能存在的视图,并向您发出错误消息,因为您最终会尝试通过发出DROP TABLE语句来删除视图。
我写了这个,因为我需要从给定的模式中删除所有表,视图,过程和函数。 也许不是最优雅的方式来完成这个,但它为我工作,我想我会分享。
DECLARE @Sql VARCHAR(MAX) , @Schema varchar(20) SET @Schema = 'Integration' --put your schema name between these quotes --tables SELECT @Sql = COALESCE(@Sql,'') + 'DROP TABLE %SCHEMA%.' + QUOTENAME(TABLE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @Schema AND TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME --views SELECT @Sql = COALESCE(@Sql,'') + 'DROP VIEW %SCHEMA%.' + QUOTENAME(TABLE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @Schema AND TABLE_TYPE = 'VIEW' ORDER BY TABLE_NAME --Procedures SELECT @Sql = COALESCE(@Sql,'') + 'DROP PROCEDURE %SCHEMA%.' + QUOTENAME(ROUTINE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = @Schema AND ROUTINE_TYPE = 'PROCEDURE' ORDER BY ROUTINE_NAME --Functions SELECT @Sql = COALESCE(@Sql,'') + 'DROP FUNCTION %SCHEMA%.' + QUOTENAME(ROUTINE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = @Schema AND ROUTINE_TYPE = 'FUNCTION' ORDER BY ROUTINE_NAME SELECT @Sql = COALESCE(REPLACE(@Sql,'%SCHEMA%',@Schema), '') PRINT @Sql
build立在@ Kevo的答案,我添加了以下删除表之前删除所有外键约束。 我只testing了SQL2008 R2
select @Sql = COALESCE(@Sql,'') + 'ALTER TABLE %SCHEMA%.' + t.name + ' drop constraint ' + OBJECT_NAME(d.constraint_object_id) + ';' + CHAR(13) from sys.tables t join sys.foreign_key_columns d on d.parent_object_id = t.object_id inner join sys.schemas s on t.schema_id = s.schema_id where s.name = @Schema ORDER BY t.name;
基于其他答案,这里是一个存储过程spDropSchema
,它删除模式中的所有对象和模式本身。
请注意,该过程也尝试删除序列对象,因此它只能在SQL Server 2012及更高版本上运行。
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'spDropSchema') BEGIN DROP PROCEDURE spDropSchema END GO CREATE PROCEDURE spDropSchema(@Schema nvarchar(200)) AS DECLARE @Sql NVARCHAR(MAX) = ''; --constraints SELECT @Sql = @Sql + 'ALTER TABLE '+ QUOTENAME(@Schema) + '.' + QUOTENAME(t.name) + ' DROP CONSTRAINT ' + QUOTENAME(f.name) + ';' + CHAR(13) FROM sys.tables t inner join sys.foreign_keys f on f.parent_object_id = t.object_id inner join sys.schemas s on t.schema_id = s.schema_id WHERE s.name = @Schema ORDER BY t.name; --tables SELECT @Sql = @Sql + 'DROP TABLE '+ QUOTENAME(@Schema) +'.' + QUOTENAME(TABLE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @Schema AND TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME --views SELECT @Sql = @Sql + 'DROP VIEW '+ QUOTENAME(@Schema) +'.' + QUOTENAME(TABLE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @Schema AND TABLE_TYPE = 'VIEW' ORDER BY TABLE_NAME --procedures SELECT @Sql = @Sql + 'DROP PROCEDURE '+ QUOTENAME(@Schema) +'.' + QUOTENAME(ROUTINE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = @Schema AND ROUTINE_TYPE = 'PROCEDURE' ORDER BY ROUTINE_NAME --functions SELECT @Sql = @Sql + 'DROP FUNCTION '+ QUOTENAME(@Schema) +'.' + QUOTENAME(ROUTINE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = @Schema AND ROUTINE_TYPE = 'FUNCTION' ORDER BY ROUTINE_NAME --sequences SELECT @Sql = @Sql + 'DROP SEQUENCE '+ QUOTENAME(@Schema) +'.' + QUOTENAME(SEQUENCE_NAME) + ';' + CHAR(13) FROM INFORMATION_SCHEMA.SEQUENCES WHERE SEQUENCE_SCHEMA = @Schema ORDER BY SEQUENCE_NAME SELECT @Sql = @Sql + 'DROP SCHEMA '+ QUOTENAME(@Schema) + ';' + CHAR(13) EXECUTE sp_executesql @Sql GO
同样基于@Kevo的答案,我添加了下面的while循环来解决我在使用TSQL Print语句时遇到的问题。 消息string最长可达8000个字符。 如果大于8,000,则打印语句将截断任何剩余的字符。
DECLARE @SqlLength int , @SqlPosition int = 1 , @printMaxLength int = 8000 SET @SqlLength = LEN(@Sql) WHILE (@SqlLength) > @printMaxLength BEGIN PRINT SUBSTRING(@Sql, @SqlPosition, @printMaxLength) SET @SqlLength = @SqlLength - @printMaxLength SET @SqlPosition = @SqlPosition + @printMaxLength END IF (@SqlLength) < @printMaxLength AND (@SqlLength) > 0 BEGIN PRINT SUBSTRING(@Sql, @SqlPosition, @printMaxLength) END
我将来自@ raider33和@Kevo的答案合并为一个直接执行的解决scheme。
DECLARE @SqlStatement NVARCHAR(MAX) DECLARE @schema varchar(30) = 'SCHEMA_NAME'; select @SqlStatement = COALESCE(@SqlStatement,'') + 'ALTER TABLE '+@schema+'.' + t.name + ' drop constraint ' + OBJECT_NAME(d.constraint_object_id) + ';' + CHAR(13) + CHAR(10) from sys.tables t join sys.foreign_key_columns d on d.parent_object_id = t.object_id inner join sys.schemas s on t.schema_id = s.schema_id where s.name = @schema ORDER BY t.name; SELECT @SqlStatement += COALESCE(@SqlStatement, '') + 'DROP TABLE ' + @schema +'.'+ QUOTENAME(TABLE_NAME) + ';' + CHAR(13) + CHAR(10) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema EXECUTE sp_executesql @SqlStatement
基于克里斯·LB的答案 ,我补充说
GROUP BY d.constraint_object_id, t.name
因为我在查询中看到重复的约束删除。 constraint_object_id
是FK约束ID,如https://msdn.microsoft.com/en-us/library/ms186306.aspx所述;
DECLARE @SqlStatement NVARCHAR(MAX), @Schema NVARCHAR(20) SET @Schema = 'aa' SELECT @SqlStatement = COALESCE(@SqlStatement,'') + 'ALTER TABLE '+@Schema+'.' + t.name + ' DROP CONSTRAINT ' + OBJECT_NAME(d.constraint_object_id) + ';' + CHAR(13) + CHAR(10) FROM sys.tables t JOIN sys.foreign_key_columns d on t.object_id = d.parent_object_id INNER JOIN sys.schemas s on t.schema_id = s.schema_id WHERE s.name = @Schema GROUP BY d.constraint_object_id, t.name ORDER BY t.name;
为了防止他人帮忙,我把这个存储过程添加到master数据库中,以便在任何db / schema上方便地使用它。
它可以这样调用:
EXEC master.dbo.dropTablesInSchema 'my_db', 'dbo
存储过程创build脚本:
CREATE PROC [master].[dbo].[dropTablesInSchema] @db nvarchar(max), @schema nvarchar(max) AS BEGIN DECLARE @Tables TABLE (name nvarchar(max)) INSERT INTO @Tables EXEC ('SELECT TABLE_NAME FROM [' + @db + '].INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ''' + @schema + ''' and TABLE_TYPE =''BASE TABLE''') DECLARE @SqlStatement NVARCHAR(MAX) SELECT @SqlStatement = COALESCE(@SqlStatement, N'') + N'DROP TABLE [' + @db + '].[' + @schema + '].' + QUOTENAME(NAME) + N';' + CHAR(13) FROM @Tables EXEC(@SqlStatement) END
这将生成所有的DROP TABLE和DROP VIEW并且存在检查。
DECLARE @SqlStatement NVARCHAR(MAX) SELECT @SqlStatement = COALESCE(@SqlStatement, N'') + N'IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'''+'['+TABLE_SCHEMA+'].' + QUOTENAME(TABLE_NAME) +''' )' + CHAR(13)+ ' DROP '+ TABLE_TYPE +' ['+TABLE_SCHEMA+'].' + QUOTENAME(TABLE_NAME) + N';' + CHAR(13) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA in ('SCHEMA1','SCHEMA2','SCHEMA13' ) ORDER BY TABLE_SCHEMA PRINT REPLACE(@SqlStatement,'DROP BASE TABLE ','DROP TABLE ') GO