如何终止与 SQL Server 2005 数据库的所有当前连接? [英] How do you kill all current connections to a SQL Server 2005 database?

查看:32
本文介绍了如何终止与 SQL Server 2005 数据库的所有当前连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重命名数据库,但不断收到数据库上无法获得排他锁"的错误消息,这意味着仍有一些连接处于活动状态.

I want to rename a database, but keep getting the error that 'couldn't get exclusive lock' on the database, which implies there is some connection(s) still active.

如何终止与数据库的所有连接以便重命名它?

How can I kill all the connections to the database so that I can rename it?

推荐答案

Adam 建议 行不通的是,在您循环活动连接期间,可以建立新的连接,你会想念那些的.您可以改用以下没有此缺点的方法:

The reason that the approach that Adam suggested won't work is that during the time that you are looping over the active connections new one can be established, and you'll miss those. You could instead use the following approach which does not have this drawback:

-- set your current connection to use master otherwise you might get an error

use master
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE 

--do you stuff here 

ALTER DATABASE YourDatabase SET MULTI_USER

这篇关于如何终止与 SQL Server 2005 数据库的所有当前连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆