When we try to rename the database then getting error below

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

To resolve this error we have to first set the database to​​ Single User Mode.

After that we can rename the database, then again we can set the database back to Multi-User mode.​​

There are some below steps​​ to rename database in SQL Server.​​

Step 1. To Set the database in single User mode:-

ALTER​​ DATABASE​​ OLD_DB_Name​​ SET​​ SINGLE_USER​​ WITH​​ ROLLBACK​​ IMMEDIATE;

Step 2. Rename the Database

ALTER​​ DATABASE​​ OLD_DBName​​ MODIFY​​ NAME​​ =​​ NEW_DBNAME;

Step 3. Set the database back to Multi-user mode

ALTER​​ DATABASE​​ NEW_DBNAME​​ SET​​ MULTI_USER​​ WITH​​ ROLLBACK​​ IMMEDIATE;