Your code is OK, but I would recommend some changes:
There is no need of doing this:
select <ORA-00001 msg resulted from the duplicate insert above> into ERR_MSG from dual;
The error is in these variables:SQLCODE and SQLERRM, so
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN -- handles duplicate records error
GrabErr(SCRT_NAME, SQLCODE||' '||SQLERRM);
ROLLBACK; -- I have added this because in a procedure GrabErr there ought to be AUTONOMOUS TRANSACTION
WHEN OTHERS THEN -- handles all other errors
GrabErr(SCRT_NAME, SQLCODE||' '||SQLERRM);
ROLLBACK;
END;
In the procedure GrabErr use AUTONOMOUS TRANSACTION.
You can also define your own application errors.
Here is more about exception handling:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/errors.htm#i7014If you want to know the line where error occurs then add a variable e.g. n_debug:
DECLARE
SCRT_NAME VARCHAR2(400) := 'InsRecOnMytable.sql';
n_debug PLS_INTEGER:=0;
BEGIN
n_debug :=1;
Insert into myschema.mytable (SCHEMA,NAME,REMARK,ORACLE
_TYPE,FUNC
TIONAL_TYP
E,OWNER,DE
TAIL_OWNER
,IS_DYNAMI
C,DISPLAY_
NAME,ENTIT
Y_NAME)
values ('myschema','mytable',' sample table 1','TABLE','BASIC','PRODUC
T','PRODUC
T',0,'Samp
le 1',null);
n_debug :=2;
Insert into myschema.mytable (SCHEMA,NAME,REMARK,ORACLE
_TYPE,FUNC
TIONAL_TYP
E,OWNER,DE
TAIL_OWNER
,IS_DYNAMI
C,DISPLAY_
NAME,ENTIT
Y_NAME)
values ('myschema','mytable',' sample table 2','TABLE','BASIC1','PRODU
CT1','PROD
UCT1',1,'S
ample 2',null);
n_debug :=3;
commit;
GrabErr(n_debug,SCRT_NAME,
'Success');
END IF;
EXCEPTION
GrabErr(n_debug,SCRT_NAME,
SQLCODE||' '||SQLERRM);
ROLLBACK;
WHEN OTHERS THEN -- handles all other errors
GrabErr(n_debug,SCRT_NAME,
SQLCODE||' '||SQLERRM);
ROLLBACK;
END;
I do it this way. You can also create a procedure with input parameter SCRT_NAME and instead of script you can use the procedure.
I hope this will be a little help for you.