sqlserver"无法删除数据库,因为该数据库当前正在使用"问题解决

来源:互联网 发布:python product 编辑:程序博客网 时间:2024/04/30 17:36

 

执行脚本:

use   master  

  go  

   

  declare   @dbname   sysname  

  set   @dbname='databasename' --这个是要删除的数据库库名  

   

  declare   @s   nvarchar(1000)  

  declare   tb   cursor   local   for  

  select   s='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  

  exec('drop   database   ['+@dbname+']')  

 

源文档 <http://www.it118.org/specials/c9fba99e-4401-49cf-8256-ac3c1a34c0d9/12163346-d9be-4e81-920b-d191d312bda9.htm>

原创粉丝点击