2

Got a really strange issue with SQL Server database backup files I am getting backup files from the state of California for petroleum production. Each year a database with the same name is created for the data for that year The database is always named, WellProductionInjection. Each year the same tables with the same contents are created and populated. The only difference is that the table names contain the year, so separate tables as supposed to exist for each year. I can get what is labelled as a full backup file for a single year. But when I try to use SQL Server Management Studio to restore all the backup files to a single database, I get only the data for one year, or an error about the database already existing. Any tips on how deal deal with merging multiple backups into a single database? Examples of the backup files can be put on the web if anyone is willing to try their luck with this.

asked Apr 19, 2021 at 1:42

2 Answers 2

5

SQL server doesn't have any built-in capacity to merge multiple backups into a single database, you'll have to restore each backup with a different database name (like WellProductionInjection_2019 and WellProductionInjection_2020), then do the work yourself to combine them however you need.

Once the databases are restored, you can combine them using a variety of tools; the simplest one is called the "Import/Export Wizard". You can use this to copy entire tables from one database to another (if all tables have different names), or to append the data from table in one database to the tables in the other (if you actually want to combine the data into the same table).

answered Apr 19, 2021 at 2:02
2
  • I was afraid that I would have to jump through hoops on this one. At least I figured out how to rename the databases as they are restored. Thanks for the help. Commented Apr 19, 2021 at 15:41
  • @BartT. Yep, SQL wouldn't have any idea how you actually want to combine the DBs, for example maybe it would be more useful for your needs to combine the data from separate years (with different table names) into a single master rollup table. But don't worry, the import/export wizard lets you do mass object copies from one database to another, so that would be my recommended approach. Commented Apr 19, 2021 at 17:01
1

There is a fairly simple way that I use for a similar task:

  1. Restore a backup under a different name. For example, WellProductionInjection_new_data

enter image description here

  1. Right-click on WellProductionInjection_new_data -> Task -> Generate Script enter image description here

  2. In the advanced options specify

     Script USE DATABASE = False
     Types of data to script = Data Only
    

enter image description here

  1. Save the script to a .sql file

  2. Import data. Open Command Prompt and enter a command like this:

    sqlcmd -d WellProductionInjection < C:\Users\User\Documents\script.sql

Where:

WellProductionInjection - the database in which you aggregate data

C:\Users\User\Documents\script.sql - the path to the script that you specified in step 2.

The described method will import only data with new PK, and this method will only work if the schema database does not change.

If desired, the algorithm can be fully automated; to automate the second step, use https://github.com/microsoft/mssql-scripter

answered Apr 26, 2021 at 11:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.