问题
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:
回答1:
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 ...
回答2:
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.
来源:https://stackoverflow.com/questions/15566087/how-to-insert-nextval-to-trigger-inside-for-loop