.comment-link {margin-left:.6em;}

Wednesday, October 18, 2006

PRAGMA AUTONOMOUS_TRANSACTION

O pragma autonomous_transaction pode ser usado para capturar erros durante a execução mesmo após um rollback. Fiz um exemplo muito simples para um colega hoje.

Explicando o teste: a tabela "t_errors" receberá o erro da divisão por zero, porém há um insert antes que não se efetivará pelo saída de exceção "WHEN OTHERS" e, claro, não podia deixar de demonstrar como usar o WHEN OTHERS corretamente, ou seja, com um RAISE no final!

Esse teste me renderá 4 chopps! :-)

ops$marcio@LUKE9I> create table t_errors (
2 dt date,
3 x varchar2(4000)
4 );

Table created.

ops$marcio@LUKE9I> create table t ( dt date );

Table created.

ops$marcio@LUKE9I> select * from t;

no rows selected

ops$marcio@LUKE9I> select * from t_errors;

no rows selected

ops$marcio@LUKE9I> declare
2 l_number number;
3 procedure err ( p_errmsg in varchar2 )
4 is
5 PRAGMA AUTONOMOUS_TRANSACTION;
6 begin
7 insert into t_errors values ( sysdate, p_errmsg );
8 commit;
9 end;
10 begin
11 insert into t values ( sysdate );
12 l_number := 10/0;
13 commit;
14 exception
15 when others then
16 rollback;
17 err( sqlerrm );
18 raise;
19 end;
20 /
declare
*
ERROR at line 1:
ORA-01476: divisor is equal to zero
ORA-06512: at line 18


ops$marcio@LUKE9I> select * from t;

no rows selected

ops$marcio@LUKE9I> select * from t_errors;

DT X
------------------- ------------------------------------------------------------
18/10/2006 01:11:44 ORA-01476: divisor is equal to zero

1 row selected.

Labels:


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?