Error on renaming database in SQL Server 2008 R2
SqlSql ServerSql Server-2008-R2Sql 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
-
Set the database to single mode:
ALTER DATABASE dbName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
-
Try to rename the database:
ALTER DATABASE dbName MODIFY NAME = NewName
-
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.
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