2

我用于更新列“p1_high_edu_ctry”的 sql 查询是:-

 update ps_personal_data J
 set (J.p1_high_edu_ctry) = (SELECT P.p1_high_edu_ctry FROM PS_PERS_DATA_EFFDT P 
 WHERE P.EFFDT=(SELECT MAX(K.EFFDT) FROM PS_PERS_DATA_EFFDT K
 WHERE K.EFFDT<=SYSDATE
 AND   J.EMPLID IN(SELECT H.EMPLID FROM PS_AUDIT_PER_EFFDT H 
 where h.AUDIT_OPRID='CHGH00000063232')))
 where exists(select 1 from PS_AUDIT_PER_EFFDT q where q.emplid=j.emplid) ;

但是我在更新时收到错误消息:-

ERROR at line 2:
ORA-01407: cannot update ("SYSADM"."PS_PERSONAL_DATA"."P1_HIGH_EDU_CTRY") to NULL

请有任何建议,为什么即使我在存在的地方处理它也不起作用???

4

2 回答 2

1

您对此字段有一个非空约束,而您的子查询不返回任何数据,返回空值。这是标量子查询表达式的记录行为

12:28:52 SYSTEM@dwal> create table n (t number not null);

Table created.

Elapsed: 00:00:00.31
12:39:18 SYSTEM@dwal> insert into n values (1);

1 row created.

Elapsed: 00:00:00.01
12:40:12 SYSTEM@dwal> update n set t = (select 2 from dual where 1 = 2);
update n set t = (select 2 from dual where 1 = 2)
             *
ERROR at line 1:
ORA-01407: cannot update ("SYSTEM"."N"."T") to NULL


Elapsed: 00:00:00.03
于 2013-04-03T04:41:35.797 回答
1

试试这个

 MERGE INTO ps_personal_data J
 USING
 (
   SELECT P.p1_high_edu_ctry, p.emplid 
   FROM PS_PERS_DATA_EFFDT P 
   WHERE P.EFFDT=
              (
                SELECT MAX(K.EFFDT) FROM PS_PERS_DATA_EFFDT K
                WHERE K.EFFDT<=SYSDATE AND P.EMPLID = K.EMPLID 
                -- updated this condition , 
                -- you need to check max date by emplid 
                -- not max date from full table               
               )
   AND   P.EMPLID IN (SELECT H.EMPLID FROM PS_AUDIT_PER_EFFDT H 
                where h.AUDIT_OPRID='CHGH00000063232')
 ) v ON (J.emplid = v.emplid)
 WHEN MATCHED THEN UPDATE
 set J.p1_high_edu_ctry = nvl(v.p1_high_edu_ctry,' ');
于 2013-04-03T04:59:54.323 回答