I am new to PL/SQL and I do not completely understand error-code parameter of the RAISE_APPLICATION_ERROR.
In the documentation it says The error_number is a negative integer with the range from -20999 to -20000. But that does not explain if I can pick random number or is there a system to it.
Also I am wondering if I can reuse the same numbers in different functions when calling RAISE_APPLICATION_ERROR? Or will that number be used somewhere to identify that particular error?
I also was looking at the procedure someone wrote and wondering why didn't they just pass SQLCODE to RAISE_APPLICATION_ERROR instead of taking random number like RAISE_APPLICATION_ERROR(l_err_code, 'Record already exists!');
I got an impression that one only uses integers -20999 to -20000 for the user-defined errors, not existing ones.
PROCEDURE insert_record
(v_row IN OUT TABLE1%ROWTYPE) IS
l_err_code NUMBER;
l_err_message VARCHAR(200);
BEGIN
INSERT INTO TABLE1 ....
RETURNING id INTO V_row.id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
l_err_code := SQLCODE;
l_err_message := 'Insert Error: ' - ' || SQLERRM;
...logging error here
RAISE_APPLICATION_ERROR(-20001, 'Record already exists!');
WHEN OTHERS THEN
l_err_code := SQLCODE;
l_err_message := 'Insert Error: ' - ' || SQLERRM;
--- loggin error here
RAISE_APPLICATION_ERROR(-20002, l_err_message);
END insert_record;
Would it make sense to modify above function as follows?:
PROCEDURE insert_record
(v_row IN OUT TABLE1%ROWTYPE) IS
l_err_code NUMBER;
l_err_message VARCHAR(200);
BEGIN
INSERT INTO TABLE1 ....
RETURNING id INTO V_row.id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
l_err_code := SQLCODE;
l_err_message := 'Record already exists!';
...logging error here
RAISE_APPLICATION_ERROR(l_err_code, l_err_message);
WHEN OTHERS THEN
l_err_code := SQLCODE;
l_err_message := 'Insert Error: ' - ' || SQLERRM;
--- loggin error here
RAISE_APPLICATION_ERROR(l_err_code, l_err_message);
END insert_record;
I'd really appreciate if someone could answer those questions for me or point me to some documentation clarifying those.