Error on renaming database in SQL Server 2008 R2

Vikram Bose picture Vikram Bose · May 22, 2013 · Viewed 132.7k times · Source

I am using this query to rename the database:

ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]

But it shows an error when excuting:

Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.

Is anything wrong with my query?

Answer

Squid picture Squid · May 22, 2013

You could try setting the database to single user mode.

https://stackoverflow.com/a/11624/2408095

use master
ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE    
ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]
ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER