简体   繁体   English

SQL Server 2008 R2:按条件删除数据库

[英]SQL Server 2008 R2 : Drop Database on Condition

Now, what I am trying here is DROPPING the database from my SQL Server on the basis of any condition (Inside a IF block), eg. 现在,我在这里尝试的是基于任何条件(例如,在IF块内)从SQL Server删除数据库。 If database exists, delete and recreate it. 如果数据库存在,请删除并重新创建。

I am using following code: 我正在使用以下代码:

IF EXISTS (SELECT 1 FROM sys.databases db with(nolock) WHERE db.name = 'practice')
BEGIN
    USE master
    DROP DATABASE practice
END

This code keeps on executing for couple of minutes and then replies with an error saying 这段代码会持续执行几分钟,然后回复错误消息

Msg 3702, Level 16, State 4, Line 1 消息3702,第16级,状态4,第1行
Cannot drop database "practice" because it is currently in use. 无法删除数据库“实践”,因为它当前正在使用中。

Then for another attempt I used a GO statement within my code to separate the execution flow, with following code (NOTE this has create database statement as well): 然后再尝试一次,在代码中使用GO语句来分隔执行流程,并添加以下代码(注意,该语句也具有创建数据库的语句):

IF EXISTS (SELECT 1 FROM sys.databases db with(nolock) WHERE db.name = 'practice')
BEGIN
    USE master
    GO

    DROP DATABASE practice
    GO

    CREATE DATABASE practice
END

Now it throws this error: 现在抛出此错误:

Msg 102, Level 15, State 1, Line 3 Msg 102,第15级,状态1,第3行
Incorrect syntax near 'master'. 'master'附近的语法不正确。
Msg 3702, Level 16, State 4, Line 1 消息3702,第16级,状态4,第1行
Cannot drop database "practice" because it is currently in use. 无法删除数据库“实践”,因为它当前正在使用中。
Msg 102, Level 15, State 1, Line 2 Msg 102,第15级,状态1,第2行
Incorrect syntax near 'END' “ END”附近的语法不正确

I don't know if I am missing something, or is there any workaround that needs to be done. 我不知道我是否缺少某些东西,或者是否有任何需要解决的方法。

Also, I would be interested in better alternates to serve this purpose, if any. 另外,如果有的话,我会希望有更好的替代品来实现此目的。

I don't know if this is even feasible. 我不知道这是否可行。

Regards, Shanks 问候,小腿

As the error message suggests,database is in use.You can drop databases forcefully using below script 如错误消息所示,数据库正在使用中。您可以使用以下脚本强制删除数据库

 if exists(select * from sys.sysdatabases where name ='test')
    begin
    use master;

    alter database test
    set single_user with rollback immediate;

    drop database test;

    end

You can also do an 您也可以

sp_who2 

to see who or what is connected to that DB. 查看谁或什么连接到该数据库。 You can then use a 然后,您可以使用

KILL SPID# (i.e. "KILL 54") 

to force that connection to close. 强制关闭该连接。 This is dangerous if you are in production but if you are dropping and recreating the DB anyways... 如果您正在生产中,但是无论如何都要删除并重新创建数据库,这将很危险...

This is a little more surgical than what @TheGameiswar suggests but is very similar. 这比@TheGameiswar的建议更具手术效果,但非常相似。

For the purpose of practice can use following code: 出于练习的目的,可以使用以下代码:

EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'practice'
GO
USE [master]
GO
ALTER DATABASE [practice] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
DROP DATABASE [practice]
GO

now you can create your new DB. 现在您可以创建新的数据库。

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

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