微软大佬提供的一个批量 kill会话的脚本,很凶很暴力,慎用慎用慎用!尤其是涉及大事务时。
請注意:如果資料庫還在正在 Recovery 階段無效,因為無法 Kill 系統 Session,必須等候 Recovery 完畢(或是重建交易紀錄檔案)。
/* Function : Kill all SPID on specific database and Repair Written by Ray Yen 2010/04/09 Modified by Ray Yen 2018/11/20 */ DECLARE @DBID INT DECLARE @DATABASENAME NVARCHAR(60) SET @DATABASENAME =[Database_Name] --Please modify the database name Select Top 1 @DBID = DBID from sys.sysdatabases Where Name = @DATABASENAME PRINT 'Database Name : ' + @DATABASENAME PRINT 'Database ID : ' + LTRIM(STR(@DBID)) PRINT '---------------------------------------' DECLARE @nKillProcess INT DECLARE @nFetchStatus INT DECLARE @sTemp NVARCHAR(255) DECLARE curProcesses CURSOR LOCAL FAST_FORWARD READ_ONLY FOR SELECT spid FROM Master..sysprocesses WHERE dbid = @DBID OPEN curProcesses FETCH NEXT FROM curProcesses INTO --Gets the first process @nKillProcess SET @nFetchStatus = @@FETCH_STATUS --Kill the processes WHILE @nFetchStatus = 0 BEGIN SET @sTemp ='KILL ' + CAST(@nKillProcess as varchar(5)) PRINT @sTemp EXEC(@sTemp) FETCH NEXT FROM curProcesses INTO --Gets the next process @nKillProcess SET @nFetchStatus = @@FETCH_STATUS END CLOSE curProcesses DEALLOCATE curProcesses
标签:curProcesses,--,DBID,SQLServer,会话,kill,DATABASENAME,DECLARE,nFetchStatus From: https://www.cnblogs.com/wang-xiaohui/p/18282414