views:

966

answers:

4

Hi I have two data tables say:

Table 1:

name  age
----  ---
Bob   40

Table 2:

auto kids
---- ----
3     3

I want to merge the two tables to get something like

name age auto kids
---  --- --- ----
Bob  40   3   3

Is this possible? I tried merge but it does not seem to be working, thanks.

A: 

Merge will only combine the data of tables with similar schema.

You will probably have to iterate through each table and make a 3rd table that has all the columns you want.

Also, I dont see any kind of relationship key here. If you had that, you could make a data relation and not need to even merge them.

StingyJack
A: 

how do these tables relate? What is the key between them

select * from table1 t1
join table2 t2 on  <some magic where clause here>
SQLMenace
+1  A: 

I'm going to take a shot at adding to the previous two answers in the hopes of making it clearer for you in case it's not. What Stingy and Menace are wondering why the two tables you speak of don't look something like this:

Person
PersonID (primary key)
FirstName
LastName
Age

Demographics
DemographicsID (primary key)
PersonID (foreign key)
Autos
Kids

... if the two tables looked like this then you could combine them by adding a few columns to the person table so that it looked like this:

Person
PersonID (primary key)
FirstName
LastName
Age
Autos
Kids

... then executing a query like this:

UPDATE Person p, Demographics d
SET
  p.Autos = d.Autos
  p.Kids = d.Kids
WHERE
  p.PersonID = d.PersonID

In the example above, without the PersonID field in both the Person and Demographics tables we don't know which Demographic record is associated with each Person record. You need to know that in order to create the combined table.

codemonkey
A: 

The reason I cannot do what you guys are suggesting is because the two data comes from two different databases(Oracle, SQL). I cannot use linkedservers because the speed is just not there, is there something you can suggest to me that I can do programatically? Thanks.

Oliver S