how to combine data from 2 different database into one for sql?

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
I have a lot of data to work with and for whatever reason, half of my tables and data are in database A. The other half are in Database B.

how do I add on the data from B to A? Database A is completed. Do I just copy the code from A, modify it to fit B, and paste it back into the queries for A?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,698
4,659
75
Most SQL databases allow dumping their data, and restoring it later. Maybe you could dump one or both databases and combine them?
 

maluckey

Platinum Member
Jan 31, 2003
2,933
0
71
Not sure what you are trying to accomplish. If you're trying to merge databases, as alredy mentioned, a database dump is the way to start.

In my case, it's business dictated that certain data is in certain boxes. As the solution, we use CMS (available in SQL Server 2008-present) to query against the multiple data sources.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
There are a whole bunch of ways to do this. The specific mechanics depend entirely on which server you'e using, and so posting your query without giving that information was a waste of your time. Not a waste of my time, because I was just reading this board, but definitely a waste of yours.
 

Scarpozzi

Lifer
Jun 13, 2000
26,392
1,780
126
You can export tables and simply import them into the DB you want to keep. I'm sure someone can google it for you if you tell us what you're using. MySQL, MSSQL, Oracle, PostgreSQL, etc...

If you're worried about corrupting data, you can always try cloning one of the DBs before you start and name it what you want as your final name....then export tables from DB2 and import them back in....as long as there are no collisions in naming conventions, you should be fine.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
Thanks for the replies. I'm using Microsoft sql server. So more specifically, let's say database A has data for 10 u.s states. Database b has data for the remaining states but the data are in different tables when compared to A. Database A has a fully functional query that executes fine and loads the tables that I need. Basically, I want to add the info from B onto A.

Right now, I'm copying the query from A onto a blank sheet,and am just adjusting the code to find the right data from B. Would that be the simplist way to do it? Once I'm done, can I just add the B code over to the the query for A and 'insert?'

S
 

Sgraffite

Senior member
Jul 4, 2001
202
139
116
You can insert into A while selecting from B:

Code:
INSERT INTO [A]
([Column1], [Column2])
SELECT [Column1], [Column2]
FROM [B]
 

maluckey

Platinum Member
Jan 31, 2003
2,933
0
71
I'm using Microsoft sql server. So Right now, I'm copying the query from A onto a blank sheet,and am just adjusting the code to find the right data from B. Would that be the simplist way to do it? Once I'm done, can I just add the B code over to the the query for A and 'insert?'

S

If this is what you are doing, and not trying to physically merge the two boxes, just use CMS (already in SQL Server), and write ONE query and be done.

If you need a flat table for some reason at the end of the procedure, you can load it to a temp table or a view at the end and do your processing from there.

M
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
Thanks. So right now, what I'm trying to achieve is to update the report once, and have it all compiled in one table.
Let's say I update the report weekly. The current situation is that I would open up the query on table 1, update the date, review it in excel. For table 2, I would update the query and review it in excel. Since it is in 2 separate sheets, I can't do an overall analysis on the report .

Ideally, I want everything to be merged together, I update once, and everything is in one table.
 

Sgraffite

Senior member
Jul 4, 2001
202
139
116
You could possibly make a view that selects from both tables. Or you could set update triggers on A and B to updated a third table which contains all of the data, essentially a materialized view.
 

sao123

Lifer
May 27, 2002
12,653
205
106
this sounds like a process that needs ETL?
have you checked into SQL Server Integration Services? (SSIS)
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
I would create a linked server on database server A to database server B. Creating a Linked Server

Then you could either just query from both databases in one query:
Code:
SELECT *
FROM myTable a
INNER JOIN [databaseServerB].DatabaseName.dbo.myOtherTable b on a.someField = b.someField

Or copy the data from database B and then you'd just query database A:
Code:
INSERT INTO a (field1, field2)
SELECT b.field1, b.field2
FROM myTable a
INNER JOIN [databaseB].DatabaseName.dbo.myOtherTable b on a.someField = b.someField

I've also used the "Import Data" feature in SQL Server. You'd need to create the Linked Server first, I think, and then on database A you can right-click the database you want to import data into and select "Tasks" -> "Import Data..." You can Google how to do it, or follow the steps, but basically you write a SQL query that selects the data in database B and use it to copy the data into database A.