ORA-01407:无法更新为null

时间:2013-04-03 04:34:23

标签: oracle

我更新列“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

请给我任何建议,为什么即使我在哪里处理它也不起作用???

2 个答案:

答案 0 :(得分:1)

当您的子查询未返回任何数据时,您对此字段有一个非空约束,返回null。这是scalar subquery expression

的记录行为
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

答案 1 :(得分: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,' ');