El bloque de excepción en pl / sql 'Función' no detecta la condición de error

I have the following function which i call from another procedure within the same pl/sql package.

Function lf_get_query(p_table in varchar2) return varchar2
    v_where_clause  varchar2(500);
    Case upper(p_table)
        When 'TABLEA'   then                    
            v_where_clause := ' Where trunc(x.purchase_dtm) < ' || '''' || trunc(v_check_Date) || ''''; 
        When 'TABLEB'   then                    
            v_where_clause := ' Where trunc(x.purchase_dtm) < ' || '''' || trunc(v_check_Date) || ''''
                                || ' And product_type='ABC'
                                || ' And customer_type='XXX'
                                || ' And contract_type='YYY';   
            raise ex_unknown_table_type;                    
    End case;           
    return v_where_clause;
    When ex_unknown_table_type then
        [[Log to file that table is urecognised. ]]
    When others then
        [[Log to file that table is urecognised and include sqlerrm ]]

When i call the function, it generates the following error:

ORA-06502: PL/SQL: numeric or value error: character string buffer too small

The error is generated because the variable v_where_clause is not large enough for some of the strings i am trying to store in the variable. What i don't understand is when the error occurs, it is not caught by any of the two exception clauses shown above. Instead the error is being caught on the exception block for the procedure that is calling this function.

The reason i know it is not being caught by the exception clauses in the function is that both exception clauses are supposed to log the error condition to a file but they are not.

Is there any reason for this? Shouldn't the exception be caught by the 'WHEN OTHERS' exception block?

Also, is there any way i can declare the v_where_clause variable without specifying a size?

Muchas Gracias

preguntado el 08 de noviembre de 11 a las 14:11

Specify your VARCHAR2 variable to a very large size i.e. 32767 and Oracle will automatically give it just enough storage to cover what you put into it. it won't set aside 32767 bytes arbitrarily. -

1 Respuestas

Is the exception block of the calling procedure issuing a rollback? If so, it will rollback your logging along with everything else. The way you get around this is to use a procedure that is defined with PRAGMA AUTONOMOUS_TRANSACTION, which will allow your logging to occur outside of the current transaction.

respondido 08 nov., 11:18

Oh im so stupid. The log entries that were logged in the table were being rolled back. Thanks!!!!!!!! - ziggy

Happens to all of us at least once ;-) - DCookie

+1 Writing log records is the , de manera valid use for AUTONOMOUS TRANSACTION. - APC

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.