Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

I am using this query to rename the database:

ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]

But it shows an error when executing:

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?

1 Answer

0 votes
by (40.4k points)

Try to set the database to a single-user mode like this:

ALTER DATABASE dbName

SET SINGLE_USER WITH ROLLBACK IMMEDIATE

Then, you can use to rename the database and to set the database to Multiuser Mode:  

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

Browse Categories

...