我需要使用已被删除的值更新表列。为了增加更多的复杂性,我需要基于派生来连接一个值,因此为concat追加的值并不总是相同的。我想出了这个查询,但它显然有问题。
update TRANSFER set PROCESS_STATUS = concat ( PREDICATE ,
(select dom_value from DOMAIN_VALUES dv where dv.val_id = st.sr_cashflow_status
CASE PREDICATE WHEN dv.dom_value = 'ZZZZ' THEN 'SPB'
WHEN dv.dom_value = 'XXXX' THEN 'SPB'
WHEN dv.dom_value = 'YYYY' THEN 'SPB'
ELSE 'DPS'
END )) from TRANSFER st
where PROCESS_STATUS is null;
我的主要问题是如何将appender(此处为谓词)维护为可以通过join获得的值检查的动态值?
任何答案/方向都非常赞赏。
答案 0 :(得分:1)
我想您正在努力实现以下内容(考虑PREDICATE
是TRANSFER
表中的一列) -
MERGE INTO TRANSFER st
using (select CASE
WHEN dv.dom_value = 'ZZZZ' THEN 'SPB'
WHEN dv.dom_value = 'XXXX' THEN 'SPB'
WHEN dv.dom_value = 'YYYY' THEN 'SPB'
ELSE 'DPS'
END as key,
val_id
from DOMAIN_VALUES) dv
on (dv.val_id = st.sr_cashflow_status and st.PROCESS_STATUS is NULL)
WHEN MATCHED THEN
UPDATE SET PROCESS_STATUS = concat(PREDICATE,dv.key);
OR
MERGE INTO TRANSFER st
using (select CASE
WHEN dv.dom_value = 'ZZZZ' THEN 'SPB'
WHEN dv.dom_value = 'XXXX' THEN 'SPB'
WHEN dv.dom_value = 'YYYY' THEN 'SPB'
ELSE 'DPS'
END as key,
val_id
from DOMAIN_VALUES) dv
on (dv.val_id = st.sr_cashflow_status and st.PROCESS_STATUS is NULL)
WHEN MATCHED THEN
UPDATE SET PROCESS_STATUS = PREDICATE||dv.key;
关于CASE
语句语法的快速说明。语法看起来不正确,因为您没有将CASE
的结果与任何内容进行比较,因为您在WHERE
子句中使用它,而且您也缺少AND
子句。
update TRANSFER set PROCESS_STATUS = concat ( PREDICATE ,
(select dom_value from DOMAIN_VALUES dv where dv.val_id = st.sr_cashflow_status
CASE PREDICATE WHEN dv.dom_value = 'ZZZZ' THEN 'SPB' -- and clause missing
WHEN dv.dom_value = 'XXXX' THEN 'SPB'
WHEN dv.dom_value = 'YYYY' THEN 'SPB'
ELSE 'DPS'
END )) --comparison missing like CASE ... END = 'SPB' since the case is inside WHERE
from TRANSFER st -- you cannot have a FROM clause in UPDATE
where PROCESS_STATUS is null;