pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Radu-Adrian Popescu <radu.popescu@aldratech.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Catching DML exceptions in PL/pgSQL
Date: Tue, 17 Jun 2003 02:56:30 -0700
Message-ID: <3EEEE5CE.1000509@joeconway.com> (raw)
In-Reply-To: <5.2.1.1.0.20030617121526.02185a50@192.168.0.1>
References: <5.2.1.1.0.20030617121526.02185a50@192.168.0.1>

Radu-Adrian Popescu wrote:
> .....................
> 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.

You could test for _data is null, and if so check attnotnull in 
pg_attribute. E.g. something like:

declare
   iattnotnull bool
[...]
begin
   if _data is null then
     select into iattnotnull attnotnull from pg_catalog.pg_attribute
     where attrelid = 'log'::regclass and attname = 'data';
     if iattnotnull then
       _res.code := -1;
       [...]

> 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 ?

There is currently no way to "catch" the exception in PL/pgSQL, but 
maybe the above would work for you.

HTH,

Joe




view thread (7+ messages)  latest in thread

Message-ID: <3EEEE5CE.1000509@joeconway.com>
Permalink:  ../3EEEE5CE.1000509@joeconway.com/
Also on:    postgresql.org/message-id/3EEEE5CE.1000509@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, radu.popescu@aldratech.com
  Subject: Re: Catching DML exceptions in PL/pgSQL
  In-Reply-To: <3EEEE5CE.1000509@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox