我想重命名一个数据库,但不断得到“无法获得数据库上的排他锁”的错误,这意味着仍有一些连接处于活动状态。
如何杀死数据库的所有连接以便重命名它?
我想重命名一个数据库,但不断得到“无法获得数据库上的排他锁”的错误,这意味着仍有一些连接处于活动状态。
如何杀死数据库的所有连接以便重命名它?
当前回答
在对象资源管理器上的MS SQL Server Management Studio中,右键单击数据库。在下面的上下文菜单中选择“任务->脱机”
其他回答
另一种“以身作则”的方法是重新启动MSSQLSERVER服务。 我喜欢从命令行做一些事情。把它粘贴到CMD中就可以了: Net stop mssqlserver & Net start mssqlserver
或者开放“服务”。找到“SQL Server (MSSQLSERVER)”,单击右键,选择“restart”。
这将“肯定,肯定”杀死该实例上运行的所有数据库的所有连接。
(比起许多在服务器/数据库上反复更改配置的方法,我更喜欢这种方法)
下线需要一段时间,有时我会遇到一些问题。
在我看来最可靠的方法是:
分离 右键单击DB -> Tasks -> Detach… 检查“删除连接” 好吧
重新接上 右键单击数据库->附加.. 添加……->选择您的数据库,并将Attach As列更改为所需的数据库名称。 好吧
右键单击数据库名称,单击属性进入属性窗口,打开选项选项卡,将“限制访问”属性从多用户更改为单用户。当你点击OK按钮时,它会提示你关闭所有打开的连接,选择“是”,你就可以将数据库重命名为....
下面是如何在MS SQL Server Management Studio 2008(可能也适用于其他版本)中可靠地执行这类事情:
In the Object Explorer Tree, right click the root database server (with the green arrow), then click activity monitor. Open the processes tab in the activity monitor, select the 'databases' drop down menu, and filter by the database you want. Right click the DB in Object Explorer and start a 'Tasks -> Take Offline' task. Leave this running in the background while you... Safely shut down whatever you can. Kill all remaining processes from the process tab. Bring the DB back online. Rename the DB. Bring your service back online and point it to the new DB.
杀他,用火杀他。
USE master
go
DECLARE @dbname sysname
SET @dbname = 'yourdbname'
DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END