Jack N @ GitHub

Full stack engineer, focus on: Angular/React, node.js/.Net

0%

SQL Server: The database could not be exclusively locked to perform the operation

Error on renaming database in SQL Server, when rename the database:

1
ALTER DATABASE MY_DB MODIFY NAME = [MY_DB_bak]

But it shows an error when executing:

1
The database could not be exclusively locked to perform the operation.

The solution is setting the database to single user mode.

1
2
3
4
use master
ALTER DATABASE MY_DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE MY_DB MODIFY NAME = [MY_DB_bak]
ALTER DATABASE MY_DB_bak SET MULTI_USER