Column name checking null still giving null error

时间:2019-05-31 11:56:07

标签: sql oracle

I want to insert values in a table. So I have written the below code for that.

create or replace PROCEDURE NEIQC_DATA_DUMP_MST AS 
BEGIN

execute immediate 'truncate table TBL_NEIQC_WF_SITE_MST_NEW';

INSERT INTO TBL_NEIQC_WF_SITE_MST_NEW

(
    OBJECTID,
    SAP_ID,
    NETWORK_ENTITY_ID    ,
    SITE_NAME            ,
    SITE_ADDRESS         ,
    MAINTENANCEZONE_CODE ,
    INVENTORY_TYPE       ,
    TYPE_NAME            ,
    SITE_STATUS_CODE     , 
    NE_MODIFIED_DATE     ,
    NE_MODIFIED_BY       ,
    --SERVICE_CODE,
    CREATED_DATE         ,
    CREATED_BY           ,
    STRUCTURE_NAME       ,
    RJ_CITY_CODE         ,
    RJ_R4G_STATE_CODE    ,
    RJ_DISTRICT_CODE     ,
    RJ_TALUK_CODE        ,
    RJ_JC_CODE           ,
    RJ_JIOPOINT_SAPCODE  ,
    RJ_COMPANY_CODE_1    ,
    RJ_COMPANY_CODE_2    ,
    PLACEMENT_DATE,
    SITE_TYPE
)

SELECT

    OBJECTID                      ,
    RJ_SAPID ,
    RJ_NETWORK_ENTITY_ID          ,
    RJ_SITE_NAME                  ,
    RJ_SITE_ADDRESS               ,
    RJ_MAINTENANCE_ZONE_CODE      ,
    CASE when RJ_SAPID LIKE '%ENB%' THEN 'SITE' ELSE 'OTHERS' end as INVENTORY_TYPE, 
    TYPE_NAME                     ,
    4,
    RJ_LAST_MODIFIED_DATE,
    RJ_LAST_MODIFIED_BY ,
    --APP_FIBERINV.FUNC_SC(RJ_SAPID),
    SYSDATE,
    'SCHEDULER',
    STRUCTURE_NAME                ,
    RJ_CITY_CODE                  ,
    RJ_R4G_STATE_CODE             ,
    RJ_DISTRICT_CODE              ,
    RJ_TALUK_CODE                 ,
    RJ_JC_CODE                    ,
    RJ_JIOPOINT_SAPCODE           ,
    RJ_COMPANY_CODE_1             ,
    RJ_COMPANY_CODE_2          ,
    PLACEMENT_DATE,
    REGEXP_SUBSTR(TO_CHAR(RJ_SAPID),'[^-]+', 1, 4) AS SITE_TYPE

 from APP_FTTX.ne_structures WHERE RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA' AND OBJECTID IS NOT NULL;



COMMIT; 

END NEIQC_DATA_DUMP_MST;

But the issue is, even after checking the null condition for OBJECTID I am still getting error as

ORA-01400: cannot insert NULL into ("APP_FIBERINV"."TBL_NEIQC_WF_SITE_MST_NEW"."OBJECTID")

Please suggest what may be wrong here.

2 个答案:

答案 0 :(得分:2)

这很可能是operator precedenceAND的优先级高于OR。所以你的情况:

WHERE RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA' AND OBJECTID IS NOT NULL

被解释为

WHERE RJ_SAPID IS NOT NULL OR (RJ_SAPID <> 'NA' AND OBJECTID IS NOT NULL)

如果您有一个源行,其中OBJECTID为null,那么如果RJ_SAPID'NA'以外的任何内容(包括null),它将仍然超出条件,并且插入失败。

我相信你想要

WHERE (RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA') AND OBJECTID IS NOT NULL

因此您需要在语句中包括这些括号。

答案 1 :(得分:1)

我认为您的病情应该得到改善。在您或条件周围添加'()'

WHERE (RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA') AND OBJECTID IS NOT NULL;