Skip to content
Advertisement

Replace Database WIth Copy Of Other Database

As part of our testing process, I’m trying to automate the following:

  1. Drop DatabaseB.
  2. Create a backup of DatabaseA
  3. Recreate DatabaseB from the backup of DatabaseA

I had hoped something like this would work RESTORE DATABASE DatabaseB FROM DISK = 'c:tempDatabaseA.bak' WITH REPLACE, but that turns out to want to overwrite the existing ldf and mdf files for DatabaseA.

It seems that Azure’s SQL Server has this functionality: CREATE DATABASE DatabaseB AS COPY OF DatabaseA, but this isn’t supported on our on-prem servers.

Is there a simple way (and clean) way to do this?

Advertisement

Answer

Based on feedback from @Larnu and more research, here’s where I ended up:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement