How to gain exclusive access to SQL Server 2005 database to restore?

后端 未结 7 1849
礼貌的吻别
礼貌的吻别 2021-01-31 05:21

Whenever I restore a backup of my database in SQL Server I am presented with the following error:

Msg 3101, Level 16, State 1, Line 1
Exclusive access could not          


        
7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 05:34

    @mattlant - that's what I was looking for. I bring it over here so it's in the thread.

    Use Master
    Go
    
    Declare @dbname sysname
    
    Set @dbname = 'name of database you want to drop connections from'
    
    Declare @spid int
    Select @spid = min(spid) from master.dbo.sysprocesses
    where dbid = db_id(@dbname)
    While @spid Is Not Null
    Begin
            Execute ('Kill ' + @spid)
            Select @spid = min(spid) from master.dbo.sysprocesses
            where dbid = db_id(@dbname) and spid > @spid
    End
    

提交回复
热议问题