SQL-Server: Error - Exclusive access could not be obtained because the database is in use

后端 未结 14 2490
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 17:45

I am actually trying to make a script (in Sql Server 2008) to restore one database from one backup file. I made the following code and I am getting an error -



        
相关标签:
14条回答
  • 2020-12-12 18:19

    I experienced this issue when trying to restore a database on MS SQL Server 2012.

    Here's what worked for me:

    I had to first run the RESTORE FILELISTONLY command below on the backup file to list the logical file names:

    RESTORE FILELISTONLY 
        FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\my_db_backup.bak'
    

    This displayed the LogicalName and the corresponding PhysicalName of the Data and Log files for the database respectively:

    LogicalName      PhysicalName               
    com.my_db        C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db.mdf
    com.my_db_log    C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db_log.ldf
    

    All I had to do was to simply replace the LogicalName and the corresponding PhysicalName of the Data and Log files for the database respectively in my database restore script:

    USE master;
    GO
    
    ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    GO
    
        
    RESTORE DATABASE my_db
        FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\my_db_backup.bak'
        WITH REPLACE,
        MOVE 'com.my_db' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db.mdf',
        MOVE 'com.my_db_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.my_db_log.ldf'
    GO
        
    ALTER DATABASE my_db SET MULTI_USER;
    GO
    

    And the Database Restore task ran successfully:

    That's all.

    I hope this helps

    0 讨论(0)
  • 2020-12-12 18:21

    taking original db to offline worked for me

    0 讨论(0)
  • 2020-12-12 18:25

    execute this query before restoring database:

    alter database [YourDBName] 
    set offline with rollback immediate
    

    and this one after restoring:

      alter database [YourDBName] 
      set online
    
    0 讨论(0)
  • 2020-12-12 18:26
    1. Set the path to restore the file.
    2. Click "Options" on the left hand side.
    3. Uncheck "Take tail-log backup before restoring"
    4. Tick the check box - "Close existing connections to destination database".
    5. Click OK.
    0 讨论(0)
  • 2020-12-12 18:27

    I'll assume that if you're restoring a db, you don't care about any existing transactions on that db. Right? If so, this should work for you:

    USE master
    GO
    
    ALTER DATABASE AdventureWorksDW
    SET SINGLE_USER
    --This rolls back all uncommitted transactions in the db.
    WITH ROLLBACK IMMEDIATE
    GO
    
    RESTORE DATABASE AdventureWorksDW
    FROM ...
    ...
    GO
    

    Now, one additional item to be aware. After you set the db into single user mode, someone else may attempt to connect to the db. If they succeed, you won't be able to proceed with your restore. It's a race! My suggestion is to run all three statements at once.

    0 讨论(0)
  • 2020-12-12 18:30

    I think you just need to set the db to single user mode before attempting to restore, like below, just make sure you're using master

    USE master
    GO
    ALTER DATABASE AdventureWorksDW
    SET SINGLE_USER
    
    0 讨论(0)
提交回复
热议问题