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 ASET (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
Anonymous
Wed, 11/28/2012 - 08:43
Permalink
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