When you want to rename your database and you hit the error below, you need to set the database to Single User Mode. After you rename your database, then you set the database back to Multi-User mode.
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation
Follow the steps below to rename your database.
- Set the database to single-mode
ALTER DATABASE OLD_DBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
2. Rename the Database
ALTER DATABASE OLD_DBName MODIFY NAME = NEW_DBNAME;
3. Set the database to Multi-user mode
ALTER DATABASE NEW_DBNAME SET MULTI_USER WITH ROLLBACK IMMEDIATE;
You may check out this blog post where I show a better way to rename a database.
Cheers!