When restoring a backup, how do I disconnect all active connections?

后端 未结 10 1025
失恋的感觉
失恋的感觉 2020-12-22 15:26

My SQL Server 2005 doesn\'t restore a backup because of active connections. How can I force it?

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 15:52

    This code worked for me, it kills all existing connections of a database. All you have to do is change the line Set @dbname = 'databaseName' so it has your database name.

    Use Master
    Go
    
    Declare @dbname sysname
    
    Set @dbname = 'databaseName'
    
    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
    

    after this I was able to restore it

提交回复
热议问题