0

我想在我的一张桌子上更新一个新列,但我不知道该怎么做。

新列是 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 列。我真的不知道如何编写更新语句。

任何帮助都非常感谢。

4

2 回答 2

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;
于 2013-05-29T13:17:47.313 回答
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

于 2013-05-29T14:14:15.700 回答