plsqlunique-constraintnextvaldatabase-trigger

How to insert nextval to trigger inside for loop


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:


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 ...