2010年11月4日 星期四

一次刪光獨佔存取的spid

當要復原資料庫時,有些連線還在使用中是無法復原,會出現無法獲得獨佔存取權的訊息。在SQL Server 中只要在活動監視器裡看到有占用的spid刪除即可但這需一個個的刪除,似乎有點麻煩。


因此只需執行下面的語法修改資料庫的名稱即可。
語法如下:

use master
declare @spid int
declare getspid cursor for select spid from sysprocesses where dbid=db_id('資料庫名稱')
open getspid
fetch next from getspid into @spid
while @@FETCH_STATUS<>-1
    begin
        exec('kill '+@spid)
        fetch next from getspid into @spid
    end
close getspid
deallocate getspid

這樣有連結到該資料庫的所有spid皆一併刪除。

沒有留言:

張貼留言