無法將資料庫從SINGLE_USER模式切換回MULTI_USER模式(Error 5064)
阿新 • • 發佈:2018-11-29
今天公司SQL Server資料庫無意間變為SINGLE_USER模式了,而且使用如下語句切換回MULTI_USER失敗:
ALTER DATABASE [MyDB] SET MULTI_USER WITH ROLLBACK IMMEDIATE
報錯:
Msg 5064, Level 16, State 1, Line 1 Changes to the state or options of database 'MyDB' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it. Msg 5069, Level 16, State 1, Line 1 ALTER DATABASE statement failed.
該錯誤提示是有其它使用者在使用資料庫,沒辦法只好排查是誰:
1)通過sys.sysprocesses或者sys.dm_exec_sessions,或者儲存過程sp_who,然後用KILL命令把會話切斷
select * from sys.sysprocesses where spid > 50 And dbid=DB_ID ('MyDB')
SELECT * FROM sys.dm_exec_sessions WHERE database_id = DB_ID ('MyDB')
2)如果上面還是行不通,再檢查sys.dm_tran_locks,然後用KILL命令把會話切斷
select * from sys.dm_tran_locks where resource_database_id= DB_ID ('MyDB')
kill 148