Pular para o conteúdo

Fóruns Banco de dados Oracle Pegando erros Pegando erros

#83732
Avatar photoLeonardo Litz
Participante

    Ola Cleber….

    Cara tem um procedure que utilizo para traduzir os erros que ocorrem….

    compile ela em seu banco de dados

    create procedure SYS_TRATA_ERRO
    is
    msg_err varchar2(512);
    str varchar2(512);
    campo varchar2(30);
    vlrs varchar2(512);
    i integer;
    j integer;
    recs integer;
    cod_err integer;
    begin
    msg_err := sqlErrM;
    cod_err := abs(sqlCode);

    if cod_err = 2290 then — Constraint Check…
    for i in 1..length(msg_err)
    loop
    if substr(msg_err,i,1) = ‘(‘ then
    for j in 1..length(msg_err)
    loop
    if substr(msg_err,-j,1) = ‘)’ then
    str := substr(msg_err,i+1,length(msg_err)-(i+j));
    exit;
    end if;
    end loop;
    exit;
    end if;
    end loop;

    for i in 1..length(str)
    loop
      if substr(str,i,1) = '.' then
        str := substr(str,i+1,length(str)-i);
        exit;
      end if;
    end loop;
    
    select COLUMN_NAME into campo from USER_CONS_COLUMNS
      where CONSTRAINT_NAME = str;
    
    select SEARCH_CONDITION into vlrs from USER_CONSTRAINTS
      where CONSTRAINT_NAME = str;
    
    for i in 1..length(vlrs)
    loop
      if substr(vlrs,i,1) = '(' then
        for j in 1..length(vlrs)
        loop
          if substr(vlrs,-j,1) = ')' then
            str := substr(vlrs,i+1,length(vlrs)-(i+j));
            exit;
          end if;
        end loop;
        exit;
      end if;
    end loop;
    
    msg_err := 'Valores Validos: (' || str || ')';
    msg_err := '|' || abs(cod_err) || '|' || campo || '|' || msg_err || '|';
    

    elsif cod_err in (1400,1407) then — Constraint not null
    for i in 1..length(msg_err)
    loop
    if substr(msg_err,i,1) = ‘(‘ then
    for j in 1..length(msg_err)
    loop
    if substr(msg_err,-j,1) = ‘)’ then
    msg_err := substr(msg_err,i+1,length(msg_err)-(i+j));
    exit;
    end if;
    end loop;
    exit;
    end if;
    end loop;

    for i in 1..length(msg_err)
    loop
      if substr(msg_err,-i,1) = '"' then
        campo := '';
        for j in (i+1)..length(msg_err)
        loop
          if substr(msg_err,-j,1)  '"' then
            campo := substr(msg_err,-j,1)||campo;
          else
            exit;
          end if;
        end loop;
        exit;
      end if;
    end loop;
    
    msg_err := 'Nao pode ser nulo!';
    msg_err := '|' || abs(cod_err) || '|' || campo || '|' || msg_err || '|';
    

    elsif cod_err = 1 then — Constraint Unique
    for i in 1..length(msg_err)
    loop
    if substr(msg_err,i,1) = ‘(‘ then
    for j in 1..length(msg_err)
    loop
    if substr(msg_err,-j,1) = ‘)’ then
    str := substr(msg_err,i+1,length(msg_err)-(i+j));
    exit;
    end if;
    end loop;
    exit;
    end if;
    end loop;

    for i in 1..length(str)
    loop
      if substr(str,i,1) = '.' then
        str := substr(str,i+1,length(str)-i);
        exit;
      end if;
    end loop;
    
    select count(1) into recs from USER_IND_COLUMNS
      where INDEX_NAME = str;
    
    if recs = 1 then
      select COLUMN_NAME into campo from USER_IND_COLUMNS
        where INDEX_NAME = str;
    
      msg_err := 'Nao pode ser duplicado!';
      msg_err := '|' || abs(cod_err) || '|' || campo || '|' || msg_err || '|';
    end if;
    

    end if;

    — Formatacao da mensagem…
    if not msg_err like ‘%|%’ then
    msg_err := ‘|’ || abs(cod_err) || ‘|$NONE$|’ || msg_err || ‘|’;
    end if;

    raise_application_error(-20001,msg_err);
    end SYS_TRATA_ERRO;

    …….

    depois de compilado

    vou deve colocar assim no bloco

    begin
    …..comando….
    exception when others then
    sys_trata_erro();
    end;

    vlw? Leonardo Litz