Pular para o conteúdo
Visualizando 13 posts - 1 até 13 (de 13 do total)
  • Autor
    Posts
  • #83785
    airoosp
    Participante

      Boa tarde,

      Alguém sabe como fazer para capturar o erro através do exception identificando se possível o nome do campo quando o erro for o código abaixo:

      ORA-01401 valor inserido grande demais para a coluna

      Usando SQLCODE e SQLERRM só consigo obter as informações acima.

      Obrigado.

      Airton

      #83786
      Avatar photoLeonardo Litz
      Participante

        Compile esta procedure e a utilize no others do exception

        begin
        .
        .
        .
        exception when others then
        sys_trata_erro;
        end;

        create or replace 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;

        Vlw Leonardo Litz

        #83788
        airoosp
        Participante

          Compilei a procedure, fiz um teste mas não foi possível identificar qual coluna esta causando o erro, a procedure SYS_TRATA_ERRO exibe a mesma mensagem que o raise.

          ORA-01401: valor inserido grande demais para a coluna

          Obrigado.

          #83789
          Avatar photoLeonardo Litz
          Participante

            Qual a versão do seu banco de dados ai?

            #83790
            airoosp
            Participante

              9i

              #83791
              Avatar photoLeonardo Litz
              Participante

                Ixi cara.. no 9i eu não conheco nenhuma forma de fazer isso, mas no 10g dá para fazer isso com certeza.

                #83799
                Rodrigo Mesquita
                Participante

                  Não seria melhor vc tratar o código para que não possa inserir um valor maior que a precisão da coluna?

                  #83801
                  airoosp
                  Participante

                    Entendi, você tem algum exemplo de rotina para tratar o código, esta tabela tem 15 campos.

                    Obrigado.

                    #83804
                    Rodrigo Mesquita
                    Participante

                      Como é que voce está fazendo esse insert? é dentro de uma procedure? alguma aplicação?

                      #83807
                      Avatar photoLeonardo Litz
                      Participante

                        Cara faz um length(variavel) > tamanho do campo testando se o tamanho da variavel é maior que o campo suporta.

                        Sacas?

                        #83823
                        airoosp
                        Participante

                          O insert é feito dentro de uma procedure, passando o comando para uma variável e depois executando através do execute immediate.

                          Exemplo:

                          lc_insert varchar2(2000);

                          lc_insert:=’insert into ……

                          select ……

                          begin
                          execute immediate lc_insert;

                          exception
                          when others then

                          end;

                          #83827
                          Rodrigo Mesquita
                          Participante

                            Na procedure na hora de montar o insert ou pegue os valores e coloque antes de montar a string em variáveis utilizando o tabela.campo%type para definir o tamanho, ou caso seja algo parametrizado aonde a tabela não é fixa compare o tamanho (length(coluna) dos valores nas colunas com o tamanho definido nesta view Select data_precision From user_tab_cols Where table_name = ‘NOME DA TABELA’

                            #83830
                            airoosp
                            Participante

                              Valeu, obrigado pela ajuda.

                            Visualizando 13 posts - 1 até 13 (de 13 do total)
                            • Você deve fazer login para responder a este tópico.