Link to home
Start Free TrialLog in
Avatar of cutie_smily
cutie_smily

asked on

Exception handling for insert statement

Need a simple program that inserts records into a table and delete some records after the insert is successful. I have to check for exceptions and commit at the right place. I not sure what exceptions I should check for the insert and when to do the commit.

Here is the same proc I wrote. Please suggest a better way of handling exception and deletes after the insert is successful.

create or replace procedure insert_adj_rec (P_btch_id batch.btchid%type)
IS
l_trans_id      trans.trans_id%type := 123;

BEGIN
INSERT INTO TABLE1 (
COL1,
COL2,
COL3)
VALUES (
SELECT COL1,
COL2,
amount * -1 ------make negative to postive
FROM TABLE2
WHERE BTCH_ID = P_BATCH_ID
AND trans_id = l_Trans_id
and amount < 0);

---after insert is success delete negative amounts
delete from table2
where amount < 0;

COMMIT;                
EXCEPTION
      WHEN DUP_VAL_ON_INDEX THEN ---not sure if this is right
      DBMS_OUTPUT.PUT_LINE ('DUPLICATE RECORD - ' || SQLERRM);
      ROLLBACK;
END;

Max points because of urgency.
ASKER CERTIFIED SOLUTION
Avatar of Franck Pachot
Franck Pachot
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cutie_smily
cutie_smily

ASKER

Appreciate your input. I do got your point and I totally agree with you. As this is a one time fix to cleanup (remove negative amounts) in the existing data I am asked to write a simple procedure to delete the data in the existing stage tables. I have to perform this in procedure only not just an sql statement. I thought I should write in a better way than just writing an insert statement in the procedure.

Please suggest a better way of writing this procedure.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Appreciate all the inputs. Please help me on the below warning messages.

Warning(1): PLW-06015: parameter PLSQL_DEBUG is deprecated; use PLSQL_OPTIMIZE_LEVEL = 1
Warning(1): PLW-06013: deprecated parameter PLSQL_DEBUG forces PLSQL_OPTIMIZE_LEVEL <= 1
Warning(2,1): PLW-05018: unit SP_hpw_ADJ omitted optional AUTHID clause; default value DEFINER used
Warning(100,1): PLW-06009: procedure "SP_hpw_ADJ" OTHERS handler does not end in RAISE or RAISE_APPLICATION_ERROR
Try this and compile the procedure
 
ALTER SESSION SET PLSQL_WARNINGS='ENABLE:SEVERE','DISABLE:PERFORMANCE','DISABLE:INFORMATIONAL';

 
Thanks for all the help and apologize for the late reply.

"i dont think u can use directly SQLERRM in the dbms_output"
I tried the below two statements in the exception and both worked so I think we can use the sqlerrm in the dbms_output.

DBMS_OUTPUT.PUT_LINE('SQLERRM: '||SQLERRM);
DBMS_OUTPUT.PUT_LINE('SQLCODE: '||SQLCODE);