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.
2 Answers 2
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).
-
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.BartAtRanch– BartAtRanch2021年04月19日 15:41:09 +00:00Commented 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.BradC– BradC2021年04月19日 17:01:45 +00:00Commented Apr 19, 2021 at 17:01
There is a fairly simple way that I use for a similar task:
- Restore a backup under a different name. For example, WellProductionInjection_new_data
Right-click on WellProductionInjection_new_data -> Task -> Generate Script enter image description here
In the advanced options specify
Script USE DATABASE = False Types of data to script = Data Only
Save the script to a .sql file
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