Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging Two tables in a DB 1

Status
Not open for further replies.

Dixen

Technical User
Mar 9, 2002
15
US

Believe it or not this is something I have just never needed to do. A database was set up with two seperate tables relating on a Name field. How does one merge the two tables together without loss of data?
 
TAKE COPIES OF THE TABLES FIRST - voice of pain here!!
You need to open the one table (table one, for example) in design mode and add in the fields that you want from table 2. Next, create a select query over table 2. Drag all the fields into the query (NOT using the * button, but select the first and shift/click the last).
Change the query to an update query, and it will ask you for the table to update to, and (providing the field names are the same across the two tables) it will automatically input the name of the relevant field in table 1.

Give it a go!!
 
Hi Dixen,

If I understand your post correctly, you have two tables with different data but a related column, Name? Is this correct? Do you want to create a new table that contains data from both existing tables? Or do you just want to select columns from matching rows both tables into one row?

How many columns do each of the tables have? Is there any duplication of columns in the two tables?

To Select all columns from both tables You'll want to JOIN the tables on the Name column. You should specifically list column names in the select list to avoid duplication of column names.

Select
a.name, a.col2, a.col3, ..., a.colN,
b.col2, b.col3, ..., b.colN
From Table1 As a
Inner Join Table2 As b
On a.name=b.name

You can convert the Seelct query to a make table query to create the new table with all of the cloumns.

Select
a.name, a.col2, a.col3, ..., a.colN,
b.col2, b.col3, ..., b.colN
Into Table3
From Table1 As a
Inner Join Table2 As b
On a.name=b.name

Run the query to create the table and insert all of the data. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions in the SQL Server forum. Many of the ideas apply to all forums.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top