Question

I have the following trigger written, and am getting an error i don't understand.... the error I'm getting is :

Error report - SQL Error: ORA-00036: maximum number of recursive SQL levels (50) exceeded ORA-00036: maximum number of recursive SQL levels (50) exceeded

how would this be?

i didn't intend to do any recursion, is it because my select statement is to vague? create or replace TRIGGER INVENTORY_AVAIL Before INSERT ON TORDER FOR EACH ROW

    DECLARE
       v_quantity_diff number;
       v_onhand_quantity number;

    BEGIN
       -- TRIGGR ON THIS...
       --INSERT INTO ORDER
       --( FK_ORDER_NO, FK_PROD_ID, QUANITY , COMPLETE_STATUS)
       --VALUES
       --( :NEW.FK_ORDER_NO, :NEW.FK_PROD_ID, :NEW.QUANITY , :NEW.COMPLETE_STATUS);
       SELECT INVENTORY_ONHAND INTO v_onhand_quantity
       FROM INVENTORY 
       WHERE :NEW.fk_prod_id = INVENTORY.FK_PROD_ID;

      IF( (v_onhand_quantity - :NEW.QUANTITY) >= 0)
      THEN
         INSERT INTO ORDER ( FK_ORDER_NO, FK_PROD_ID, QUANTITY, COMPLETE_STATUS)
          VALUES ( :NEW.FK_ORDER_NO, :NEW.FK_PROD_ID, :NEW.QUANTITY , :NEW.COMPLETE_STATUS);

      ELSE
      raise_application_error (-20001,'ERROR: QUANTITY ' || :NEW.QUANTITY 
           || ' EXCEEDS INVENTORY ONHAND [' || TO_CHAR(v_onhand_quantity) || ']' ); 
      END IF;

    END;
Was it helpful?

Solution

Then it's clear, you have a trigger on table Order Inserts that inserts into table Order/

i.e.,

  1. Your code inserts one or more rows into Table Order, firing trigger.
  2. trigger executes, and examines on_hand quantity, if sufficient, it
  3. inserts into table ORDER, firing trigger,
  4. trigger executes, and examines on_hand quantity, if sufficient, it
  5. inserts into table ORDER, firing trigger,
  6. trigger executes, and examines on_hand quantity, if sufficient, it
  7. inserts into table ORDER, firing trigger,
  8. trigger executes, and examines on_hand quantity, if sufficient, it
  9. inserts into table ORDER, firing trigger, etc..

    Your application code should have already done the Insert when the trigger runs... If there are sufficient items in inventory you simply need to let the transaction be committed, you do not need to perform the insert again.

    If, otoh, there are not sufficient items in inventory, then you need to rollback the transaction in the trigger (as well as any other operations initiated in the trigger.

here is kinda the way it ought to be written:

DECLARE
   v_quantity_diff number;
   v_onhand_quantity number;

BEGIN
   -- TRIGGR ON THIS...
   --INSERT INTO ORDER
   --( FK_ORDER_NO, FK_PROD_ID, QUANITY , COMPLETE_STATUS)
   --VALUES
   --( :NEW.FK_ORDER_NO, :NEW.FK_PROD_ID, :NEW.QUANITY , :NEW.COMPLETE_STATUS);
   SELECT INVENTORY_ONHAND INTO v_onhand_quantity
   FROM INVENTORY 
   WHERE :NEW.fk_prod_id = INVENTORY.FK_PROD_ID;

  IF( (:NEW.QUANTITY > v_onhand_quantity) 
  THEN Begin
      Rollback Transaction
      raise_application_error (-20001,'ERROR: QUANTITY ' || :NEW.QUANTITY 
       || ' EXCEEDS INVENTORY ONHAND [' || TO_CHAR(v_onhand_quantity) || ']' ); 
     End
  END IF;

END;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top