SQL Server Error 1447: Fix Alter Database Command
In this article
SQL Server Error 1447 occurs when you attempt to execute an ALTER DATABASE command while other users or processes are still connected to that database. To resolve this, you must terminate all active connections to the target database so that SQL Server can gain exclusive access. The fastest way to achieve this is by setting the database to SINGLE_USER mode with an immediate rollback of all current transactions.
What You'll Need
- SQL Server Management Studio (SSMS) or a similar SQL query tool.
- Administrative privileges (sysadmin role) on the SQL Server instance.
- The exact name of the database you are attempting to modify.
Step-by-Step
- Open SQL Server Management Studio (SSMS): Launch the application and connect to the SQL Server instance where the error is occurring.
- Open a New Query Window: Click the "New Query" button in the toolbar. Ensure that the database dropdown menu in the top-left corner is set to "master". You cannot run the command to kick users out of a database if your own session is currently using that same database.
- Execute the Single User Command: Copy and paste the following T-SQL command, replacing
[YourDatabaseName]with the actual name of your database:
ALTER DATABASE [YourDatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
TheROLLBACK IMMEDIATEclause is critical; it tells SQL Server to immediately disconnect all other users and roll back any uncommitted transactions. Without this, the command will hang indefinitely until every single user manually logs off. - Perform the Desired Alteration: Now that you have exclusive access, run the original ALTER DATABASE command that triggered Error 1447. For example, if you were trying to change the database name or modify a file growth setting, execute that command now.
- Return the Database to Multi-User Mode: Once your changes are complete, you must allow other users and applications to connect again. Execute the following command:
ALTER DATABASE [YourDatabaseName] SET MULTI_USER;
- Verify Connectivity: Refresh the Object Explorer in SSMS to ensure the database is online and accessible to your application users.
Common Mistakes
One of the most frequent mistakes is attempting to run the SET SINGLE_USER command while the query window is pointed at the database being modified. If your session is the one holding the lock, the command may fail or cause a deadlock. Always ensure your connection context is set to the master database before attempting to change the access mode of another database.
Another common error is omitting the WITH ROLLBACK IMMEDIATE clause. Many administrators simply use SET SINGLE_USER. In a production environment with active application pools or background services, the command will simply wait for users to disconnect. Because applications often maintain persistent connection pools, the command may appear to "freeze" SSMS, leading the administrator to believe the server has crashed when it is actually just waiting for a connection to close.
Some users also forget to switch the database back to MULTI_USER mode. If you leave a database in single-user mode, the first person or service that connects to it will occupy that single slot. Any subsequent attempt by the application or other admins to connect will result in an error stating that the database is already in use, effectively causing an unplanned outage for that specific database.
Tips
To avoid this error in the future during scheduled maintenance, it is helpful to stop the application services or IIS App Pools that connect to the database before running your scripts. This ensures a clean disconnection and reduces the risk of data loss from rolled-back transactions.
If you are working in a high-traffic environment, consider using a script that checks for active sessions before attempting the alter command. You can query sys.dm_exec_sessions to identify exactly who is connected and what their session IDs (SPIDs) are. This allows you to communicate with users before forcibly disconnecting them.
When using SSMS, you can also perform these actions via the Graphical User Interface (GUI). Right-click the database, go to Properties, select the Options page, and find the "Restrict Access" setting. Changing this to "Single User" and selecting "Rollback Immediate" in the prompt achieves the same result as the T-SQL code provided above. However, the T-SQL method is generally faster and more reliable for automation.
Troubleshooting
If you execute the SET SINGLE_USER command but still cannot perform your ALTER operation, it is possible that another process "stole" the single available connection immediately after you cleared the others. This often happens with monitoring tools or automated backup agents that constantly poll the database.
To fix this, follow these steps:
- Run
sp_who2or querysys.dm_exec_sessionsto find the SPID (Session ID) currently connected to the database. - Use the
KILL [SPID]command to terminate that specific session. For example,KILL 54;. - Immediately execute your
ALTER DATABASEcommand before another process can connect.
If the KILL command does not work or the session immediately returns, you may need to temporarily disable the SQL Server Agent or stop the specific service (such as a third-party monitoring tool) that is aggressively reconnecting to the database. Once the external pressure is removed, you will be able to maintain the exclusive lock required for the alteration.
In extreme cases where the database remains locked despite these efforts, restarting the SQL Server service (MSSQLSERVER) will clear all active connections. Warning: This is a disruptive action as it will disconnect all users from all databases on that instance, not just the one you are targeting. Use this only as a last resort in a non-production environment or during a planned maintenance window.
Frequently Asked Questions
Does ROLLBACK IMMEDIATE cause data loss?
It does not delete committed data, but it does undo any transactions that were currently in progress and not yet committed. Any unsaved changes made by users at the exact moment of the command will be lost.
Can I run this command on a system database?
Yes, you can use this on system databases like msdb, but be extremely cautious. Forcing a single-user mode on critical system databases can interfere with server-wide operations and background tasks.
Why does the command hang when I run it?
The command hangs if you did not include WITH ROLLBACK IMMEDIATE. SQL Server is waiting for all existing connections to close naturally, which may never happen if an application is using a connection pool.
Is there a way to see who is connected before I kick them off?
Yes, you can run SELECT session_id, login_name, host_name FROM sys.dm_exec_sessions WHERE database_id = DB_ID('YourDatabaseName'); to see a list of all active users.
Will this affect other databases on the same server?
No. Setting a specific database to single-user mode only affects that individual database. All other databases on the instance will continue to operate in multi-user mode normally.
What is the difference between SINGLE_USER and RESTRICTED_USER?
SINGLE_USER allows only one connection total. RESTRICTED_USER allows multiple connections, but only for members of the db_owner, dbcreator, or sysadmin roles, blocking general application users.