简体   繁体   English

删除SQL Server数据库中属于不同模式的所有对象?

[英]Drop all objects in SQL Server database that belong to different schemas?

Is there a way to drop all objects in a db, with the objects belonging to two different schemas? 有没有办法删除数据库中的所有对象,对象属于两个不同的模式?

I had been previously working with one schema, so I query all objects using: 我以前使用过一个模式,所以我使用以下方法查询所有对象:

Select * From sysobjects Where type=...

then dropped everything I using 然后放弃我使用的一切

Drop Table ...

Now that I have introduced another schema, every time I try to drop it says something about I don't have permission or the object does not exist. 现在我已经介绍了另一个模式,每次我尝试删除它都会说一些关于我没有权限或者该对象不存在的事情。 BUT, if I prefix the object with the [schema.object] it works. 但是,如果我使用[schema.object]为对象添加前缀,则它可以正常工作。 I don't know how to automate this, cause I don't know what objects, or which of the two schemas the object will belong to. 我不知道如何自动执行此操作,因为我不知道对象将属于哪个对象或哪两个模式。 Anyone know how to drop all objects inside a db, regardless of which schema it belongs to? 任何人都知道如何删除数据库中的所有对象,无论它属于哪个模式?

(The user used is owner of both schemas, the objects in the DB were created by said user, as well as the user who is removing the objects - which works if the prefix I used IE. Drop Table Schema1.blah ) (使用的用户是两个模式的所有者,数据库中的对象是由所述用户创建的,以及正在删除对象的用户 - 如果我使用IE的前缀,则可以工作Drop Table Schema1.blah

Use sys.objects in combination with OBJECT_SCHEMA_NAME to build your DROP TABLE statements, review, then copy/paste to execute: sys.objectsOBJECT_SCHEMA_NAME结合使用来构建DROP TABLE语句,查看,然后复制/粘贴以执行:

SELECT 'DROP TABLE ' +
       QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + '.' +
       QUOTENAME(name) + ';'
  FROM sys.objects
 WHERE type_desc = 'USER_TABLE';

Or use sys.tables to avoid need of the type_desc filter: 或者使用sys.tables来避免使用type_desc过滤器:

SELECT 'DROP TABLE ' +
       QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + '.' +
       QUOTENAME(name) + ';'
  FROM sys.tables;

SQL Fiddle SQL小提琴

Neither of the other questions seem to have tried to address the all objects part of the question. 似乎没有其他问题试图解决问题的所有对象部分。

I'm amazed you have to roll your own with this - I expected there to be a drop schema blah cascade. 我很惊讶你必须自己动手 - 我希望有一个下降模式等级级联。 Surely every single person who sets up a dev server will have to do this and having to do some meta-programming before being able to do normal programming is seriously horrible. 当然,设置开发服务器的每个人都必须这样做,并且在能够进行正常编程之前必须进行一些元编程是非常可怕的。 Anyway... rant over! 无论如何......咆哮!

I started looking at some of these articles as a way to do it by clearing out a schema: There's an old article about doing this, however the tables mentioned on there are now marked as deprecated . 我开始查看其中一些文章作为清除模式的方法:有一篇关于这样做的旧文章 ,但是现在提到的表格已被标记为已弃用 I've also looked at the documentation for the new tables to help understand what is going on here. 我还查看了新表文档,以帮助理解这里发生了什么。

There's another answer and a great dynamic sql resource it links to. 还有另一个答案 ,它链接到一个很棒的动态sql资源

After looking at all this stuff for a while it just all seemed a bit too messy. 看了所有这些东西一段时间后,它们似乎有点太乱了。

I think the better option is to go for 我认为更好的选择是去

ALTER DATABASE 'blah' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
drop database 'blah'

create database 'blah'

instead. 代替。 The extra incantation at the top is basically to force drop the database as mentioned here 顶部的额外咒语基本上是强制删除这里提到的数据库

It feels a bit wrong but the amount of complexity involved in writing the drop script is a good reason to avoid it I think. 感觉有点不对,但编写drop脚本所涉及的复杂程度是我认为避免它的一个很好的理由。

If there seem to be problems with dropping the database I might revisit some of the links and post another answer 如果删除数据库似乎有问题,我可能会重新访问一些链接并发布另一个答案

try this with sql2012 or above, 尝试使用sql2012或更高版本,

this will be help to delete all objects by selected schema 这将有助于删除所选模式的所有对象

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)

I do not know wich version of Sql Server are you using, but assuming that is 2008 or later, maybe the following command will be very useful (check that you can drop ALL TABLES in one simple line): 我不知道您使用的是哪个版本的Sql Server,但假设是2008或更高版本,可能以下命令将非常有用(检查您是否可以将所有表格放在一个简单的行中):

 sp_MSforeachtable "USE DATABASE_NAME DROP TABLE ?"

This script will execute DROP TABLE .... for all tables from database DATABASE_NAME . 此脚本将对数据库DATABASE_NAME中的所有表执行DROP TABLE .... Is very simple and works perfectly. 很简单,效果很好。 This command can be used for execute other sql instructions, for example: 此命令可用于执行其他sql指令,例如:

sp_MSforeachtable "USE DATABASE_NAME SELECT * FROM ?"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM