您好,欢迎访问一九零五行业门户网

终止SQL Server中的用户进程方法

一、情景:在很多情况下,往往会要求数据库管理员终止sql server中的用户进程。本文将为大家介绍如何创建一个简单的存储过程来实现同时终止多个会话、结束连续的会话和结束连接到数据库的所有会话等功能。 在很多情况下,往往会要求数据库管理员终止sql serv
一、情景:在很多情况下,往往会要求数据库管理员终止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 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
其它类似信息

推荐信息