還原資料庫出現“未獲得排他訪問”解決方案(殺死資料庫連接的預存程序sqlserver),預存程序sqlserver

來源:互聯網
上載者:User

還原資料庫出現“未獲得排他訪問”解決方案(殺死資料庫連接的預存程序sqlserver),預存程序sqlserver

在master資料庫下建立預存程序如下:

createproc killspid (@dbnamevarchar(20))
as
begin
declare@sqlnvarchar(500)
declare@spidint
set@sql='declare getspid cursor for select spid from sysprocesses where dbid in (select dbid from sysdatabases where name=' +@dbname+' )'
exec(@sql)
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
end
--用法

use master
exec killspid  '''資料庫名'''

 

單引號嵌套

用三個單引號的作用,最外層的單引號表示vchar,第二個單引號是轉義,最裡面的單引號是字串中的單引號;declare @dbname nvarchar(500)

set @dbname='''zcc''' --得到的變數為'zcc',set @dbname='zcc'   --得到的變數是zcc

 


還原資料庫時,提示:因為資料庫正在使用,所以未可以獲得對資料庫的排他訪問權

說明確實是正在被使用.關閉服務運行.在編寫時候.只開啟編輯介面.
 
用SQL Server2000還原資料庫,提示資料庫正在使用,未可以獲得對資料庫的排他訪問權,資料庫不可以離線

分離資料庫後試試看。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.