如何终止SQL Server中的用户进程(终止某一个用户访问sql server数据库)

来源:互联网 发布:获取网页源码 编辑:程序博客网 时间:2024/06/03 06:37

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


一、情景:在很多情况下,往往会要求数据库管理员终止SQL Server中的用户进程。本文将为大家介绍如何创建一个简单的存储过程来实现同时终止多个会话、结束连续的会话和结束连接到数据库的所有会话等功能。

  在很多情况下,往往会要求数据库管理员终止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

 

图一

         图一


原创粉丝点击