Network Management

Taking SQL DB offline takes a long time in SQL Management Studio

Taking a SQL Server database offline using the following query in SQL Management Studio takes a long time: ALTER DATABASE <dbname> SET OFFLINE WITH ROLLBACK IMMEDIATE. The following error is displayed: ALTER DATABASE failed because a lock could not be placed on database 'dbname' Try again later.

First published date

10/18/2018 6:23 AM

Last published date

4/26/2020 2:14 AM

Overview

Taking a SQL Server database offline using the following query in SQL Management Studio takes a long time:

-- Scripts are not supported under any SolarWinds support program or service. -- Scripts are provided AS IS without warranty of any kind. SolarWinds further -- disclaims all warranties including, without limitation, any implied warranties -- of merchantability or of fitness for a particular purpose. The risk arising -- out of the use or performance of the scripts and documentation stays with you. -- In no event shall SolarWinds or anyone else involved in the creation, -- production, or delivery of the scripts be liable for any damages whatsoever -- (including, without limitation, damages for loss of business profits, business -- interruption, loss of business information, or other pecuniary loss) arising -- out of the use of or inability to use the scripts or documentation.  
ALTER DATABASE <dbname> SET OFFLINE WITH ROLLBACK IMMEDIATE  


The following error is displayed:
 

ALTER DATABASE failed because a lock could not be placed on database 'dbname' Try again later.

Product section

Orion Platform

Cause

MSSQL is unable to close open connections.

Resolution

  1. Run the following:
    -- Scripts are not supported under any SolarWinds support program or service. -- Scripts are provided AS IS without warranty of any kind. SolarWinds further -- disclaims all warranties including, without limitation, any implied warranties -- of merchantability or of fitness for a particular purpose. The risk arising -- out of the use or performance of the scripts and documentation stays with you. -- In no event shall SolarWinds or anyone else involved in the creation, -- production, or delivery of the scripts be liable for any damages whatsoever -- (including, without limitation, damages for loss of business profits, business -- interruption, loss of business information, or other pecuniary loss) arising -- out of the use of or inability to use the scripts or documentation. 
    EXEC sp_who2
  2. Locate the database in the results, and locate the SPID for that connection.
  3. Run the following:
    -- Scripts are not supported under any SolarWinds support program or service. -- Scripts are provided AS IS without warranty of any kind. SolarWinds further -- disclaims all warranties including, without limitation, any implied warranties -- of merchantability or of fitness for a particular purpose. The risk arising -- out of the use or performance of the scripts and documentation stays with you. -- In no event shall SolarWinds or anyone else involved in the creation, -- production, or delivery of the scripts be liable for any damages whatsoever -- (including, without limitation, damages for loss of business profits, business -- interruption, loss of business information, or other pecuniary loss) arising -- out of the use of or inability to use the scripts or documentation. 
    KILL <SPID>