在PL / SQL中使用If条件循环

时间:2015-12-21 22:47:43

标签: plsql cursor

我创建了这个块来测试一个名为process的表中的三个条件。我根据需求创建了sql查询,并将其插入到FOR LOOP中,每当我测试与进程表中的日期匹配的日期时,它应返回多个值。我对这个块的问题是 - 第一:当我针对DATE_PO记录测试无效日期时,我得到了ELSE条件,但是这开始循环两次以获得表进程中的记录数:

anonymous block completed
No records made in this date !
No records made in this date !

第二个问题,如果此表中没有数据,如何为没有找到记录的用户测试和显示消息做出适当的条件?

表格流程

       NUM        DATE_PO      STATUS                                    
    ----------  -----------   -----------
      1243      21-DEC-15       CANCEL                                 
      5678      21-DEC-15       APPROVE




BEGIN
  FOR X IN
  (SELECT PO.STATUS,
    PO.DATE_PO,
    SUM(D.QUANTIY) QUANTITY_SUM,
    SUM(NVL(D.QUANTIY,0) * NVL (M.COST,0)) T_VALUE
    FROM PROCESS.PO, DELETE_PROCESS, MONEY M
    WHERE D.PO = M.PO
    AND PO.NUM  = D.NUM
    GROUP BY PO.STATUS, PO.DATE_PO)
 LOOP
   IF  TO_CHAR(X.DATE_PO, 'mm-yyyy') ='10-2015' THEN --Not valid date
     IF X.STATUS ='CANCEL' THEN
       DBMS_OUTPUT.PUT_LINE('CANCELLATIONS: ' || X.QUANTITY_SUM );
       DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
       DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
    ELSIF X.STATUS ='HOLD' THEN
       DBMS_OUTPUT.PUT_LINE('HOLDS: ' || X.QUANTITY_SUM );
       DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
       DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
    ELSIF X.STATUS = 'APPROVE' THEN
       DBMS_OUTPUT.PUT_LINE('APPROVES: ' || X.QUANTITY_SUM );
       DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
       DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
    END IF;
   ELSE
      DBMS_OUTPUT.PUT_LINE('No records made in this date !');
   END IF;
  END LOOP;
 END;
 /

1 个答案:

答案 0 :(得分:0)

  

试试这个。希望能帮助到你。虽然它不是最好的解决方案   不使用ROW-BY-ROW处理完成。批量收集选项也可以   使用。如果这有帮助,请告诉我。因为我没有工作空间请   请原谅任何语法错误(如果有的话)。

DECLARE
  lv_cnt PLS_INTEGER;
  lv_date VARCHAR2(10):='12-2015';
  lv_dt DATE;
BEGIN
  BEGIN
    SELECT TO_DATE(lv_date,'MM-YYYY') INTO lv_dt FROM DUAL;
  EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line('Not a valid Date');
    RETURN;
  END;
  SELECT COUNT(1)
  INTO lv_cnt
  FROM
    (SELECT PO.STATUS,
      PO.DATE_PO,
      SUM(D.QUANTIY) QUANTITY_SUM,
      SUM(NVL(D.QUANTIY,0) * NVL (M.COST,0)) T_VALUE
    FROM PROCESS.PO,
      DELETE_PROCESS,
      MONEY M
    WHERE D.PO = M.PO
    AND PO.NUM = D.NUM
    GROUP BY PO.STATUS,
      PO.DATE_PO
    )A;

  IF lv_cnt > 0 THEN

    FOR X IN
    (SELECT PO.STATUS,
      PO.DATE_PO,
      SUM(D.QUANTIY) QUANTITY_SUM,
      SUM(NVL(D.QUANTIY,0) * NVL (M.COST,0)) T_VALUE
    FROM PROCESS.PO,
      DELETE_PROCESS,
      MONEY M
    WHERE D.PO = M.PO
    AND PO.NUM = D.NUM
    GROUP BY PO.STATUS,
      PO.DATE_PO
    )
    LOOP
      IF X.STATUS ='CANCEL' THEN
        DBMS_OUTPUT.PUT_LINE('CANCELLATIONS: ' || X.QUANTITY_SUM );
        DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
        DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
      ELSIF X.STATUS ='HOLD' THEN
        DBMS_OUTPUT.PUT_LINE('HOLDS: ' || X.QUANTITY_SUM );
        DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
        DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
      ELSIF X.STATUS = 'APPROVE' THEN
        DBMS_OUTPUT.PUT_LINE('APPROVES: ' || X.QUANTITY_SUM );
        DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
        DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
      END IF;
    END LOOP;
  ELSIF lv_cnt = 0 THEN
    dbms_output.put_line('No records found for the user');
  END IF;
END;