rivi10
MIS
- Dec 17, 2010
- 16
Hi,
In the following cursor, I need to raise an exception when the Insert in the Loop fails.
My objective is, if all the records retrieved by the cursor are NOT inserted, then abort the insert transaction and do not commit.
Should my EXCEPTION be just before the loop ends or after the loop ends?
DECLARE
CURSOR c_get_employ IS
SELECT EMP.EMPLOYER_ID, EMP.EMPLOYER_NAME, EMP.EMPLOYER_LOCATION
FROM EMP
WHERE EMP.EMPLOYER_COUNTRY = 'USA'
ORDER BY EMP.EMPLOYER_ID;
BEGIN
-- Loop through the cursor and start inserting the data
FOR c_post_employ IN c_get_employ
LOOP
INSERT INTO EMP_OUTPUT
(EMPID, EMPNAME, EMPLOC)
VALUES
(c_post_employ.EMPLOYER_ID,c_post_employ.EMPLOYER_NAME,c_post_employ.EMPLOYER_LOCATION);
END LOOP;
WHEN OTHERS THEN
msg.message_code := 101;
msg.message := 'Unable to insert into EMP_OUTPUT Table for LOT Number-';
RAISE emp_error;
COMMIT;
EXCEPTION
WHEN emp_error THEN
DBMS_OUTPUT.PUT_LINE(msg.message_code, msg.message,SQLCODE,SQLERRM,USER);
END;
In the following cursor, I need to raise an exception when the Insert in the Loop fails.
My objective is, if all the records retrieved by the cursor are NOT inserted, then abort the insert transaction and do not commit.
Should my EXCEPTION be just before the loop ends or after the loop ends?
DECLARE
CURSOR c_get_employ IS
SELECT EMP.EMPLOYER_ID, EMP.EMPLOYER_NAME, EMP.EMPLOYER_LOCATION
FROM EMP
WHERE EMP.EMPLOYER_COUNTRY = 'USA'
ORDER BY EMP.EMPLOYER_ID;
BEGIN
-- Loop through the cursor and start inserting the data
FOR c_post_employ IN c_get_employ
LOOP
INSERT INTO EMP_OUTPUT
(EMPID, EMPNAME, EMPLOC)
VALUES
(c_post_employ.EMPLOYER_ID,c_post_employ.EMPLOYER_NAME,c_post_employ.EMPLOYER_LOCATION);
END LOOP;
WHEN OTHERS THEN
msg.message_code := 101;
msg.message := 'Unable to insert into EMP_OUTPUT Table for LOT Number-';
RAISE emp_error;
COMMIT;
EXCEPTION
WHEN emp_error THEN
DBMS_OUTPUT.PUT_LINE(msg.message_code, msg.message,SQLCODE,SQLERRM,USER);
END;