is it possible to increment the field a and b of a table (A.a and A.b) using the value c and d of a different table (B.c B.d) for all the row of A where A.x == B.z?
I'm getting crazy with this query
Yes it is possible. You can try something like this:
MERGE INTO A
USING (SELECT c, d, z from B) B
ON (A.x = B.z)
WHEN MATCHED THEN
UPDATE SET A.a = A.a + B.c, A.b = A.b + B.d;
You can read more about MERGE here.
DB2 and the SQL standard don't have a FROM clause in an UPDATE statement. So you have to clearly separate the steps to
.
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 may 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 sub-queries in the SET and the FROM clause are identical and it should merge them in the internal execution plan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With