获取SQL SERVER 当前连接,以及如何断开连接

获取连接信息非常容易

sp_who

 

断开连接使用:

kill pid

pid为连接信息中的ID

 

 

下面是断开指定库的所有用户连接的一个过程(在master数据库中进行) 
  use   master  
  go  
   
  if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[sp_KillSpid]')   and   OBJECTPROPERTY(id,   N'IsProcedure')   =   1)  
  drop   procedure   [dbo].[sp_KillSpid]  
  GO  
   
  create   proc   sp_KillSpid  
  @dbname   sysname     --要断开连接的数据库名  
  as      
  declare   @s   nvarchar(1000)  
  declare   tb   cursor   local    
  for  
  select   N'kill   '+cast(spid   as   varchar)  
  from   master..sysprocesses    
  where   dbid=db_id(@dbname)  
   
  open   tb    
  fetch   next   from   tb   into   @s  
  while   @@fetch_status=0  
  begin  
  exec(@s)  
  fetch   next   from   tb   into   @s  
  end  
  close   tb  
  deallocate   tb  
  go  
   
  --调用  
  exec   sp_KillSpid     'aa'


你可能感兴趣的:(sql,数据库,server,object,kill,Go)