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

后端 未结 14 2528
隐瞒了意图╮
隐瞒了意图╮ 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:31

    Here's a way I am doing database restore from production to development:

    NOTE: I am doing it via SSAS job to push production database to development daily:

    Step1: Delete previous day backup in development:

    declare @sql varchar(1024);
    
    set @sql = 'DEL C:\ProdAEandAEXdataBACKUP\AE11.bak'
    exec master..xp_cmdshell @sql
    

    Step2: Copy production database to development:

    declare @cmdstring varchar(1000)
    set @cmdstring = 'copy \\Share\SQLDBBackup\AE11.bak C:\ProdAEandAEXdataBACKUP'
    exec master..xp_cmdshell @cmdstring 
    

    Step3: Restore by running .sql script

    SQLCMD -E -S dev-erpdata1 -b -i "C:\ProdAEandAEXdataBACKUP\AE11_Restore.sql"
    

    Code that is within AE11_Restore.sql file:

    RESTORE DATABASE AE11
    FROM DISK = N'C:\ProdAEandAEXdataBACKUP\AE11.bak'
    WITH MOVE 'AE11' TO 'E:\SQL_DATA\AE11.mdf',
    MOVE 'AE11_log' TO 'D:\SQL_LOGS\AE11.ldf',
    RECOVERY;
    
    0 讨论(0)
  • 2020-12-12 18:32

    I got this error when there was not enough disk space to restore Db. Cleaning some space solved it.

    0 讨论(0)
提交回复
热议问题