When you want to rename your database and you hit the error below, you need to set the database to Single User Mode. After you rename your database, then you set the database back to Multi-User mode.
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation
Follow the steps below to rename your database.
- Set the database to single-mode
ALTER DATABASE OLD_DBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
2. Rename the Database
ALTER DATABASE OLD_DBName MODIFY NAME = NEW_DBNAME;
3. Set the database to Multi-user mode
ALTER DATABASE NEW_DBNAME SET MULTI_USER WITH ROLLBACK IMMEDIATE;
You may check out this blog post where I show a better way to rename a database.
Cheers!
I am trying to delete an existing database in SQL Server 2005. My first attempt produced the following error:
5030: The database could not be exclusively
locked to perform the operation.
I have since killed all processes that are accessing the database. I have also removed the replication subscription that it had previously been involved in.
Any thoughts on what else that could be holding the lock on it besides SQL Server processes and replication?
Update: I restarted the server, and that fixed it. I was trying to avoid that, since this is a production server, but hey what can you do?
asked Feb 8, 2009 at 5:14
A production server in which so many connections use the database yet you want to drop it?
None the less, how to kick out everybody from the database:
USE [dbname];
ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Then drop the database:
USE [master];
DROP DATABASE [dbname];
There is still a very small window of opportunity between the USE [master];
and DROP DATABASE ...
where some other connection can grab the 1 single allowed lock on the database, but it usually not worth working around that.
answered Nov 23, 2009 at 20:05
Remus RusanuRemus Rusanu
289k40 gold badges443 silver badges569 bronze badges
3
I hate to say it, but a quick solution is to restart the system, make sure the sql server server service is not started, then you should be able to delete.
Also, is IIS stopped if you db is connected to a web ap?
answered Feb 8, 2009 at 5:19
Chris BallanceChris Ballance
33.8k26 gold badges104 silver badges151 bronze badges
2
You don’t happen to know if anyone left a transaction in an uncompleted rollback state (or otherwise uncompleted)? Might as well check the locks list, too.
answered Feb 8, 2009 at 5:34
dkretzdkretz
37.4k13 gold badges80 silver badges138 bronze badges
1
In the management studio, goto Management->Activity Monitor (right click) -> View Processes. That will give you a full list of everything running, you can sort the list by Database to see what is still attached, and you can also kill any connections. It’s easy to end up with orphaned connections that will prevent you from getting the exclusive access that you need.
answered Feb 8, 2009 at 5:19
MrTellyMrTelly
14.7k1 gold badge48 silver badges81 bronze badges
2
No One else should be using the DB, including yourself.
answered Feb 8, 2009 at 5:19
1
Why would we make a deleted DB to multi user mode.
ALTER DATABASE dbName SET MULTI_USER WITH ROLLBACK IMMEDIATE
Soner Gönül
97.2k102 gold badges207 silver badges365 bronze badges
answered Jun 13, 2014 at 7:55
To avoid this error, use the T-SQL script below in the master database. Make sure to run this (and modify the @dbname) for each database you are running the ALTER DATABASE command in.
«The database could not be exclusively locked to perform the
operation»
This «connection killer» script will work if Windows has established JDBC connections to the database. But this script is unable to kill off JDBC connections for Linux services (e.g. JBoss). So you’ll still get that error if you don’t stop JBoss manually. I haven’t tried other protocols, but please comment if you find out more information as you build new systems.
USE master;
DECLARE @dbname sysname
Set @dbname = 'DATABASE_NAME_HERE-PROD'
Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
Execute ('Kill ' + @spid)
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname) and spid > @spid
End
answered Oct 28, 2015 at 20:26
JustBeingHelpfulJustBeingHelpful
18.4k38 gold badges161 silver badges245 bronze badges
This error normally occurs when your database is in Multi User mode where users are accessing your database or some objects are referring to your database.
First you should set the database to single user mode:
ALTER DATABASE dbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Now we will try to delete the database
delete DATABASE ...
Finally set the database to Multiuser mode
ALTER DATABASE dbName
SET MULTI_USER WITH ROLLBACK IMMEDIATE
answered Sep 16, 2013 at 5:37
- Remove From My Forums
-
Question
-
I am trying to rename the database but i am getting below exception while doing it—>
Error: the database could not be exclusively locked to perform the operation.(Microsoft Sql Server,Error 5030)
Thanks.
Answers
-
Thats because someone else is accessing the database.. Put the database into single user mode the rename it.
USE [master]; GO ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO EXEC sp_renamedb N'foo', N'bar';
vt
Please mark answered if I’ve answered your question and vote for it as helpful to help other user’s find a solution quicker
-
Proposed as answer by
Tuesday, October 23, 2012 9:15 AM
-
Marked as answer by
Maggy111
Tuesday, October 23, 2012 9:22 AM
-
Proposed as answer by
If you are facing this error : “The database could not be exclusively locked to perform the operation. (Microsoft SQL Server, Error: 5030)” when you try to rename SQL server database you need to temporary switch from Multi User mode to single user mode, then rename the database and then set it back to Multi user mode.
Do it step by step.
ALTER DATABASE dbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE dbName MODIFY NAME = dbNewName
ALTER DATABASE dbNewName
SET MULTI_USER WITH ROLLBACK IMMEDIATE
Published
You might have seen «The database could not be exclusively locked to perform the operation. (Microsoft SQL Server, Error: 5030)» error when you try to rename SQL server database. This error normally occurs when your database is in Multi User mode where users are accessing your database or some objects are referring to your database.
Nothing much to do to resolve the issue. First set the database to single user mode and then try to rename the database and then set it back to Multi user mode.
We will go through step by step.
First we will see how to set the database to single user mode,
ALTER DATABASE dbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Now we will try to rename the database
ALTER DATABASE dbName MODIFY NAME = dbNewName
Finally we will set the database to Multiuser mode
ALTER DATABASE dbName
SET MULTI_USER WITH ROLLBACK IMMEDIATE
Hope you are able to rename your database without any issues now!!!