SQL Server Error 991 occurs when the database engine cannot transition a database to the OFFLINE state because there are active connections or pending transactions that the system cannot automatically terminate. To resolve this, you must manually identify and kill the active sessions associated with that specific database or use a command that forces the disconnection of all users before attempting the offline transition again.

What You'll Need

  • SQL Server Management Studio (SSMS) or a similar SQL query editor.
  • Administrative privileges (sysadmin role) to execute KILL commands and alter database states.
  • The exact name of the database that is triggering the error.

Step-by-Step

  1. Identify Active Connections: Before attempting to take the database offline again, you need to see who or what is currently using it. Open a new query window in SSMS and run the following command: SELECT session_id, login_name, host_name, program_name FROM sys.dm_exec_sessions WHERE database_id = DB_ID('YourDatabaseName'); (Replace 'YourDatabaseName' with the actual name of your database). This provides a list of all Session IDs (SPIDs) currently locking the database.
  2. Terminate Active Sessions Manually: For every session_id returned in the previous step, execute the KILL command. For example, if the session ID is 54, run KILL 54;. Repeat this for every single active session listed. This ensures that no remaining locks prevent the database from transitioning states.
  3. Set the Database to Single User Mode: To prevent new connections from forming while you are trying to take the database offline, switch it to SINGLE_USER mode with an immediate rollback of existing transactions. Execute: ALTER DATABASE [YourDatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;. The ROLLBACK IMMEDIATE clause is critical because it tells SQL Server to terminate all current connections and roll back any uncommitted transactions instantly, rather than waiting for them to complete.
  4. Execute the Offline Command: Now that the database is in single-user mode and all other connections are severed, you can safely take it offline. Run the command: ALTER DATABASE [YourDatabaseName] SET OFFLINE;.
  5. Verify the State Change: Check the Object Explorer in SSMS. Right-click the server instance and select Refresh. The database should now show (Offline) next to its name. Alternatively, run SELECT name, state_desc FROM sys.databases WHERE name = 'YourDatabaseName'; to confirm the state is listed as OFFLINE.
  6. Return to Multi-User Mode (Optional): If you later bring the database back online, remember that it is still in SINGLE_USER mode. To allow your applications and users back in, run: ALTER DATABASE [YourDatabaseName] SET MULTI_USER;.

Common Mistakes

One of the most frequent mistakes is attempting to use the GUI (Right-click > Tasks > Take Offline) without checking the "Drop All Active Connections" checkbox. If this box is left unchecked, SQL Server will attempt a "graceful" shutdown of the database. If a long-running query or a hung application session exists, the request will time out or fail with Error 991 because the system refuses to kill the process automatically.

Another common error is trying to run the SET OFFLINE command while the user's own query window is pointed at the database they are trying to take offline. If your current session is using the database, you are essentially trying to lock the door while you are still standing inside the room. Always ensure your query window is set to the master database (via the dropdown menu in the top left of SSMS) before executing these commands.

Some administrators mistakenly believe that restarting the entire SQL Server service is the only way to clear these locks. While a service restart does work, it is a "sledgehammer" approach that disrupts every other database on the instance. Using ROLLBACK IMMEDIATE is a surgical approach that only affects the target database.

Tips

When dealing with production environments, always communicate with the application team before running ROLLBACK IMMEDIATE. Because this command kills active transactions, any user currently writing data to the database will experience an abrupt disconnection and their unsaved changes will be lost. It is best to perform these actions during a scheduled maintenance window.

If you find yourself needing to take databases offline frequently for backups or maintenance, consider using a script that automates the transition to SINGLE_USER mode. This reduces the risk of human error and ensures that the process is consistent across different environments (Dev, Test, and Production).

Keep an eye on the program_name column when running the sys.dm_exec_sessions query. This helps you identify if the lock is being caused by a scheduled SQL Agent job, a third-party backup tool, or a specific user's workstation, which can help you address the root cause of why the database was busy in the first place.

Troubleshooting

If you have executed the KILL commands and the SET SINGLE_USER WITH ROLLBACK IMMEDIATE command, but you still receive Error 991, check for "Zombie" processes. Sometimes a process is in a state of KILLED/ROLLBACK. This happens when a very large transaction is being undone. SQL Server cannot take the database offline until the rollback is complete because it must maintain data integrity.

To check for this, run sp_who2 and look at the Status column. If you see rollback, you must wait. Attempting to force the database offline during a rollback can lead to database corruption or an extremely long recovery time when you eventually bring the database back online.

If the database is part of an Availability Group (AG) or Mirroring setup, you cannot simply take it offline on a secondary replica. You must first remove the database from the Availability Group or fail over to a different node. Attempting to force an offline state on a synchronized secondary will often result in errors because the mirroring endpoint is maintaining a constant connection to the primary.

In rare cases, a system-level lock (such as an antivirus scan locking the .mdf or .ldf files at the OS level) can interfere with the database engine's ability to change the state. If the SQL commands fail but no active sessions are found, check the Windows Event Viewer for I/O errors or file access conflicts.

Frequently Asked Questions

Does taking a database offline delete any data?

No. Taking a database offline simply tells the SQL Server engine to stop accessing the files and release the handles on the .mdf and .ldf files. The data remains intact on the disk.

What is the difference between Offline and Detached?

Offline keeps the database registered within the SQL Server system metadata, but the files are inaccessible. Detaching removes the database from the SQL Server instance entirely, allowing you to move the files to another server.

Can I take the master database offline?

No. The master database is required for the SQL Server instance to function. It cannot be taken offline while the service is running.

Why does ROLLBACK IMMEDIATE cause some users to lose data?

Because it does not wait for the user to finish their current transaction. It immediately terminates the connection and reverts any uncommitted changes to the last known stable state.

How do I bring a database back online after Error 991 is fixed?

Use the command ALTER DATABASE [YourDatabaseName] SET ONLINE;. If you previously set it to SINGLE_USER mode, remember to set it back to MULTI_USER immediately after.