Oliver S
Oliver S

Reputation: 611

Merge Datatable columns

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.


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.

Upvotes: 0

Views: 3810

Answers (4)

Rizvi Hasan
Rizvi Hasan

Reputation: 712

If you want to join this two data tables - Table1(name,age) and Table2(auto,kids) regardless any joining between columns then i would prefer to use Linq. In Linq you can join two or more tables with the "rowIndex" of the tabel.

Try following VB.NET code:

Dim Table1, Table2, MergerTable As New DataTable

    Dim rs = From c In Table1.AsEnumerable()
        Join c1 In Table2.AsEnumerable() On Table1.Rows.IndexOf(c) Equals LicensDatum.Rows.IndexOf(c1)
                   Select New With
                   {
                        .a0 = c.Item(0),
                        .b0 = c.Item(0),
                        .a1 = c.Item(0),
                        .b1 = c1.Item(0)
                   }

    MergerTable = New DataTable()
    MergerTable.Columns.Add("name", GetType(String))
    MergerTable.Columns.Add("age", GetType(String))
    MergerTable.Columns.Add("auto", GetType(String))
    MergerTable.Columns.Add("kid", GetType(String))

    For Each row In rs.ToList()
        Dim tableRow = MergerTable.NewRow()
        tableRow.Item("name") = row.a0
        tableRow.Item("age") = row.b0
        tableRow.Item("auto") = row.a1
        tableRow.Item("kid") = row.b1
        MergerTable.Rows.Add(tableRow)
    Next

You can play with the types if you want to do this merge with more type safety.

Upvotes: 0

codemonkey
codemonkey

Reputation: 2665

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.

Upvotes: 1

SQLMenace
SQLMenace

Reputation: 135011

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

select * from table1 t1
join table2 t2 on  <some magic where clause here>

Upvotes: 0

StingyJack
StingyJack

Reputation: 19469

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.

Upvotes: 0

Related Questions