sql server 2008 如何断开所有人与我的数据库的连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4230290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:21:23  来源:igfitidea点击:

sql server 2008 how do i disconnect everyone from my db?

sqlsql-serversql-server-2008

提问by l--''''''---------''''''''''''

I need to rename the database, but I cannot since other processes is using it. I don't care about the process, and I need to kill it.

我需要重命名数据库,但我不能,因为其他进程正在使用它。我不在乎这个过程,我需要杀死它。

How do I remove all connections from the db?

如何从数据库中删除所有连接?

回答by Martin Smith

As per my comment once in single_usermode you need to do the rename from the same connection. Don't try and rename it through Object Explorer as that will try and open up a new connection. The following works this end...

根据我的评论,一旦进入single_user模式,您需要从同一连接进行重命名。不要尝试通过对象资源管理器重命名它,因为这会尝试打开一个新连接。以下工作到此为止......

ALTER DATABASE AdventureWorks SET single_user WITH ROLLBACK IMMEDIATE

ALTER DATABASE AdventureWorks MODIFY NAME = NewAdventureWorks

ALTER DATABASE NewAdventureWorks SET multi_user 

回答by SQLMenace

Like this: Kill All Active Connections To A Database

像这样:杀死与数据库的所有活动连接

ALTER DATABASE oldNameSET SINGLE_USER WITH ROLLBACK IMMEDIATE

EXEC sp_renamedb 'oldName', 'newName'

ALTER DATABASE newName SET MULTI_USER  --new name of course

回答by Stu

Run sp_who, then kill [pid] for everyone in your database.

运行 sp_who,然后为数据库中的每个人杀死 [pid]。

回答by David Fawzy

This worked fine for me to rename database and force disconnect everybody:

这对我来说很有效,可以重命名数据库并强制断开每个人的连接:

ALTER DATABASE [old_name]
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [old_name]
    MODIFY NAME = [new_name]
GO
ALTER DATABASE [new_name]
    SET MULTI_USER
GO