我想關閉與MS SQL Server的現有連接,以便可以以編程方式對該數據庫執行還原。如何關閉與數據庫的現有連接
29
A
回答
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
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
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
相關問題
- 1. 關閉數據庫連接
- 2. 關閉連接數據庫
- 3. 關閉數據庫連接
- 4. 關閉數據庫連接
- 5. 關閉數據庫連接
- 6. CodeIgniter數據庫連接沒有關閉
- 7. 數據庫連接沒有關閉
- 8. PHP沒有關閉與數據庫的連接(odbc)
- 9. 企業庫數據庫連接關閉
- 10. 關閉打開的數據庫連接?
- 11. 與關閉數據庫與servlet連接的問題
- 12. TcpInboundGateway:如何關閉與遠程客戶端的現有連接
- 13. 關閉和打開數據庫連接?
- 14. entitymanager.close()不關閉連接到數據庫
- 15. laravel 4關閉數據庫連接
- 16. Node.js關閉數據庫連接
- 17. Play Framework:數據庫連接池關閉
- 18. 數據庫連接不關閉
- 19. C#關閉數據庫連接
- 20. 關閉數據庫連接()內finally塊
- 21. 數據庫連接自動關閉
- 22. asp.net; repeater.databind和數據庫連接關閉
- 23. Oracle數據庫:java.sql.SQLException:關閉連接
- 24. 關閉DataMapper數據庫連接
- 25. 數據庫連接不關閉
- 26. 要關閉或不關閉數據庫中的連接
- 27. 如何關閉數據源mysql連接
- 28. 使用python中的happybase關閉與hbase數據庫的連接
- 29. SSMS「刪除數據庫」上的「關閉現有連接」有什麼作用?
- 30. 如何關閉PostgreSQL數據庫上的空閒連接
可能重複:[?你如何殺死一個SQL Server 2005數據庫的所有當前連接] (http://stackoverflow.com/questions/11620/how-do-you-kill-all-current-connections-to-a-sql-server-2005-database) – zimdanen