Skip to Content
Custom Search

Update Data in one DB2 table using data in another table

 

UPDATE DATA IN ONE DB2 TABLE USING DATA IN ANOTHER TABLE
 
 
There are instances when you wish to add a new column in a table and initialize it 
with data from a column in another table. 
 
UPDATE TABLE1 A
SET (A.COLUMN1) = (SELECT B.COLUMN2 FROM TABLE2 B WHERE B.COLUMN_KEY = A.COLUMN_KEY)
 
Where in the above query 
 
TABLE1     - Table in which update is to run
TABLE2     - Table which contains the data to be used for update   
COLUMN1    - Column which needs to be updated
COLUMN2    - Column whose value is to be used to populate data in COLUMN1
COLUMN_KEY - Column which is used to determine the record to be updated

Comments

Better use the MERGE command

 

MERGE INTO TABLE1 AS A

USING (

SELECT B.COLUMN2 FROM TABLE2 B

) AS B ON (

B.COLUMN_KEY = A.COLUMN_KEY

)

WHEN MATCHED THEN

UPDATE SET A.COLUMN1 = B.COLUMN2

;

Let me know if you have any questions.

Balázs

NewPush.com

Post new comment

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

Click the +1 button  below to share it with your friends and colleagues

 

Share this if you liked it!

 

Disclaimer



Who's online

There are currently 0 users and 34 guests online.
Dr. Radut | page