pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alex Pilosov <alex@pilosoft.com>
To: DI Hasenöhrl <i.hasenoehrl@aon.at>
Cc: pgsql-sql@postgresql.org
Subject: Re: Kind of error-handler in a pgsql function
Date: Wed, 4 Jul 2001 23:12:03 -0400 (EDT)
Message-ID: <Pine.BSO.4.10.10107042305140.7004-100000@spider.pilosoft.com> (raw)
In-Reply-To: <00c401c104c0$b9a03200$01011eac@irina>

You must do it the other way around: 

First, try update, then see how many records were updated, if there were 0
records updated, then do the insert.

Currently, plpgsql lacks decent exception handling. 

Sample code:

create function ...
...
declare rec_affected int;
begin
  update ... 
  get diagnostics rec_affected = ROW_COUNT;
  if rec_affected = 0 then  
    insert ...
  end if;
end;

On Wed, 4 Jul 2001, [iso-8859-1] DI Hasenöhrl wrote:

> Hi,
> 
> When I write in psql:
> testdb=# update table1 set tableattribute='any' where table_nr=1;
>     if a tuple exists, I get this message 
> testdb=# update 1
>     if no tuple with table_nr=1 exists, I get this message
> testdb=# update 0
>     
> 
> Is there a possibility to make a difference in a pgsql function like this:
> create function updTable(text,integer) returns int AS
> 'DECLARE
>    msg ALIAS FOR $1;
>    nr    ALIAS FOR $2;
>  BEGIN
>      update table1 set tableattribute=msg where table_nr=nr;
>      --pseudocode
>        if update = 0 then
>          return 0;
>        else
>          return 1;
>        end if;
>    END;
> 'language 'plpgsql';
> 
> or for a function, which inserts data:
> create function insTable(text,integer) returns int AS
> 'DECLARE
>    msg ALIAS FOR $1;
>    nr    ALIAS FOR $2;
>  BEGIN
>      insert into table1 values (nr,msg);
>      --pseudocode
>        if error= cannot insert duplicate key.....then
>          return 0;
>        else
>          return 1;
>        end if;
>    END;
> 'language 'plpgsql';
> 
> I want to know the result of an insert or update, because I call these functions from an Access form and the next steps of the program depend on these results.
> 
> I hope, someone can help me, because I didn't find anything in the docu or mailing list.
> Thanks in advance
> Irina
> 
> E-Mail: i.hasenoehrl@aon.at
> 




view thread (5+ messages)  latest in thread

Message-ID: <Pine.BSO.4.10.10107042305140.7004-100000@spider.pilosoft.com>
Permalink:  ../Pine.BSO.4.10.10107042305140.7004-100000@spider.pilosoft.com/
Also on:    postgresql.org/message-id/Pine.BSO.4.10.10107042305140.7004-100000@spider.pilosoft.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: alex@pilosoft.com, i.hasenoehrl@aon.at
  Subject: Re: Kind of error-handler in a pgsql function
  In-Reply-To: <Pine.BSO.4.10.10107042305140.7004-100000@spider.pilosoft.com>
  References: <00c401c104c0$b9a03200$01011eac@irina> <Pine.BSO.4.10.10107042305140.7004-100000@spider.pilosoft.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