SqlServer:此資料庫處於單使用者模式,導致資料庫無法刪除的處理
阿新 • • 發佈:2019-01-06
今天在刪除一個數據庫時,一直報錯,大意是:此資料庫處理單使用者模式,尚在連線當中,無法刪除(既使將SQLServer停止後再啟動也是如此)
百度之後找到了解決辦法,備份於此:
USE [master] GO /****** Object: StoredProcedure [dbo].[killspid] Script Date: 03/28/2011 11:01:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --建一個儲存過程,斷開所有使用者連線。 create proc [dbo].[killspid] (@dbname varchar(20)) as begin declare @sql nvarchar(500) declare @spid int set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''
[email protected]+''')' exec (@sql) open getspid fetch next from getspid into @spid while @@fetch_status<>-1 begin exec('kill '[email protected]) fetch next from getspid into @spid end close getspid deallocate getspid end GO
先在master中建立一個儲存過程,用於幹掉所有連線,然後呼叫
use
master
exec
killspid
'出問題的資料庫名'