Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql statement
有没有什么方法可以通过删除所有表并删除一个SQL语句中的存储过程,触发器,约束和所有依赖项来清理SQl Server 2005中的数据库?
请求的理由:
我希望有一个数据库脚本来清理未使用的现有数据库,而不是创建新数据库,特别是当您必须向数据库管理员发出请求并等待一段时间才能完成时!
此脚本清除所有视图,SPS,函数PK,FK和表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | /* 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之外的其他模式中的对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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
最好的办法是"为Drop生成脚本"
选择数据库 - >右键单击 - >任务 - >生成脚本 - 将打开向导以生成脚本
在set Scripting选项中选择对象后,单击Advanced Button
- - >将选项'Script to create'设置为true(想要创建)
- - >将选项'Script to Drop'设置为true(想要删除)
- - >选中复选框以选择希望创建脚本的对象
- - >选择编写脚本的选项(文件,新窗口,剪贴板)
-
它默认包含依赖对象。(并且首先会删除约束)
执行脚本
这样我们就可以自定义脚本了。
删除所有表格:
1 | EXEC sp_MSforeachtable 'DROP TABLE ?' |
当然,这将删除除存储过程之外的所有约束,触发器等。
对于存储过程,我担心您将需要存储在
我会用两个语句来做:
然后
我在这里尝试了一些脚本,但它们对我不起作用,因为我在模式中有表格。所以我把以下内容放在一起。请注意,此脚本采用模式列表,然后按顺序删除。您需要确保在模式中有完整的排序。如果存在任何循环依赖关系,那么它将失败。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 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 |
这是我尝试过的:
1 | SELECT 'DROP TABLE [' + SCHEMA_NAME(schema_id) + '].[' + name + ']' FROM sys.tables |
它将打印什么输出,只需复制全部并粘贴新查询并按下执行。
这将删除所有表。
备份一个完全空的数据库。不要删除所有对象,只需恢复备份。
今晚我不小心对我的主数据库运行了一个db init脚本。无论如何,我很快就遇到了这个帖子。我用过:exec sp_MSforeachtable'DROP TABLE?'回答,但不得不多次执行它,直到它没有错误(依赖关系。)之后,我偶然发现了一些其他线程并将它拼凑在一起以删除所有存储过程和函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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 |
除了@ Ivan的回答,还需要包含所有类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /* 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 |
试试这个
1 2 3 4 5 6 7 8 9 | 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' |
对我来说似乎是一个相当危险的特征。如果你实现这样的东西,我会确保以一种你无法在每次事故中运行它的方式正确保护它。
如前所述,您可以自己制作某种存储过程。
在SQL Server 2005中,您可以查看此系统表以确定并找到要删除的对象。
1 | SELECT * FROM sys.objects |
没有一个声明可用于实现这一目标。
您当然可以创建一个
然后,您可以使用此单个语句执行该过程。
1 | EXEC sp_CleanDatabases @DatabaseName='DBname' |
在这里,我找到了删除所有sp,函数和触发器的新查询
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 |
尝试使用sql2012或更高版本,
这将有助于删除所选模式的所有对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | DECLARE @MySchemaName VARCHAR(50)='dbo', @SQL VARCHAR(MAX)=''; DECLARE @SchemaName VARCHAR(255), @ObjectName VARCHAR(255), @ObjectType VARCHAR(255), @ObjectDesc VARCHAR(255), @Category INT; DECLARE cur CURSOR FOR SELECT (s.name)SchemaName, (o.name)ObjectName, (o.type)ObjectType,(o.type_desc)ObjectDesc,(so.category)Category FROM sys.objects o INNER JOIN sys.schemas s ON o.schema_id = s.schema_id INNER JOIN sysobjects so ON so.name=o.name WHERE s.name = @MySchemaName AND so.category=0 AND o.type IN ('P','PC','U','V','FN','IF','TF','FS','FT','PK','TT') OPEN cur FETCH NEXT FROM cur INTO @SchemaName,@ObjectName,@ObjectType,@ObjectDesc,@Category SET @SQL=''; WHILE @@FETCH_STATUS = 0 BEGIN IF @ObjectType IN('FN', 'IF', 'TF', 'FS', 'FT') SET @SQL=@SQL+'Drop Function '+@MySchemaName+'.'+@ObjectName+CHAR(13) IF @ObjectType IN('V') SET @SQL=@SQL+'Drop View '+@MySchemaName+'.'+@ObjectName+CHAR(13) IF @ObjectType IN('P') SET @SQL=@SQL+'Drop Procedure '+@MySchemaName+'.'+@ObjectName+CHAR(13) IF @ObjectType IN('U') SET @SQL=@SQL+'Drop Table '+@MySchemaName+'.'+@ObjectName+CHAR(13) --PRINT @ObjectName + ' | ' + @ObjectType FETCH NEXT FROM cur INTO @SchemaName,@ObjectName,@ObjectType,@ObjectDesc,@Category END CLOSE cur; DEALLOCATE cur; SET @SQL=@SQL+CASE WHEN LEN(@SQL)>0 THEN 'Drop Schema '+@MySchemaName+CHAR(13) ELSE '' END PRINT @SQL EXECUTE (@SQL) |
要添加到Ivan的答案,我还需要删除所有用户定义的类型,所以我已将其添加到脚本中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /* 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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 |
您必须先禁用所有
1 2 3 | EXEC sp_MSforeachtable @command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL" EXEC sp_MSforeachtable @command1="ALTER TABLE ? DISABLE TRIGGER ALL" |
之后,您可以生成用于删除对象的脚本
1 2 3 | SELECT 'Drop Table '+name FROM sys.tables WHERE TYPE='U'; SELECT 'Drop Procedure '+name FROM sys.procedures WHERE TYPE='P'; |
执行生成的语句。
要删除oracle中的所有对象:
1)动态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | DECLARE CURSOR IX IS SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE ='TABLE' AND OWNER='SCHEMA_NAME'; CURSOR IY IS SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE IN ('SEQUENCE', 'PROCEDURE', 'PACKAGE', 'FUNCTION', 'VIEW') AND OWNER='SCHEMA_NAME'; CURSOR IZ IS SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE IN ('TYPE') AND OWNER='SCHEMA_NAME'; BEGIN FOR X IN IX LOOP EXECUTE IMMEDIATE('DROP '||X.OBJECT_TYPE||' '||X.OBJECT_NAME|| ' CASCADE CONSTRAINT'); END LOOP; FOR Y IN IY LOOP EXECUTE IMMEDIATE('DROP '||Y.OBJECT_TYPE||' '||Y.OBJECT_NAME); END LOOP; FOR Z IN IZ LOOP EXECUTE IMMEDIATE('DROP '||Z.OBJECT_TYPE||' '||Z.OBJECT_NAME||' FORCE '); END LOOP; END; / |
2)静态
1 2 3 4 5 6 7 8 9 10 11 12 | SELECT 'DROP TABLE"' || TABLE_NAME || '" CASCADE CONSTRAINTS;' FROM user_tables UNION ALL SELECT 'drop '||object_type||' '|| object_name || ';' FROM user_objects WHERE object_type IN ('VIEW','PACKAGE','SEQUENCE', 'PROCEDURE', 'FUNCTION') UNION ALL SELECT 'drop ' ||object_type ||' ' || object_name || ' force;' FROM user_objects WHERE object_type IN ('TYPE'); |
试试这个....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |