Error on renaming database in SQL Server 2008 R2
Asked Answered
S

11

226

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?

Saltant answered 22/5, 2013 at 6:53 Comment(2)
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.Tactician
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.Parishioner
K
424

You could try setting the database to single user mode.

https://mcmap.net/q/99934/-how-do-you-kill-all-current-connections-to-a-sql-server-2005-database

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
Kudva answered 22/5, 2013 at 7:2 Comment(2)
is WITH ROLLBACK IMMEDIATE necessary. If I don't use it at all, will it cause problems?Silly
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.Sapling
R
86
  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
    
Regardless answered 12/6, 2014 at 6:0 Comment(1)
@SamieyMehdi Should I use WITH ROLLBACk IMMEDIATE for multiuser?Elbrus
U
61

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.

Ullman answered 14/4, 2015 at 17:33 Comment(2)
Quick and easy!Manufacture
That's perfect. Working with SQL Server 2017Fortyish
A
23

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

Anguiano answered 22/5, 2013 at 6:59 Comment(0)
A
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
Argyres answered 17/2, 2015 at 15:29 Comment(0)
H
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

Helmsman answered 22/5, 2013 at 6:57 Comment(0)
Y
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

Yare answered 1/3, 2017 at 23:12 Comment(0)
P
4

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

Pheni answered 28/1, 2022 at 5:7 Comment(0)
F
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

Frager answered 5/10, 2014 at 1:31 Comment(0)
C
0

Another way to close all connections:

Administrative Tools > View Local Services

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

Crain answered 14/2, 2017 at 19:45 Comment(0)
O
-3
use master

ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE  

exec sp_renamedb 'BOSEVIKRAM','BOSEVIKRAM_Deleted'

ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER
Overspread answered 30/1, 2017 at 8:59 Comment(1)
This is the exact same answer as Squid'sMerriman

© 2022 - 2024 — McMap. All rights reserved.