Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Merge TWO, Problem is not solved

Author  Topic 

dtong004
Constraint Violating Yak Guru

281 Posts

Posted - 2001-06-08 : 14:28:38
The question did not answered and I want to get help.

Here is the orignal question:

I want to be able to merge two tables into one using a stored procedure if possible.

I have two tables with a common set of columns. (Common meaning same column name and datatype/size.) I won't know the names of those common columns or how many columns between the two tables are common.

These two tables can also have additional columns as well.

How can I merge these two tables' structures creating a new table and then populate the resulting table with the applicable data from the existing tables?
Example:
----Table One------

[db1]..[Table One] ColA varchar(25) -- Same as ColA in db2ColB int -- Same as ColB in db2Colx char(10) -- Different Col

-------------------


----Table Two ----- [db2]..[Table Two] ColA varchar(25) -- Same as ColA in db1ColB int -- Same as ColB in db1Col9 int -- Different Col


-------------------
The resulting table I want is:

[db3]..[MergedTbl] ColA varchar(25) ColB int colx char(10) Col9 int


The original answer:

There are two approaches you could take to solve this problem. The first is a UNION query where you simply specify NULL for the columns that do not match between the two tables. The other is to build a temporary table and populate it with the each table. Effectively the same result as the UNION query.


However, my problem did not solved. Here is my problem: ****************************************

The answer does not address the question. The problem is we need the records in table two match colA and colB with should be updated.

example:
table one:

AA, 2, X
AB, 3, Y
AA, 3, Z
AA, 3, R

Table two like this:
AA, 3, 27
AB, 3, 50
AA, 1, 30

The result should be like this:

AA, 2, X, -
AB, 3, Y, 50
AA, 3, Z, 27
AA, 3, R, -
AA, 1, -, 30

Aperantly, Union cannot do the work. The key is colA+colB is not unique, I don't want the data in table two repeated.

Thanks for you help.

Daniel



   

- Advertisement -