Script to kill all connections to a database (More than RESTRICTED_USER ROLLBACK)

后端 未结 14 1503
-上瘾入骨i
-上瘾入骨i 2020-11-28 17:28

I have a development database that re-deploy frequently from a Visual Studio Database project (via a TFS Auto Build).

Sometimes when I run my build I get this error:

相关标签:
14条回答
  • 2020-11-28 17:49

    Updated

    For MS SQL Server 2012 and above

    USE [master];
    
    DECLARE @kill varchar(8000) = '';  
    SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), session_id) + ';'  
    FROM sys.dm_exec_sessions
    WHERE database_id  = db_id('MyDB')
    
    EXEC(@kill);
    

    For MS SQL Server 2000, 2005, 2008

    USE master;
    
    DECLARE @kill varchar(8000); SET @kill = '';  
    SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'  
    FROM master..sysprocesses  
    WHERE dbid = db_id('MyDB')
    
    EXEC(@kill); 
    
    0 讨论(0)
  • 2020-11-28 17:50
    USE MASTER
    GO
     
    DECLARE @Spid INT
    DECLARE @ExecSQL VARCHAR(255)
     
    DECLARE KillCursor CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY
    FOR
    SELECT  DISTINCT SPID
    FROM    MASTER..SysProcesses
    WHERE   DBID = DB_ID('dbname')
     
    OPEN    KillCursor
     
    -- Grab the first SPID
    FETCH   NEXT
    FROM    KillCursor
    INTO    @Spid
     
    WHILE   @@FETCH_STATUS = 0
        BEGIN
            SET     @ExecSQL = 'KILL ' + CAST(@Spid AS VARCHAR(50))
     
            EXEC    (@ExecSQL)
     
            -- Pull the next SPID
            FETCH   NEXT 
            FROM    KillCursor 
            INTO    @Spid  
        END
     
    CLOSE   KillCursor
     
    DEALLOCATE  KillCursor
    
    0 讨论(0)
  • 2020-11-28 17:52

    To my experience, using SINGLE_USER helps most of the times, however, one should be careful: I have experienced occasions in which between the time I start the SINGLE_USER command and the time it is finished... apparently another 'user' had gotten the SINGLE_USER access, not me. If that happens, you're in for a tough job trying to get the access to the database back (in my case, it was a specific service running for a software with SQL databases that got hold of the SINGLE_USER access before I did). What I think should be the most reliable way (can't vouch for it, but it is what I will test in the days to come), is actually:
    - stop services that may interfere with your access (if there are any)
    - use the 'kill' script above to close all connections
    - set the database to single_user immediately after that
    - then do the restore

    0 讨论(0)
  • 2020-11-28 17:53

    I have tested successfully with simple code below

    USE [master]
    GO
    ALTER DATABASE [YourDatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    GO
    
    0 讨论(0)
  • 2020-11-28 18:01

    You should be careful about exceptions during killing processes. So you may use this script:

    USE master;
    GO
     DECLARE @kill varchar(max) = '';
     SELECT @kill = @kill + 'BEGIN TRY KILL ' + CONVERT(varchar(5), spid) + ';' + ' END TRY BEGIN CATCH END CATCH ;' FROM master..sysprocesses 
    EXEC (@kill)
    
    0 讨论(0)
  • 2020-11-28 18:02

    You can get the script that SSMS provides by doing the following:

    1. Right-click on a database in SSMS and choose delete
    2. In the dialog, check the checkbox for "Close existing connections."
    3. Click the Script button at the top of the dialog.

    The script will look something like this:

    USE [master]
    GO
    ALTER DATABASE [YourDatabaseName] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
    GO
    USE [master]
    GO
    DROP DATABASE [YourDatabaseName]
    GO
    
    0 讨论(0)
提交回复
热议问题