PL/ SQL 循环未找到数据

PL/ SQL Loop No data found

我尝试将 VarNewIdOne 的值作为批量集合。因此,在我尝试将该值等同于另一个变量 (VarOldLevlIdOne) 然后作为未找到数据退出时,我获得了批量收集的值。

            SELECT ITEM_ID bulk collect INTO VarNewIdOne
            FROM   BIZZXE_V2_SCH.ITEMS
            WHERE  PARENT_ITEM_ID = VarId;

  FOR k IN VarNewIdOne.First ..VarNewIdOne.Last LOOP

   SELECT LEVEL_ID  INTO VarOldLevlIdOne  
   FROM   BIZZXE_V2_SCH.ITEM_UOM_LEVELS
   WHERE  ITEM_ID IN (VarNewIdOne(K));


 DELETE  FROM BIZZXE_V2_SCH.ITEM_UOM_LEVEL_CONTROLS WHERE LEVEL_ID=VarOldLevlIdOne;

END LOOP;

这些是我的桌子

    CREATE TABLE  "ITEMS" 
   (    "ITEM_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "NAME" VARCHAR2(50) NOT NULL ENABLE, 
    "SHORT_NAME" VARCHAR2(25) NOT NULL ENABLE, 
    "CODE" VARCHAR2(25) NOT NULL ENABLE, 
    "HS_CODE_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "BRAND_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "CAT_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "XMLCOL" "SYS"."XMLTYPE" , 
    "REMARKS" VARCHAR2(50), 
    "VARIANT_TEMP_ID" NUMBER NOT NULL ENABLE, 
    "LOCATION_ID" NUMBER(38,0), 
    "TRANS_DATE" DATE, 
    "USER_ID" VARCHAR2(128), 
    "VERSION_ID" NUMBER(7,2), 
    "PARENT_ITEM_ID" NUMBER, 
     CONSTRAINT "ITEM_PK" PRIMARY KEY ("ITEM_ID")
  USING INDEX  ENABLE
   )


CREATE TABLE  "ITEM_UOM_LEVELS" 
   (    "LEVEL_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "ITEM_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "CONV_FACTR" VARCHAR2(50), 
    "IMAGE" BLOB, 
    "STATUS_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "XMLCOL" "SYS"."XMLTYPE" , 
    "UOM_LEVEL" NUMBER NOT NULL ENABLE, 
    "LONG_NAME" VARCHAR2(50) NOT NULL ENABLE, 
    "UOM_ID" NUMBER NOT NULL ENABLE, 
    "SYS_USAGE_ID" NUMBER NOT NULL ENABLE, 
    "UOM_LEVEL_VOLUME" NUMBER, 
     CONSTRAINT "ITEM_LEVEL_PK" PRIMARY KEY ("LEVEL_ID")
  USING INDEX  ENABLE
   )
ALTER TABLE  "ITEM_UOM_LEVELS" ADD CONSTRAINT "ITM_UOM_LEVELS_ITEM_FK" FOREIGN KEY ("ITEM_ID")
      REFERENCES  "ITEMS" ("ITEM_ID") ENABLE

样本数据

insert into ITEMS   (Item_Id,   Name,   Short_Name, Code,   Hs_Code_Id, Brand_Id,   Cat_Id  ,Remarks    ,Variant_Temp Id,   Parent_Item_Id) values (203,"small Large updated","SML_LRG","SML_LRGBYPO123",   111,    8,  5,      "Comment",4,202);

   insert into ITEM_UOM_LEVELS (LEVEL_ID,ITEM_ID,CONV_FACTR,STATUS_ID,UOM_LEVEL,    LONG_NAME,UOM_ID,SYS_USAGE_ID,UOM_LEVEL_VOLUME) values (61,71,"Cost",1, 1,"Test",9,1,30)

ORA-01403: no data found 当 SELECT 语句中的 过滤器谓词 无法根据条件检索任何行时发生。

WHERE ITEM_ID IN (VarNewIdOne(K))

如果没有匹配的 ITEM_ID 则 找不到数据 将引发异常。

例如,

SQL> DECLARE
  2    cnt NUMBER;
  3  BEGIN
  4    FOR i IN
  5    (SELECT empno FROM emp
  6    )
  7    LOOP
  8      SELECT empno INTO cnt FROM emp WHERE empno = 1234;
  9    END LOOP;
 10  END;
 11  /
DECLARE
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 8


SQL>

最好尽可能避免显式循环逻辑。在这种情况下,您可以用单个语句替换批量收集和循环:

DELETE FROM BIZZXE_V2_SCH.ITEM_UOM_LEVEL_CONTROLS c
  WHERE c.LEVEL_ID IN (SELECT DISTINCT l.LEVEL_ID
                         FROM BIZZXE_V2_SCH.ITEMS i
                         INNER JOIN BIZZXE_V2_SCH.ITEM_UOM_LEVELS l
                           ON l.ITEM_ID = i.ITEM_ID
                         WHERE i.PARENT_ITEM_ID = VarId);

祝你好运。

显然 table ITEM_UOM_LEVELS 有时没有 ITEM_ID 条目,因此会导致 NO DATA FOUND 错误。所以改用子选择:

SELECT ITEM_ID bulk collect INTO VarNewIdOne
FROM   BIZZXE_V2_SCH.ITEMS
WHERE  PARENT_ITEM_ID = VarId;

FOR k IN VarNewIdOne.First ..VarNewIdOne.Last LOOP
  DELETE  FROM BIZZXE_V2_SCH.ITEM_UOM_LEVEL_CONTROLS WHERE LEVEL_ID IN
  (
    SELECT LEVEL_ID  
    FROM   BIZZXE_V2_SCH.ITEM_UOM_LEVELS
    WHERE  ITEM_ID = VarNewIdOne(K)
  );
END LOOP;

或者甚至将其设为单个 SQL 语句:

DELETE  FROM BIZZXE_V2_SCH.ITEM_UOM_LEVEL_CONTROLS WHERE LEVEL_ID IN
(
  SELECT LEVEL_ID  
  FROM   BIZZXE_V2_SCH.ITEM_UOM_LEVELS
  WHERE  ITEM_ID IN
  (
    SELECT ITEM_ID
    FROM   BIZZXE_V2_SCH.ITEMS
    WHERE  PARENT_ITEM_ID = VarId
  )
);