0
点赞
收藏
分享

微信扫一扫

SQL2005 查看与断开数据库的连接

贵州谢高低 2023-09-21 阅读 24


获取连接信息非常容易

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'

举报

相关推荐

0 条评论