oracle 用另一个表中的数据更新空列

dw1jzc5e  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(114)

我有一个要求,比较一个表,并更新另一个表,如果有空记录。表LEGACY_CUST_INFO具有正确的CUST_PRIVILEGE_NUMBER。因此,我需要在第二个表CUST_INFO中找出CUST_PRIVILEGE_NUMBER中的空记录,并从第一个表LEGACY_CUST_INFO更新CUST_PRIVILEGE_NUMBER。除了更新CUST_PRIVILEGE_NUMBER,我还需要用SYSDATE更新UPDATED列,用'ADMIN'更新UPDATED_BYROW_ID在两个表中匹配是完全匹配。
下面是两个表结构
表1名称:LEGACY_CUST_INFO

ROW_ID  CUST_PRIVILEGE_NUMBER UPDATED UPDATED_BY
1-AB 2345 16-JUN-2022 ADMIN
1-AC 3345 17-JUN-2022 ADMIN
1-AV 2335 20-JUN-2022 ADMIN
1-AN 2042 18-JUN-2022 ADMIN
1-AG 2255 01-JUN-2022 ADMIN

表2名称:CUST_INFO

ROW_ID  CUST_PRIVILEGE_NUMBER UPDATED UPDATED_BY
1-AB NULL 22-JUN-2022 ADMIN
1-AC NULL 22-JUN-2022 ADMIN
1-AV 5409 22-JUN-2022 ADMIN
1-AN NULL 22-JUN-2022 ADMIN
1-AG 8848 22-JUN-2022 ADMIN

当我使用下面的更新时,它不更新任何东西。有没有办法更新空值?

merge into cust_info dst
using (
select lci.row_id, lci.cust_privilege_number
from legacy_cust_info lci
inner join cust_info ci on ci.row_id = lci.row_id
where ci.cust_privilege_number != lci.cust_privilege_number
) src
on (dst.row_id = src.row_id)
when matched then 
  update set dst.cust_privilege_number = src.cust_privilege_number, dst.updated= sysdate, dst.updated_by = 'ADMIN';
zzlelutf

zzlelutf1#

样本数据:

SQL> SELECT * FROM legacy_cust_info;

ROW_ CUST_PRIVILEGE_NUMBER UPDATED     UPDAT
---- --------------------- ----------- -----
1-AB                  2345 16-JUN-2022 ADMIN
1-AC                  3345 17-JUN-2022 ADMIN
1-AV                  2335 20-JUN-2022 ADMIN
1-AN                  2042 18-JUN-2022 ADMIN
1-AG                  2255 01-JUN-2022 ADMIN

SQL> SELECT * FROM cust_info;

ROW_ CUST_PRIVILEGE_NUMBER UPDATED     UPDAT
---- --------------------- ----------- -----
1-AB                       22-JUN-2022 ADMIN
1-AC                       22-JUN-2022 ADMIN
1-AV                  5409 22-JUN-2022 ADMIN
1-AN                       22-JUN-2022 ADMIN
1-AG                  5515 22-JUN-2022 ADMIN

MERGEWHERE子句中使用IS NULL条件,因为没有任何东西“等于”NULL,因此:WHERE d.cust_privilege_number <> s.cust_privilege_number是不够的。

SQL> MERGE INTO cust_info d
  2       USING legacy_cust_info s
  3          ON (d.row_id = s.row_id)
  4  WHEN MATCHED
  5  THEN
  6     UPDATE SET
  7        d.cust_privilege_number = s.cust_privilege_number,
  8        d.updated = SYSDATE,
  9        d.updated_by = 'ADMIN'
 10             WHERE    d.cust_privilege_number <> s.cust_privilege_number
 11                   OR d.cust_privilege_number IS NULL;

5 rows merged.

结果:

SQL> SELECT * FROM cust_info;

ROW_ CUST_PRIVILEGE_NUMBER UPDATED     UPDAT
---- --------------------- ----------- -----
1-AB                  2345 21-JUN-2023 ADMIN
1-AC                  3345 21-JUN-2023 ADMIN
1-AV                  2335 21-JUN-2023 ADMIN
1-AN                  2042 21-JUN-2023 ADMIN
1-AG                  2255 21-JUN-2023 ADMIN

SQL>

相关问题