sql tsql__kill_db_connections.sql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql tsql__kill_db_connections.sql相关的知识,希望对你有一定的参考价值。

/*** KILL ALL CONNECTIONS TO  DATABASE ***/

USE master
GO

DECLARE @dbName nvarchar(50) = 'DATABASE_NAME'

DECLARE @KillSpid INT
	,@CMD NVARCHAR(50);

SET @CMD = '';

DECLARE cur_kill CURSOR
FOR
SELECT s.session_id
FROM sys.dm_exec_sessions s
JOIN sys.databases d ON d.database_id = s.database_id
WHERE d.name = @dbName

OPEN cur_kill

FETCH NEXT
FROM cur_kill
INTO @KillSpid;

WHILE @@FETCH_STATUS = 0
BEGIN
	SET @CMD = 'BEGIN TRY KILL ' + CAST(@KillSpid AS VARCHAR(3)) + ' END TRY BEGIN CATCH END CATCH'

	EXECUTE sp_executesql @CMD

	PRINT CAST ( @KillSpid  as Varchar(3)) +  ' SPID KILLED '
	FETCH NEXT
	FROM cur_kill
	INTO @KillSpid
END
CLOSE cur_kill
DEALLOCATE cur_kill

以上是关于sql tsql__kill_db_connections.sql的主要内容,如果未能解决你的问题,请参考以下文章

sql tsql__bulk_insert.sql

sql tsql__get_schema.sql

sql tsql__template_create_database.sql

sql tsql__SQLCMD_mode_check.sql

sql tsql__get_lock_history.sql

sql tsql__kill_idle_session.sql