计数器删除记录pl / sql

时间:2016-01-13 00:24:06

标签: plsql oracle11g

我的程序存在问题,应该根据下表中的订单号和产品编号一次删除一个产品。我最初尝试了这个过程,它只在从odetails中删除第一个pno并插入所需的表时才起作用。但是,我的问题是当订单有多个产品编号并想要删除One By One时。当我尝试删除它时,我通常会在最后一项中收到错误,并且由于此表中没有删除,因此订单也不会被删除。总结如何在完成所有删除过程后从订单中删除最后一个ono?任何帮助将不胜感激!

create or replace PROCEDURE ONE_BY_ONE( ORD_NUM IN NUMBER(5), PRO_NUM IN NUMBER(5))
AS
CURSOR CUR1
IS
SELECT * FROM ORDERS 
WHERE ONO = ORD_NUM;

CURSOR CUR2
IS
SELECT * FROM ODETAILS
WHERE PNO = PRO_NUM
AND ONO = P_ONO;

VAL1 CUR1%ROWTYPE;
VAL2 CUR2%ROWTYPE;

BEGIN

OPEN CUR1;
OPEN CUR2;

LOOP
FETCH CUR1 INTO VAL1;
FETCH CUR2 INTO VAL2;

EXIT WHEN CUR1%NOTFOUND;
EXIT WHEN CUR1%NOTFOUND;


IF VAL1.SHIPPED IS NOT NULL THEN
   IF PRO_NUM = VAL2.PNO AND ORD_NUM = VAL2.ONO THEN
      INSERT INTO (ID_NUMBER, ONO,CNO,DATE_SEND,CONDITION)
      VALUES(Seq.NEXTVAL, VAL1.ONO,VAL1.CNO,SYSDATE,'SEND BACK');
      INSERT INTO REMOVED_ODETAILS(ONO,PNO,QTY) VALUES (VAL2.ONO, VAL2.PNO, VAL2.QTY);
   END IF;  
      DELETE FROM ODETAILS WHERE ONO = ORD_NUM AND PNO = PRO_NUM;
       DBMS_OUTPUT.PUT_LINE('Item will be send back ');

-- DELETE FROM ORDERS WHERE ONO = ORD_NUM;

ELSIF VAL1.SHIPPED IS NULL THEN
       DBMS_OUTPUT.PUT_LINE('DO NOTHING ');
END IF;

END LOOP;
CLOSE CUR1;
CLOSE CUR2;
END ONE_BY_ONE;

订单

    ONO        CNO        ENO      RECEIVED    SHIPPED 
 ---------- ---------- ---------- ---------   ---------
    1112      1010       9999     18-JAN-16    15-JAN-16 

ODETAILS

     ONO        PNO         QTY
  ---------- ---------- ----------
    1112       12345          1 
    1112       67891          4  

1 个答案:

答案 0 :(得分:1)

只需回答您的问题,即可添加到删除

DELETE FROM ORDERS 
WHERE ONO = ORD_NUM and 
NOT EXISTS (select 1 from ODETAILS where ONO = ORD_NUM );

之后,只有当订单是订单中的最后一个产品时才会执行删除。