2012-04-30 81 views
29

我想關閉與MS SQL Server的現有連接,以便可以以編程方式對該數據庫執行還原。如何關閉與數據庫的現有連接

+1

可能重複:[?你如何殺死一個SQL Server 2005數據庫的所有當前連接] (http://stackoverflow.com/questions/11620/how-do-you-kill-all-current-connections-to-a-sql-server-2005-database) – zimdanen

回答

47

這應該斷開所有的人,並讓你作爲唯一的用戶:

alter database YourDb set single_user with rollback immediate 

注:請不要忘記

alter database YourDb set MULTI_USER 
完成之後,您可以使用

!通過Stev.org提供

+13

完成後別忘了'ALTER DATABASE YourDb SET MULTI_USER'! –

3

在這裏找到它: http://awesomesql.wordpress.com/2010/02/08/script-to-drop-all-connections-to-a-database/

DECLARE @dbname NVARCHAR(128) 
SET @dbname = 'DB name here' 
-- db to drop connections 
DECLARE @processid INT 
SELECT @processid = MIN(spid) 
FROM master.dbo.sysprocesses 
WHERE dbid = DB_ID(@dbname) 
WHILE @processid IS NOT NULL 
    BEGIN 
     EXEC ('KILL ' + @processid) 
     SELECT @processid = MIN(spid) 
     FROM master.dbo.sysprocesses 
     WHERE dbid = DB_ID(@dbname) 
    END 
+0

謝謝,但這個查詢花了2分鐘以上我剛取消它 – Haz

+1

恕我直言,這是一個不必要的複雜和無效的方式來做到這一點。 @ Andomar的答案以更少的努力完成了同樣的事情,並對飛行中的交易進行了更好的強力控制。它也不會捕獲具有* deprecated * sysprocesses視圖中報告的不同數據庫的用戶,但仍在當前數據庫上保留鎖定。 –

+3

謝謝亞倫,因爲我每週都感到羞恥。它殺死了這個過程,但肯定不是很優雅。 – Vinnie

2

完美解決方案: http://www.stev.org/post/2011/03/01/MS-SQL-Kill-connections-by-host.aspx

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[KillConnectionsHost]') AND type in (N'P', N'PC')) 
DROP PROCEDURE [dbo].[KillConnectionsHost] 
GO 


/****** Object: StoredProcedure [dbo].[KillConnectionsHost] Script Date: 10/26/2012 13:59:39 ******/ 

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE PROCEDURE [dbo].[KillConnectionsHost] @hostname varchar(MAX) 
AS 
    DECLARE @spid int 
    DECLARE @sql varchar(MAX) 

    DECLARE cur CURSOR FOR 
     SELECT spid FROM sys.sysprocesses P 
      JOIN sys.sysdatabases D ON (D.dbid = P.dbid) 
      JOIN sys.sysusers U ON (P.uid = U.uid) 
      WHERE hostname = @hostname AND hostname != '' 
      AND P.spid != @@SPID 

    OPEN cur 

    FETCH NEXT FROM cur 
     INTO @spid 

    WHILE @@FETCH_STATUS = 0 
    BEGIN 
     PRINT CONVERT(varchar, @spid) 

     SET @sql = 'KILL ' + RTRIM(@spid) 
     PRINT @sql 
     EXEC(@sql) 

     FETCH NEXT FROM cur 
      INTO @spid 
    END 

    CLOSE cur 
    DEALLOCATE cur 
GO 
+0

其相當長,但也唯一的解決方案爲我工作! thx – curiousBoy

13

還原嚮導單擊分離數據庫嚮導點擊

「到目標數據庫關閉現有連接」, 「丟棄連接」項目。

+0

該選項不總是可用的 – Squazz

1

您可以使用光標這樣的:

USE master 
GO 

DECLARE @SQL AS VARCHAR(255) 
DECLARE @SPID AS SMALLINT 
DECLARE @Database AS VARCHAR(500) 
SET @Database = 'AdventureWorks2016CTP3' 

DECLARE Murderer CURSOR FOR 
SELECT spid FROM sys.sysprocesses WHERE DB_NAME(dbid) = @Database 

OPEN Murderer 

FETCH NEXT FROM Murderer INTO @SPID 
WHILE @@FETCH_STATUS = 0 

    BEGIN 
    SET @SQL = 'Kill ' + CAST(@SPID AS VARCHAR(10)) + ';' 
    EXEC (@SQL) 
    PRINT ' Process ' + CAST(@SPID AS VARCHAR(10)) +' has been killed' 
    FETCH NEXT FROM Murderer INTO @SPID 
    END 

CLOSE Murderer 
DEALLOCATE Murderer 

我寫在我的博客在這裏:http://www.pigeonsql.com/single-post/2016/12/13/Kill-all-connections-on-DB-by-Cursor