漫漫技术人生路

C#

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  234 随笔 :: 0 文章 :: 30 评论 :: 8 引用

公告

 无法分离数据库"DataBase0",因为它当前正在使用。是什末原因呢?
/*  
  关闭用户打开的进程,再分离即可  
  */  
  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     '数据库名'  
posted on 2009-10-15 13:13 javaca88 阅读(298) 评论(0) 编辑 收藏
(评论功能已被博主禁用)