天天看點

"無法删除資料庫,因為該資料庫目前正在使用"問題解決

異常處理彙總-資料庫系列   http://www.cnblogs.com/dunitian/p/4522990.html

 以前剛學資料庫的時候比較苦惱這個問題,今天删除的時候又看見了,正好一起記錄一下:

圖形化方法:

"無法删除資料庫,因為該資料庫目前正在使用"問題解決
指令行方法:

use master    
go   
     
declare @dbname sysname    
set @dbname = 'BigValuesTest' --這個是要删除的資料庫庫名    
     
declare @s nvarchar(1000)    
declare tb cursor local   
for  
    select s = 'kill   ' + cast(spid as varchar)  
    from   master.dbo.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 + ']')   
      

這個原理類似于作業系統裡面通過pid幹掉程式了

"無法删除資料庫,因為該資料庫目前正在使用"問題解決

作者:

毒逆天

出處:

https://www.cnblogs.com/dotnetcrazy

打賞:

18i4JpL6g54yAPAefdtgqwRrZ43YJwAV5z

本文版權歸作者和部落格園共有。歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接!