SQL Brain teaser March 29, 2006
Warning: array_merge() [function.array-merge]: Argument #2 is not an array in /homepages/26/d120742039/htdocs/Norm/iseries/wordpress/wp-content/plugins/technotag.php on line 30
I could have inserted a Sudoku puzzle here, but since we are an iSeries Users Group, here is a brain teaser for the SQL chumps out there. If you know the answer, feel free to post the code with explanation in the comments area below this post or e-mail it to us. If there are no responses, answer will be posted next week. So here it is:
I have two tables and I want to update Column_X in Table 1 with the value in Column_Y in Table 2. Table1 and Table2 are matched on Key1. You have to make sure no null values are written to the Column_X.
Will this piece of SQL code work?
UPDATE Table1 T1
SET Column_X = (SELECT Column_Y FROM Table2 T2 where T1.Key1 = T2.Key1)
What happens when there is no match? How can you prevent it from writing nulls in Column_X?
Hint : You may find your solution in DB2 Cookbook, which is a free download from Graeme Birchall’s website.
Possible Related Links
