断开指定数据库连接

来源:互联网 发布:开淘宝店如何找货源 编辑:程序博客网 时间:2024/05/17 21:56

数据丢失,

需要从备份的数据库中找数据(同一个数据库文件的备份),

执行完查询语句后,需要断开连接,在还原之前的备份

1、分离数据库(可以断开连接),新建数据库,在还原(全部覆盖)有点麻烦;

2、选中数据库,还原,执行完查询语句后,     断开连接,在还原再查询;



  /*  
  断开所有用户打开的连接  
  */  
  use   master  
  go  
   
  if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[p_killspid]')   and   OBJECTPROPERTY(id,   N'IsProcedure')   =   1)  
  drop   procedure   [dbo].[p_killspid]  
  GO  
   
  create   proc   p_killspid  
  @dbname   varchar(200) --要关闭进程的数据库名  
  as      
  declare   @sql     nvarchar(500)      
  declare   @spid   nvarchar(20)  
   
  declare   #tb   cursor   for  
  select   spid=cast(spid   as   varchar(20))   from   master..sysprocesses   where   dbid=db_id(@dbname)  
  open   #tb  
  fetch   next   from   #tb   into   @spid  
  while   @@fetch_status=0  
  begin      
  exec('kill   '+@spid)  
  fetch   next   from   #tb   into   @spid  
  end      
  close   #tb  
  deallocate   #tb  
  go  
   
  --用法      

  exec   p_killspid     '数据库名'


原文:http://blog.sina.com.cn/s/blog_4bf21642010007f0.html

原创粉丝点击