[SQL server] 解決 rename database 時出現exclusive lock 問題

當rename database 時, 有時會出現elusive lock 問題. 這是因為執行時有其他user 正在使用而令database locked. 解決這個問題, 除了offline database 再rename名, 還可以把database 轉為single user mode 後再 rename, 有關的 SQL command 如下:

USE master;
--Set the database to single mode.
ALTER DATABASE <<dbName>>
SET SINGLE_USER WITH ROLLBACK IMMEDIATE

--Try to rename the database.
ALTER DATABASE <<dbName>> MODIFY NAME = <<NewDBName>>

--Set the database to Multiuser mode.
ALTER DATABASE <<NewDBName>>
SET MULTI_USER WITH ROLLBACK IMMEDIATE

 

About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.