Re: Catching DML exceptions in PL/pgSQL

From: Tomasz Myrta <jasiek(at)klaster(dot)net>
To: Radu-Adrian Popescu <radu(dot)popescu(at)aldratech(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Catching DML exceptions in PL/pgSQL
Date: 2003-06-17 09:44:36
Message-ID: 3EEEE304.1010106@klaster.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Dnia 2003-06-17 11:25, Użytkownik Radu-Adrian Popescu napisał:

> Hello all,
> (and sorry if this has been aswered before)
>
> Take this piece of code for example:
> .....................
> begin
> _res.code:=1;
> select id into iid from log where id=_id;
> if not found then begin
> _res.msg:=''insert'';
> *insert into log (log, data) values (_log, _data);
> if not found* then begin
> _res.msg:=_res.msg || '' error'';
> _res.code:=-1;
> end;
> end if;
> end;
> else begin
> .....................
> The thing is if _data (parameter) is null and table has a (data <> null)
> check, the insert would fail and abort the function before my "if not
> found" test.
>
> I'm porting a java app. from mssql to postgresql, and the java code
> relies on the stored procedure to always return it's status (in
> _res.code in this case).
>
> Is there anything I can do to make sure the function always returns _res ?
> Something along the lines of Oracle's exception handling, or the @@error
> trick in mssql ?
You can't do it this way. Your insert is violation of some constraint
(problably "not null" or "primary key" constraint). This kind of violation
raises exception and whole transaction is aborted. I don't use java, but C
librares raises also ordinary C exception which can be easily caught. If you
want to avoid such cases - check your data before inserting them.

Regards,
Tomasz Myrta

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Radu-Adrian Popescu 2003-06-17 09:50:24 Re: Catching DML exceptions in PL/pgSQL
Previous Message Radu-Adrian Popescu 2003-06-17 09:25:06 Catching DML exceptions in PL/pgSQL