是否可以使用A的所有行的不同表(Bc Bd)的值c和d来递增表(Aa和Ab)的字段a和b,其中Ax == Bz?
我对此查询感到疯狂
答案 0 :(得分:8)
DB2和SQL标准在UPDATE语句中没有FROM子句。 所以你必须明确地将步骤分开到
以下是一个例子:
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 )
要更新两个字段,您可以使用如下示例:
UPDATE table1 t1
SET (col1, col2) = (
SELECT col3, col4
FROM table2 t2
WHERE t1.col8=t2.col9
)
优化器将在SET和FROM子句中看到子查询 是相同的,它应该在内部执行计划中合并它们。
答案 1 :(得分:5)
是的,这是可能的。你可以尝试这样的事情:
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;
您可以阅读有关MERGE here的更多信息。
答案 2 :(得分:0)
在DB2 10.6下测试
实际上与@jhnwsk相同,但添加了表格快捷方式。
Merge into "PRODUCTION"."COUNTRY" as N
using (SELECT OD.NAME,OD.KEY from "DEVELOPMENT"."COUNTRY" as OD) as O
on (O.KEY = N.KEY)
WHEN MATCHED THEN
UPDATE SET N.NAME=O.NAME;
答案 3 :(得分:0)
这也很好用
update TableA A
set a = (select a from TableB B where A.x = B.z)
where exists
(select 1 from TableB B where A.x = B.z) ;
答案 4 :(得分:-2)
UPDATE CS70P t1
SET (T1.TIPOCRE) = (
SELECT MOROSO
FROM FCSALDOC t2 ,CS70P t1
WHERE t1.NUMCRE =t2.CODCTA
)