WITH CTE_SID ( BSID, SID, sql_handle )
AS ( SELECT blocking_session_id ,
session_id ,
sql_handle
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0
UNION ALL
SELECT A.blocking_session_id ,
A.session_id ,
A.sql_handle
FROM sys.dm_exec_requests A
JOIN CTE_SID B ON A.SESSION_ID = B.BSID
)
SELECT C.BSID ,
C.SID ,
S.login_name ,
S.host_name ,
S.status ,
S.cpu_time ,
S.memory_usage ,
S.last_request_start_time ,
S.last_request_end_time ,
S.logical_reads ,
S.row_count ,
q.text
into #tmp
FROM CTE_SID C
JOIN sys.dm_exec_sessions S ON C.sid = s.session_id
CROSS APPLY sys.dm_exec_sql_text(C.sql_handle) Q
ORDER BY sid
select * from #tmp
---Kill Lock
DECLARE @UserId varchar(50)
DECLARE My_Cursor CURSOR
FOR (SELECT SID FROM #tmp)
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor INTO @UserId;
WHILE @@FETCH_STATUS = 0
BEGIN
exec('kill '+@UserId)
FETCH NEXT FROM My_Cursor INTO @UserId;
END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标
drop table #tmp