天天看點

殺死鎖和程序

如何去手動的殺死程序和鎖?最簡單的辦法,重新啟動服務。但是這裡要介紹一個存儲過程,通過顯式的調用,可以殺死程序和鎖。

use master

go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[p_killspid]

GO

create proc p_killspid

@dbname varchar(200)    --要關閉程序的資料庫名

as 

    declare @sql  nvarchar(500) 

    declare @spid nvarchar(20)

    declare #tb cursor for

        select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)

    open #tb

    fetch next from #tb into @spid

    while @@fetch_status=0

    begin 

        exec('kill '[email protected])

        fetch next from #tb into @spid

    end 

    close #tb

    deallocate #tb

go

--用法 

exec p_killspid  'newdbpy'