PL / SQL循环未找到数据

时间:2015-05-27 11:51:28

标签: sql plsql oracle11g oracle-apex

我尝试将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)

3 个答案:

答案 0 :(得分:0)

当SELECT语句中的过滤谓词无法根据条件检索任何行时,会出现

ORA-01403: no data found

  

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>

答案 1 :(得分:0)

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

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);

祝你好运。

答案 2 :(得分:0)

显然,表ITEM_UOM_LEVELS有时没有ITEM_ID条目,因此导致NO DATA FOUND错误。所以请改用subselect:

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
  )
);