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

后端 未结 7 1846
礼貌的吻别
礼貌的吻别 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:35

    I find this vastly faster and generally better than taking offline. Do read about it in MSDN so you understand the caveats. If using aysnc statistics, you have to turn those off, as well.

    -- set single user, terminate connections
    ALTER DATABASE [target] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    RESTORE ...
    ALTER DATABASE [target] SET MULTI_USER
    

    The "with rollback immediate" is the essential "termination" clause. Leaving it out waits forever. A nicer version of the above gives user transactions a few seconds to terminate.

    ALTER DATABASE [target] SET SINGLE_USER WITH ROLLBACK AFTER 5
    

    Offline is a good idea if you want to copy database files around, a scenario that can be handy in desktop editions of SQL. Too heavy for this scenario. If offline, this would be preferred. SQL is moving away from sp_dboption.

    ALTER DATABASE [target] SET OFFLINE WITH ROLLBACK AFTER 5
    

提交回复
热议问题