Msg 5030 means another connection has the database open. Find that session before retrying the operation.
What Msg 5030 Says
Something is connected to the database, and the operation you asked for needs everyone out. Renames, collation changes, detach and several ALTER DATABASE options all need an exclusive lock. On SQL Server 2025 the message names the database it could not lock; the same sentence without the name, which is how it is usually quoted, is the same error. SET SINGLE_USER is the odd one out: without a termination clause it waits for the lock rather than failing, and WITH NO_WAIT gives up with Msg 5070 instead. The connection in the way is very often your own.

Find the Sessions Holding the Database Open
SELECT s.session_id,
s.login_name,
s.host_name,
s.program_name,
s.status,
s.last_request_end_time
FROM sys.dm_exec_sessions AS s
WHERE s.database_id = DB_ID(N'YourDb')
ORDER BY s.last_request_end_time;
Read the program_name column before you kill anything. 3 answers come up constantly:
program_name | What it really is |
|---|---|
Microsoft SQL Server Management Studio | Object Explorer, holding its own connection |
Microsoft SQL Server Management Studio - Query | A query window someone left open, possibly yours |
| Your application’s name | A live connection pool, which will reconnect the moment you clear it |
Check Your Query Window and Object Explorer
Your own window counts. If your query window’s context is the database you are altering, you are the blocker. Switch to master first, then run the statement:
USE master;
GO
ALTER DATABASE YourDb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Object Explorer counts too, and it reconnects on its own. Expanding the database node in SSMS is enough to take a connection, which is why the operation fails again 30 seconds after you cleared everything.
WITH ROLLBACK IMMEDIATE is what makes this work: it rolls back every open transaction in that database rather than waiting. That is a data-losing action for anyone mid-transaction, so it belongs in a maintenance window, not in a working day.
Microsoft’s reference covers setting a database to single-user mode in full, with one prerequisite worth reading before you get locked out: if AUTO_UPDATE_STATISTICS_ASYNC is on, the background statistics thread takes its own connection to the database and can hold the single slot, so check it is off first.
Perform the Change and Restore Multi-User Access
USE master;
GO
ALTER DATABASE YourDb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE YourDb MODIFY NAME = YourDbArchive;
GO
ALTER DATABASE YourDbArchive SET MULTI_USER;
Never leave a database in SINGLE_USER at the end of a session. An application connection pool will take the one available slot within seconds and you will be locked out of your own database.
Recover When Single-User Mode Locks You Out
It happens, and the recovery is not obvious, because you cannot get in to fix it.
SELECT s.session_id, s.login_name, s.program_name
FROM sys.dm_exec_sessions AS s
WHERE s.database_id = DB_ID(N'YourDb');
KILL 57; -- the session holding the single slot
Run that from master, kill the holder, and immediately run SET MULTI_USER before anything else reconnects. If you are racing an application pool, stop the application or take the database offline rather than losing the race repeatedly.
Avoid Scheduling SINGLE_USER with ROLLBACK IMMEDIATE
Do not put SET SINGLE_USER WITH ROLLBACK IMMEDIATE in a scheduled job as a way of making a maintenance script reliable. It converts other people’s committed work into rolled-back work on a timer, and the job’s own history will not record what it destroyed.
Frequently Asked Questions
I closed every window and it still fails.
master, and trust sys.dm_exec_sessions rather than the SSMS interface.Is WITH ROLLBACK IMMEDIATE safe?
I set SINGLE_USER and now I cannot get back in.
sys.dm_exec_sessions from master, KILL it, then run SET MULTI_USER immediately. If you keep losing the race, stop the application or take the database offline.Which statements raise 5030, and what do the states mean?
ALTER DATABASE ... MODIFY NAME raises 5030 with State 2 and ALTER DATABASE ... COLLATE raises it with State 5, both immediately. SET SINGLE_USER does not raise it: without a termination clause it waits for the other session to leave, and WITH NO_WAIT fails with Msg 5070 instead. DROP DATABASE with a session inside fails with Msg 3702. Same cause every time, different message.Where To Go Next
If the database still cannot be locked, use these to find or clear the connections, or recover if it is already in single-user mode:
- Get Active Connections by Database, to see which sessions are connected before deciding what to stop.
- Kill All User Sessions on a Database, to clear connected sessions before an operation needs exclusive access.
- The Database Is in Single-User Mode, to recover if the single available connection is already taken.
Leave a Reply