我想在我的一张桌子上更新一个新列,但我不知道该怎么做。
新列是MARITAL_STATUS,位于SCV_CLEINT_DETAILS表中。此表的值将从各种来源生成,下面的查询将获取我正在寻找的值:
SELECT scd.qsclient_id,
scd.system_client_id,
NVL(c.paxus_client_id, c.client_id),
UPPER(NVL(NVL2(c.paxus_client_id, pc.clt_mar_stat, c.maritial_status_code), decode(c.client_type_id, '2000001702', 'C', 'U'))) AS NewMarStatus,
scd.marital_status
FROM scv_client_details scd, client c, paxus_client pc
WHERE scd.system_client_id = to_char(c.client_id)
AND c.paxus_client_id = pc.client(+)
AND UPPER(scd.SYSTEM_INDICATOR) = 'WRITEN'
AND (scd.marital_status <> UPPER(NVL(NVL2(c.paxus_client_id, pc.clt_mar_stat, c.maritial_status_code), decode(c.client_type_id, '2000001702', 'C', 'U'))) OR
scd.marital_status IS NULL)
我想使用上面NewMarStatus中生成的值更新新的MARITAL_STATUS列。我真的不知道如何编写更新语句。
任何帮助都非常感激。
答案 0 :(得分:1)
我总是使用批量上传程序来处理这些事情。
declare
cursor cur
is
SELECT scd.rowid row_id,
UPPER(NVL(NVL2(c.paxus_client_id, pc.clt_mar_stat, c.maritial_status_code), decode(c.client_type_id, '2000001702', 'C', 'U'))) AS marital_status
FROM scv_client_details scd, client c, paxus_client pc
WHERE scd.system_client_id = to_char(c.client_id)
AND c.paxus_client_id = pc.client(+)
AND UPPER(scd.SYSTEM_INDICATOR) = 'WRITEN'
AND (scd.marital_status <> UPPER(NVL(NVL2(c.paxus_client_id, pc.clt_mar_stat, c.maritial_status_code), decode(c.client_type_id, '2000001702', 'C', 'U'))) OR
scd.marital_status IS NULL)
order by row_id
;
type type_rowid_array is table of rowid index by binary_integer;
type type_marital_status_array is table of scv_client_details.marital_status%type;
arr_rowid type_rowid_array;
arr_marital_status type_marital_status_array;
begin
open cur;
loop
fetch cur bulk collect into arr_rowid, arr_marital_status;
forall i in arr_rowid.first .. arr_rowid.last
update scv_client_details tab
SET tab.marital_status = arr_marital_status(i)
where tab.rowid = arr_rowid(i)
;
exit when cur%notfound;
end loop;
close cur;
commit;
exception
when others
then rollback;
raise_application_error(-20000, 'Fout bij uitvoeren update van scv_client_details(marital_status) - '||sqlerrm);
end;
答案 1 :(得分:1)
更新连接的几种方法在其他SO中描述:
MERGE
是进行群发连接更新的最简单方法之一。它可以很好地扩展,因为它可以使用所有形式的连接,尤其是HASH JOIN。
USING
子句是您的查询(显然添加了rowid
):
MERGE INTO scv_client_details d
USING (SELECT NVL(c.paxus_client_id, c.client_id),
UPPER(NVL(NVL2(c.paxus_client_id,
pc.clt_mar_stat,
c.maritial_status_code),
decode(c.client_type_id, '2000001702', 'C', 'U')))
AS NewMarStatus,
scd.ROWID rid
FROM scv_client_details scd, client c, paxus_client pc
WHERE scd.system_client_id = to_char(c.client_id)
AND c.paxus_client_id = pc.client(+)
AND (scd.marital_status <>
UPPER(NVL(NVL2(c.paxus_client_id,
pc.clt_mar_stat,
c.maritial_status_code),
decode(c.client_type_id, '2000001702', 'C', 'U')))
OR
scd.marital_status IS NULL)) v
ON (d.ROWID = v.rid)
WHEN MATCHED THEN
UPDATE SET d.marital_status = newmarstatus;
有关完整示例,请参阅此SQLFiddle。