217

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vikram Bose
  • 3,197
  • 2
  • 16
  • 33
  • 4
    There's nothing wrong with the query - the error is telling you that other connections are connected to the database, so you're not allowed to rename it at this time. – Damien_The_Unbeliever May 22 '13 at 06:55
  • 2
    If you're doing this from SSMS, make sure you don't have a query window open against that db, as that's a separate connection that places a lock on the db. – jleach Feb 19 '16 at 09:17

11 Answers11

413

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
Community
  • 1
  • 1
Squid
  • 4,560
  • 1
  • 12
  • 8
  • 2
    is `WITH ROLLBACK IMMEDIATE` necessary. If I don't use it at all, will it cause problems? – user13892 Sep 06 '19 at 17:53
  • 2
    Bit late to the party, but to answer this question: yes, you should use `WITH ROLLBACK IMMEDIATE` while altering a database that other users might be operating on, in order to ensure the integrity of these operations. But it is not really necessary when setting the database back to MULTI_USER mode again since the database is already at SINGLE_USER mode and you are the only user able to run any transactions anyway. – Hakan Yildizhan Mar 29 '20 at 18:00
79
  1. Set the database to single mode:

    ALTER DATABASE dbName
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    
  2. Try to rename the database:

    ALTER DATABASE dbName MODIFY NAME = NewName
    
  3. Set the database to Multiuser mode:

    ALTER DATABASE NewName
    SET MULTI_USER WITH ROLLBACK IMMEDIATE
    
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63
58

In SQL Server Management Studio (SSMS):

You can also right click your database in the Object Explorer and go to Properties. From there, go to Options. Scroll all the way down and set Restrict Access to SINGLE_USER. Change your database name, then go back in and set it back to MULTI_USER.

Pang
  • 9,564
  • 146
  • 81
  • 122
Justin Woodmancy
  • 842
  • 6
  • 11
22

Try to close all connections to your database first:

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

Taken from here

Community
  • 1
  • 1
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
7

This did it for me:

USE [master];
GO
ALTER DATABASE [OldDataBaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
EXEC sp_renamedb N'OldDataBaseName', N'NewDataBaseName';


-- Add users again
ALTER DATABASE [NewDataBaseName] SET MULTI_USER
GO
t_plusplus
  • 4,079
  • 5
  • 45
  • 60
5

That's because someone else is accessing the database. Put the database into single user mode then rename it.

This link might help:
http://msdn.microsoft.com/en-IN/library/ms345378(v=sql.105).aspx

and also:
http://msdn.microsoft.com/en-us/library/ms345378.aspx

Pang
  • 9,564
  • 146
  • 81
  • 122
Dhwani
  • 7,484
  • 17
  • 78
  • 139
5

1.database set 1st single user mode

ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE

2.RENAME THE DATABASE

ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]

3.DATABAE SET MULIUSER MODE

ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER WITH ROLLBACK IMMEDIATE

K GANGA
  • 51
  • 1
  • 2
3

Change database to single user mode as shown in the other answers

Sometimes, even after converting to single user mode, the only connection allowed to the database may be in use.

To close a connection even after converting to single user mode try:

select * from master.sys.sysprocesses
where spid>50 -- don't want system sessions
  and dbid = DB_ID('BOSEVIKRAM')

Look at the results and see the ID of the connection to the database in question.

Then use the command below to close this connection (there should only be one since the database is now in single user mode)

KILL connection_ID

Replace connection_id with the ID in the results of the 1st query

slayernoah
  • 4,382
  • 11
  • 42
  • 73
2

For me the reason why I could not rename a database is because there are active connections. I just take the database offline first, ticking the Drop All Active Connections. Then bring it online again and I can rename the database already. Take Offline

enter image description here

remondo
  • 318
  • 2
  • 7
0

Another way to close all connections:

Administrative Tools > View Local Services

Stop/Start the "SQL Server (MSSQLSERVER)" service

Chronozoa
  • 310
  • 6
  • 10
-2
use master

ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE  

exec sp_renamedb 'BOSEVIKRAM','BOSEVIKRAM_Deleted'

ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
KamalDeep
  • 791
  • 5
  • 8