DB2 and the SQL standard do not have a FROM clause in an UPDATE statement. Therefore, you need to clearly separate the steps from
- identify the lines to be changed, and
- calculate the new value.
.
Here is an example:
UPDATE TABLE A SET A.FLD_SUPV = ( SELECT B.FLD_SUPV FROM TABLEA A, TABLEB B, TABLEC C,TABLED D WHERE A.FLD1= B.FLD1 AND A.FLD_DT >= B.FLD_FM_DT AND A.FLD_DT <= B.FLD_THRU_DT AND A.FLD_DT > D.FLD_THRU_DT AND A.FLD_DT < C.FLD_EFF_DT ) WHERE EXISTS ( SELECT B.FLD_SUPV FROM TABLEA A, TABLEB B, TABLEC C,TABLED D WHERE A.FLD1= B.FLD1 AND A.FLD_DT >= B.FLD_FM_DT AND A.FLD_DT <= B.FLD_THRU_DT AND A.FLD_DT > D.FLD_THRU_DT AND A.FLD_DT < C.FLD_EFF_DT )
To update two fields, you can use an example like this:
UPDATE table1 t1 SET (col1, col2) = ( SELECT col3, col4 FROM table2 t2 WHERE t1.col8=t2.col9 )
The optimizer will see that the subqueries in the SET and FROM clauses are identical and should combine them in the internal execution plan.
source share