1

我之前创建了一个示例数据库,现在我想删除该数据库,但它没有被删除。我在网上搜索,但没有找到任何有效的解决方案。

使用 T-SQL,我尝试了:

USE [Sample]

ALTER DATABASE [Sample]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO

DROP DATABASE [Sample]

使用 GUI,我收到以下错误:

在此处输入图像描述

我关闭了现有的连接,然后这也发生了,这是我的本地机器。请在这里帮助我!

4

4 回答 4

3

使用此代码:

USE MASTER 
GO

ALTER DATABASE Sample 
SET multi_user WITH ROLLBACK IMMEDIATE
GO


ALTER DATABASE Sample
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

DROP DATABASE Sample
GO
于 2018-01-27T11:06:48.680 回答
0

关闭您的 SSMS ,打开一个新实例,然后尝试以下操作,将整个内容复制粘贴到一个新的查询窗口中并立即执行:

USE [master];            
GO

ALTER DATABASE [Sample]   --<-- go back into Multi-user mode
SET MULTI_USER;
GO

ALTER DATABASE [Sample]   --<-- Will disconnect everyone 1st
SET SINGLE_USER              -- and will leave the database in single user mode
WITH ROLLBACK IMMEDIATE;
GO

USE [Sample];                -- quickly grab that single connection before any 
GO                           -- other process does

USE [master];                -- Connect to master db,  connection
GO                           -- This also means disconnect Sample DB 

DROP DATABASE [Sample]       -- At this point there should be no active connections
GO                           -- to this database and can be dropped 
于 2018-01-27T11:07:19.747 回答
0

删除数据库的另一种方法(无需编码)

  • 在 Microsoft SQL Server Management Studio 中,右键单击要删除的数据库(在您的情况下为示例数据库),然后选择Properties
  • 选择“选项”页面。
  • 另一个选项下,找到“状态”下的“限制用户”选项。
  • 将其值MULTI_USER更改为SINGLE_USER
  • 然后按Okay 并重试(或再次右键单击数据库并单击Delete

截屏

于 2018-01-27T12:26:19.880 回答
0

使用此代码应该会有所帮助。

ALTER DATABASE [dbname]
SET SINGLE_USER --or RESTRICTED_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE [dbname];
GO
于 2019-07-18T09:27:47.703 回答