Thứ Hai, 12 tháng 12, 2011

SQL Script to kill all blocked processes

-- ******************************************** */
-- Description                                  */
-- ============                                 */
-- Script to kill all blocked processes         */
-- =============================================*/
-- Compatibility  :     2000+                   */
-- ******************************************** */
declare @max_count int, @count int, @sqlstring varchar(100)
declare @spid_table table (spid int NOT NULL)

INSERT @spid_table
select spid
from master.dbo.sysprocesses
where spid in (select blocked from master.dbo.sysprocesses where blocked <> 0) and blocked = 0

select @max_count = MAX(spid) FROM @spid_table
select top 1 @count = spid from @spid_table

while @count <= @max_count
begin
    select @sqlstring = 'kill ' + CONVERT(varchar(4), @count)
exec(@sqlstring)
print @sqlstring 

IF @count = @max_count
  begin
    break
  end
ELSE
  BEGIN
    select top 1 @count = spid FROM @spid_table where spid > @count
  end
end
 
This script will kill all blocked process inside the SQL Server instance 

Không có nhận xét nào:

Đăng nhận xét