Solved

Add contents of one table's column to another table

Posted on 2007-03-26
4
241 Views
Last Modified: 2010-03-20
Sorry, I'm a real newbie to SQL. I have two tables with the same columns and would like to do the following:

Add the contents of Table2.ColumnX to Table1.ColumnX where Table1.pkID = Table2.pkID (all the rows are the same in both tables except for the values of ColumnX)

Thanks in advance for any help!
0
Comment
Question by:siono3
  • 3
4 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 18796591
Try this:

SELECT Table2.ColumnX + Table1.ColumnX  as YourSum
FROM  Table1 Inner Join Table2 ON Table1.pkID = Table2.pkID
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18796599
If you actually need to update Table1's data, not just a select query, make a backup and try this:

Update Table1 Inner Join Table2 ON Table1.pkID = Table2.pkID
SET  Table2.ColumnX  = Table2.ColumnX + Table1.ColumnX  
0
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 18796611
Correction... The update query I posted before will update table2's data.  This will update table1:

Update Table1 Inner Join Table2 ON Table1.pkID = Table2.pkID
SET  Table1.ColumnX  = Table2.ColumnX + Table1.ColumnX  
0
 

Author Comment

by:siono3
ID: 18799320
Thanks mbizup,

That almost worked. For some reason I had to tweak it a little bit and ended up with the following:
UPDATE    Table1
SET              Table1.ColumnX = Table2.ColumnX
FROM         Table1 INNER JOIN
                      Table2 ON Table1.pkID = Table2.pkID

but you set me on the right track...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now