[ 登录 ][ 注册 ] 天极传媒: 比特网 | 天极网 | IT专家网 | 52PK游戏网 | 手机天极 | IT分众 |
您现在的位置: IT专家网 > 数据库子站 > 数据库技巧

如何终止SQL Server中的用户进程

作者: cyw,  出处:IT专家网, 责任编辑: 李书琴, 
2008-09-11 11:31
  在很多情况下,往往会要求数据库管理员终止SQL Server中的用户进程。本文将为大家介绍如何创建一个简单的存储过程来实现同时终止多个会话、结束连续的会话和结束连接到数据库的所有会话等功能。

  【IT专家网独家】

  在很多情况下,往往会要求数据库管理员终止SQL Server中的用户进程,例如在停止某个数据库的运作时,或者还原数据库之前,或者长时间运行活动事务等情况下。数据库管理员通常会使用SQL Server中提供的“KILL”命令来完成任务。

  但是,SQL Server提供的“KILL”命令灵活性不够,不能在一次性结束多个会话,一次只能解决掉一个会话。本文将为大家介绍如何创建一个简单的存储过程来实现同时终止多个会话、结束连续的会话和结束连接到数据库的所有会话等功能。

  首先,我们在主数据库中创建“KILL2”这个进程,代码如下所示(参考图一):

USE [master]
GO
IF  EXISTS (SELECT * FROM master.dbo.sysobjects
    WHERE id = OBJECT_ID(N'[kill2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[kill2]
GO
--Usage1: Kill2 '51-57' --> Kills all the session IDs from 51 to 57
--Usage2: Kill2 '58'  --> Kills the session IDs 58
--Usage3: Kill2 '51,56,100,58' 
    --> Kills the session IDs 51,56,100 and 58
--Usage4: Kill2 'DB=MyDatabase' 
    --> Kills all the session IDs that are connected
   to the database "MyDatabase"

use master
go
set concat_null_yields_null off
go
create procedure kill2 @param2 varchar(500)
as
--declare @param2 varchar(500)
declare @param varchar(500)
declare @startcount int
declare @killcmd varchar(100)
declare @endcount int
declare @spid int
declare @spid2 int
declare @tempvar varchar(100)
declare @tempvar2 varchar(100)
--set @param2 ='54'
set @param=REPLACE(@param2,' ','')
if CHARINDEX('-',@param) <> 0
begin
select @startcount= convert(int,SUBSTRING(@param,1,charindex('-',@param)-1))
select @endcount=convert(int,SUBSTRING(@param,charindex('-',@param)+1,(LEN(@param)-charindex('-',@param))))
print 'Killing all SPIDs from ' + convert(varchar(100),@startcount)+' to ' +convert(varchar(100),@endcount)
while @startcount <=@endcount
begin
set @spid=(select spid from master.dbo.sysprocesses where spid=@startcount and spid>50)
if @spid = @startcount
begin
print 'Killing '+convert(varchar(100),@startcount)
set @killcmd ='Kill '+convert(varchar(100),@startcount)
exec(@killcmd)
end
else
begin
Print 'Cannot kill the SPID ' +convert(varchar(100),@startcount) + ' because it does not Exist'
end
set @startcount=@startcount + 1
end

end

if CHARINDEX(',',@param) <> 0
begin
set @tempvar =@param
 while charindex(',',@tempvar ) <> 0
 begin
 SET @tempvar2=left(@tempvar,charindex(',',@tempvar)-1)
 set @spid=(select spid from master.dbo.sysprocesses where spid=CONVERT(varchar(100),@tempvar2) and spid>50)
 if @spid = CONVERT(varchar(100),@tempvar2)
  begin
  print 'Killing '+CONVERT(varchar(100),@tempvar2)
  set @killcmd='Kill '+CONVERT(varchar(100),@tempvar2)
  exec (@killcmd)
 
  end
  else
  begin
  Print 'Cannot kill the SPID ' +CONVERT(varchar(100),@tempvar2) + ' because it does not Exist'
  end
 set @tempvar =REPLACE(@tempvar,left(@tempvar,charindex(',',@tempvar)),'')
 end
 set @spid=(select spid from master.dbo.sysprocesses where spid=CONVERT(varchar(100),@tempvar) and spid>50)
 if @spid = CONVERT(varchar(100),@tempvar)
  begin
  print 'Killing '+CONVERT(varchar(100),@tempvar)
  set @killcmd='Kill '+CONVERT(varchar(100),@tempvar)
  exec (@killcmd)
 
  end
  else
  begin
  Print 'Cannot kill the SPID ' +CONVERT(varchar(100),@tempvar) + ' because it does not Exist'
  end
end

if CHARINDEX('=',@param2) <>0
begin
print 'Killing all the SPIDs that are connected to the database '+RIGHT(@param2,(len(@param2)-3))
declare dbcursor 
 cursor forward_only for select SPID from master.dbo.sysprocesses where DB_NAME(dbid) = RIGHT(@param2,(len(@param2)-3))
open dbcursor
fetch dbcursor into @spid
while @@FETCH_STATUS =0
begin
 set @spid2=(select spid from master.dbo.sysprocesses where spid=@spid and spid>50)
 if @spid = @spid2  begin
  print 'Killing '+CONVERT(varchar(100),@spid2)
  set @killcmd='Kill '+CONVERT(varchar(100),@spid2)
  exec (@killcmd)
 
  end
  else
  begin
  Print 'Cannot kill the SPID ' +CONVERT(varchar(100),@spid2) + ' because it does not Exist'
  end

fetch dbcursor into @spid
end
close dbcursor
deallocate dbcursor

end

if CHARINDEX('-',@param)=0 and CHARINDEX(',',@param) = 0  and CHARINDEX('=',@param)=0
begin
 set @spid=(select spid from master.dbo.sysprocesses where spid=CONVERT(varchar(100),@param) and spid>50)
 if @spid = CONVERT(varchar(100),@param)
  begin
  print 'Killing '+CONVERT(varchar(100),@param)
  set @killcmd='Kill '+CONVERT(varchar(100),@param)
  exec (@killcmd)
 
  end
  else
  begin
  Print 'Cannot kill the SPID ' +CONVERT(varchar(100),@param) + ' because it does not Exist'
  end

end
go
--kill2 '51'
--go
--kill2 '51-56'
--go
--kill2 '56,57,58,52'
--go
--kill2 'db=AdventureWorks2008'
--kill2 'db=My Database'
--go
--sp_who

 

图一

         图一

共4页。 1 2 3 4 :

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。

邮件订阅