|
-
Here's an example. You may want to clean up the string parsing a bit. You shoul put the code in a stored procedure and then call it in any exception handler that requires it.
scott@AMSAA>
1 begin
2 insert into test2 values(-999, 'test');
3 end;
4*
scott@AMSAA> /
begin
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.TEST2_FK) violated - parent key not found
ORA-06512: at line 2
declare
l_err_tbl user_constraints.table_name%type;
l_cons user_constraints.constraint_name%type;
l_start number;
l_stop number;
-- user defined exception to handle error we are hitting
e_ConsViolation exception;
pragma exception_init(e_ConsViolation, -02291);
begin
insert into test2 values(-999, 'test');
exception
when e_ConsViolation then
l_start := instr( sqlerrm, '(' ) + 1;
l_stop := instr( sqlerrm, ')' );
l_cons := substr(sqlerrm, l_start, l_stop-l_start);
l_start := instr( l_cons, '.' ) + 1;
l_cons := substr(l_cons, l_start, l_stop-l_start);
select table_name
into l_err_tbl
from user_constraints
where constraint_name= (select r_constraint_name
from user_constraints
where constraint_name = l_cons);
dbms_output.put_line('Parent table is ' || l_err_tbl);
when others then
dbms_output.put_line('other section');
end;
/
Parent table is TEST1
PL/SQL procedure successfully completed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|