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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick SQL merge question

Status
Not open for further replies.

GGcpp

Programmer
Joined
Nov 21, 2007
Messages
2
Hope I finally got the right forum :)

I have this SQL problem. I’m a C++ guy who's database experience is limited to basic Select commands. I boasted this should be easy but realized how long ago I did any 'real' SQL commands. I know this is easy stuff for the gurus ;)
Note, I am under Access (MDB) usually do stuff in C++ (one row at a time) but I know this is easy and hope to learn something.

I need to update a table (user) with data contained in another table (latest user info)

Here is my problem in ‘C’ like pseudo-code:
Code:
//I have 2 similar tables, ‘Master’ and ‘Updates’
// Step 1: update / merge (join?)
if (Master.numberId == Updates.MasterNumber)
{
    Master.total_global = Updates.tglobal
    Master.ACTION_text += Updates.ACTION_text
    Master.fact = Updates.fact
}
Code:
// Step 2: add new entries
if(Updates.MasterNumber ‘is not in’ Master.numberId)
{
    Insert into Master( field1, field2, field3, ...)
    Select * from  Updates where MasterNumber  == Updates.MasterNumber
}
 
SQL code:
Code:
UPDATE Master INNER JOIN Updates ON Master.numberId = Updates.MasterNumber
SET Master.total_global = Updates.tglobal
, Master.ACTION_text = Master.ACTION_text & Updates.ACTION_text
, Master.fact = Updates.fact

Code:
INSERT INTO Master (field1, ..., fieldN)
SELECT col1, ..., colN FROM Updates
WHERE MasterNumber NOT IN (SELECT numberID FROM Master)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
A big THANK YOU to PHV, worked on first try.

I remember why I decided to go C++ instead of database after school ;)

A big Merci from Snowy Montréal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top