1. 程式人生 > >如何終止SQL Server中的使用者程序(終止某一個使用者訪問sql server資料庫)

如何終止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 <

[email protected]
begin
set @spid=(select spid from master.dbo.sysprocesses where [email protected] 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 @
[email protected]
+ 1
end

end

if CHARINDEX(',',@param) <> 0
begin
set @tempvar [email protected]
 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 [email protected] 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

圖一

         圖一