Error on renaming database in SQL Server 2008 R2

SqlSql ServerSql Server-2008-R2

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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

Solution 2 - Sql

  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
    

Solution 3 - Sql

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.

Solution 4 - Sql

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

Solution 5 - Sql

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

Solution 6 - Sql

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

Solution 7 - Sql

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

Solution 8 - Sql

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

Solution 9 - Sql

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

Solution 10 - Sql

Another way to close all connections:

Administrative Tools > View Local Services

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

Solution 11 - Sql

use master

ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE  
  
exec sp_renamedb 'BOSEVIKRAM','BOSEVIKRAM_Deleted'

ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVikram BoseView Question on Stackoverflow
Solution 1 - SqlSquidView Answer on Stackoverflow
Solution 2 - SqlSamiey MehdiView Answer on Stackoverflow
Solution 3 - SqlJustin WoodmancyView Answer on Stackoverflow
Solution 4 - SqlAndrey GordeevView Answer on Stackoverflow
Solution 5 - Sqlt_plusplusView Answer on Stackoverflow
Solution 6 - SqlDhwaniView Answer on Stackoverflow
Solution 7 - SqlK GANGAView Answer on Stackoverflow
Solution 8 - SqlslayernoahView Answer on Stackoverflow
Solution 9 - SqlremondoView Answer on Stackoverflow
Solution 10 - SqlChronozoaView Answer on Stackoverflow
Solution 11 - SqlKamalDeepView Answer on Stackoverflow