I have the following code in a package which inserts data into table.
I am able to get parent key not found exception. How can I specifically get constraint violated message for each column? E.g. if proj_id
is violated then raise exception, if proj_code
is violated, then raise another exception.
PROCEDURE add_project(
p_proj_id project.proj_id%TYPE,
p_proj_desc project.proj_desc%TYPE,
p_proj_code project.proj_code%TYPE,
p_proj_date project.proj_date%TYPE
)
IS
parent_not_found exception;
pragma exception_init(parent_not_found, -2291);
BEGIN
INSERT
INTO projects (proj_id,proj_desc,proj_code,proj_date) values
(p_proj_id,p_proj_desc,p_proj_code,p_proj_date);
exception
when parent_not_found then
raise_application_error(-20001,'Invalid');
END;
Take a look at EXCEPTION_INIT Pragma.
Or you can use
WHEN OTHERS
and look for theSQLCODE
.EDIT:
Note that you are not forced to manually check that the parent key exists to raise the exception, you can stay with :
But if you want to be able to get violated constraint name easily, it can be useful to raise exception manually for each column. Also you can try to get the constraint name using
USER_CONSTRAINTS
andUSER_CONS_COLUMNS
.Another way to get the violated constraint name is to parse the
SQLERRM
error message.