If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Help - Column Data Migration

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-12-11, 20:45
coolbay22 coolbay22 is offline
Registered User
 
Join Date: Jul 2011
Posts: 1
Question Help - Column Data Migration

I have a situation where all the data in a column of Table2 needs to be migrated to another column of Table1 based on a join condition.

For example,

Table1 has A,B,C

and

Table2 has X,Y,Z. ( Y foreign key to A of Table1)

I add a new column Z to Table1 and need all the Table2 Z values in Table1 Z column where Table1.A = Table2.Y

How can I do this in Db2 via SQL, Stored Procs or any other DB2 tools?

I tried Insert statements such as follows ,. but does not work

Insert into Table1(Z)
select Table2.Z from Table1,Table2 where Table1.A = Table2.Y
Reply With Quote
  #2 (permalink)  
Old 07-12-11, 21:26
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Y foreign key to A of Table1
Then, if multiple rows of Table2 matched with a row of Table1,
what to do?
Reply With Quote
  #3 (permalink)  
Old 07-13-11, 00:41
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Here is an exmple,
if multiple rows of Table2 matched with a row of Table1,
choose one by some additional conditions.

For example: Take maximum value.
(Not tested)
Code:
MERGE INTO
       table1 t1
 USING (SELECT y
             , MAX(z) AS z
         FROM  table2
         GROUP BY
               y
       ) t2
   ON  t2.y = t1.a
WHEN MATCHED THEN
UPDATE
   SET z = t2.z

Last edited by tonkuma; 07-13-11 at 00:44.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On