杀掉相应数据库的进程

代码来源:http://www.cnblogs.com/LCX/archive/2008/12/03/1346924.html

 

Create Proc Sp_KillAllProcessInDB

@DbName VarChar(100)

as

if db_id(@DbName) = Null

begin

Print 'DataBase dose not Exist'

end

else

 

Begin

Declare @spId Varchar(30)

 

DECLARE TmpCursor CURSOR FOR

Select 'Kill ' + convert(Varchar, spid) as spId

from master..SysProcesses

where db_Name(dbID) = @DbName

and spId <> @@SpId

and dbID <> 0

OPEN TmpCursor

 

FETCH NEXT FROM TmpCursor

INTO @spId

 

WHILE @@FETCH_STATUS = 0

 

BEGIN

 

Exec (@spId)

 

FETCH NEXT FROM TmpCursor

INTO @spId

 

END

 

 

CLOSE TmpCursor

DEALLOCATE TmpCursor

 

end

 

GO

--To Execute

Exec dbo.Sp_KillAllProcessInDB 'DBname'

 

posted @ 2011-10-14 13:37  顿金  阅读(155)  评论(0编辑  收藏  举报