自动终止某些特定任务的会话

Automatically kill session of some specific task

在某些报告生成中,我们在 SQL 服务器中遇到死锁问题,所以我能做的是

select * 
from sys.sysprocesses 
where dbid = db_id() 
  and spid <> @@SPID 
  and blocked <> 0  
  and lastwaittype LIKE 'LCK%'

dbcc inputbuffer (SPID from above query result)

dbcc inputbuffer (blocked from above query result)

如果 EventInfo 列包含 'mytext',我想通过

终止该部分
Kill 53  

53 不是 SPID 或在我看到我想终止其连接的特定文本时被阻止

我想在出现死锁并找到特定单词时自动执行此过程,从而终止这些会话。没有用户间隔或操作。

有时我使用这个旧查询来删除具有特定描述的会话:

declare @t table (sesid int)

--Here we put all sessionid's with specific description into temp table
insert into @t 
select spid
from sys.sysprocesses 
where dbid = db_id() 
  and spid <> @@SPID 
  and blocked <> 0  
  and lastwaittype LIKE 'LCK%'

DECLARE @n int,
        @i int= 0,
        @s int,
        @kill nvarchar(20)= 'kill ',
        @sql nvarchar (255)

SELECT @n = COUNT(*) FROM @t
--Here we execute `kill` for every sessionid from @t in while loop
WHILE @i < @n
BEGIN 
    SELECT TOP 1 @s = sesid from @t
    SET @sql = @kill + cast(@s as nvarchar(10))

    --select @sql
    EXECUTE sp_executesql @sql

    delete from @t where sesid = @s
    SET @i = @i + 1
END

从上面的答案,我根据自己的要求进行更改。

/*drop table #inputbuffer 
 --First Time create Table 

create table #inputbuffer
(eventType varchar(255) ,
parameters int ,
procedureText varchar(255),
spid varchar(6))
*/
declare @spid varchar(6)
declare @sql varchar(50)

declare sprocket cursor fast_forward for 
select spid  from SYS.sysprocesses
where dbid =db_id() and spid <> @@SPID and blocked <>0 and lastwaittype LIKE 'LCK%'
union all 
select blocked  from SYS.sysprocesses
where dbid =db_id() and spid <> @@SPID and blocked <>0 and lastwaittype LIKE 'LCK%'

open sprocket
fetch next from sprocket into
@spid

while @@fetch_status = 0
    begin
        set @sql = 'dbcc inputbuffer(' + @spid + ')'
        insert into #inputbuffer(eventType, parameters, procedureText)
        exec (@sql)

        update #inputbuffer
            set spid = @spid
            where spid is null

        fetch next from sprocket into
        @spid
    end

close sprocket
deallocate sprocket

if @@cursor_rows <> 0
    begin 
        close sprocket
        deallocate sprocket
end

DELETE from #inputbuffer  where procedureText NOT like '%SUBLED%'

select spid, eventType, parameters, procedureText from #inputbuffer  

DECLARE @n int,
        @i int= 0,
        @s int,
        @kill nvarchar(20)= 'kill ',
        @sqln nvarchar (255)

SELECT @n = COUNT(*) FROM #inputbuffer

WHILE @i < @n
BEGIN 
    SELECT TOP 1 @s = spid from #inputbuffer 
    SET @sqln = @kill + cast(@s as nvarchar(10))
    select @sqln
    EXECUTE sp_executesql @sqln
    delete from #inputbuffer where spid = @s
    SET @i = @i + 1
END

这是终止所有会话的更简洁的查询 运行 具体 sql。

DECLARE @kill varchar(8000); SET @kill = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), session_id) + ';'
FROM sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle)
WHERE database_id = db_id('dldb') and session_id <> @@SPID 
    and text like '%FROM dl2ResultsHeaderItemSubTree%'
EXEC(@kill);

自 SQL Server 2005 起有效。

合并自 List the queries running on SQL Server and Script to kill all connections to a database (More than RESTRICTED_USER ROLLBACK)