Q
Problem solve Get help with specific problems with your technologies, process and projects.

Updating one table from another

Kindly help me with writing SQL/400 statement for the following scenario:

MAIN file: record number, customer number

temp file: record#, corrected cust#

How can I update MAIN file using record# in both files? Thank you for your advice.


Update each row in MAIN with a value from the matching row in temp --

update MAIN
   set cust# = ( select cust# 
                   from temp
                  where record# = MAIN.record# )

The subquery here is a scalar subquery, and must return only one value (i.e. it selects only one column and must return only one row), or else it will cause an error and the entire UPDATE will fail. The WHERE clause of the subquery makes it a correlated subquery, and the way to understand how this works is to imagine that the subquery is evaluated separately for each row of the MAIN table. So for any given row in the MAIN table, the subquery returns any row(s) from the temp table with a matching record number -- if there is more than one matching row, you get an error, but I'll assume that the record number is unique and this won't happen.

Now note that there is no WHERE clause on the UPDATE statement. This means that every row will be updated. If there is any row in MAIN that does not have a matching row in temp, what happens when the subquery is evaluated? The subquery will return a NULL, and this is what the MAIN customer number will be set to. So if there are unmatched rows in MAIN, and you do not want the customer number set to NULL, then you have to qualify which rows of MAIN are to be updated, like this --

update MAIN
   set cust# = ( select cust# 
                   from temp
                  where record# = MAIN.record# )
 where exists ( select cust# 
                   from temp
                  where record# = MAIN.record# )

For More Information

  • What do you think about this answer? E-mail the edtiors at editor@searchDatabase.com with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

This was last published in May 2002

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close