Question

Here is a code for trigger and it have a for loop. When trigger is fired (INSERT OR UPDATE) there's another table data must include it is MICL_SUP

 OPEN  projMgrsCursor;
        LOOP
        FETCH projMgrsCursor INTO projMgr;  
        select micl_sup_id_seq.nextval into  SUPID from dual;
        insert into MICL_SUP VALUES ((SUPID), (SELECT SYSDATE FROM DUAL),                             :NEW.ENTRYADDEDBY_EMP_NO, 3000, 0,projMgr, NULL,:NEW.EMP_NO);
       END LOOP;    

      CLOSE projMgrsCursor;

This is the table structure. Sup_ID primary and unique key . I can't make any changes to table structure

SUP_ID  -primary key             
ASSIGNED_DATE            
ASSIGNED_BY_EMP_NO          
AMOUNT_LIMIT            
IS_OVVERRIDDEN      
SUP_EMP_NO          
RTD_EMP     
EMP_NO  

To enter sup_ID I use select micl_sup_id_seq.nextval into SUPID from dual; but when I run this code there's an error "RA-00001: unique constraint violated" (this is not a compilation error ) Is there any other way to add sup_ID? Where have I gone wrong?

This question is related with this trigger PlSQL trigger error ORA-0000 ORA-06512:

Était-ce utile?

La solution

Why not including calculation of micl_sup_id_seq.nextval into your cursor?

cursor  projMgrsCursor is 
  select b.BU_MEMBER_ID, micl_sup_id_seq.nextval SUPID
  from ...

Autres conseils

Try rewriting your code as:

DECLARE
  nSupid   NUMBER;
  projMgr  VARCHAR2(32767);
BEGIN
  OPEN  projMgrsCursor;

  LOOP
    FETCH projMgrsCursor INTO projMgr;
    EXIT WHEN projMgrsCursor%NOTFOUND;

    select micl_sup_id_seq.nextval into nSUPID from dual;

    insert into MICL_SUP 
      (SUPID, ASSIGNED_DATE, ASSIGNED_BY_EMP_NO,   AMOUNT_LIMIT,
       IS_OVERRIDDEN, SUP_EMP_NO, RTD_EMP, EMP_NO)
    VALUES
      (nSupid, SYSDATE,  :NEW.ENTRYADDEDBY_EMP_NO, 3000,
       0,              projMgr,   NULL,    :NEW.EMP_NO);
   END LOOP;    

  CLOSE projMgrsCursor;

  DBMS_OUTPUT.PUT_LINE('Successful completion');
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception: ' || SQLCODE || '  ' || SQLERRM);
    RAISE;
END;

Share and enjoy.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top