A
A
Alexey Sumin2013-08-19 14:59:33
Oracle
Alexey Sumin, 2013-08-19 14:59:33

How to make custom error text for constraint?

When a foreign key is made in Oracle with ON DELETE = RESTRICT, then when trying to delete a parent record, an error is thrown: “ORA-02292: violated integrity constraint (owner.constraintname)- child record found”, is it possible somehow this error for each constraint to clarify so that more meaningful text is displayed, for example, “You can’t delete this picture because it is associated with a video”?
Can be a trigger or a constraint comment.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Sumin, 2013-08-20
@asumin

parsing the text of the error in the application for its Russification is generally a very dubious practice

X
xtender, 2013-10-05
@xtender

It cannot be replaced, but it can be supplemented:

create or replace trigger catch_unique
   after servererror on database
   disable
declare
   l_sql_text   ora_name_list_t;
   l_msg        varchar2(2000) := null;
   l_stmt       varchar2(2000) := null;
begin
  if IS_SERVERERROR(1) then
      /* если нужен текст ошибки, запроса и тд:
      for depth in 1 .. ora_server_error_depth loop
        l_msg := l_msg || ora_server_error_msg(depth);
      end loop;

      for i in 1 .. ora_sql_txt(sql_text) loop
         l_stmt := l_stmt || sql_text(i);
      end loop;
      */
      raise_application_error(-20001,'Ooops!');
  end if;
end;
/
sho err;
--если ошибок нет, то включаем:
alter trigger catch_unique enable;
--а теперь проверка:
SQL> insert into xt_unique1 values(1);
insert into xt_unique1 values(1)
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-20001: Ooops!
ORA-06512: at line 16
ORA-00001: unique constraint (XTENDER.SYS_C0043662) violated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question