pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Kind of error-handler in a pgsql function
5+ messages / 3 participants
[nested] [flat]

* Kind of error-handler in a pgsql function
@ 2001-07-04 19:37 DI Hasenöhrl <i.hasenoehrl@aon.at>
  2001-07-05 00:38 ` Re: Kind of error-handler in a pgsql function Tom Lane <tgl@sss.pgh.pa.us>
  2001-07-05 03:12 ` Re: Kind of error-handler in a pgsql function Alex Pilosov <alex@pilosoft.com>
  0 siblings, 2 replies; 5+ messages in thread

From: DI Hasenöhrl @ 2001-07-04 19:37 UTC (permalink / raw)
  To: pgsql-sql

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

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Kind of error-handler in a pgsql function
  2001-07-04 19:37 Kind of error-handler in a pgsql function DI Hasenöhrl <i.hasenoehrl@aon.at>
@ 2001-07-05 00:38 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 5+ messages in thread

From: Tom Lane @ 2001-07-05 00:38 UTC (permalink / raw)
  To: DI Hasenöhrl <i.hasenoehrl@aon.at>; +Cc: pgsql-sql

=?iso-8859-1?Q?DI_Hasen=F6hrl?= <i.hasenoehrl@aon.at> writes:
> I want to know the result of an insert or update, because I call these func=
> tions from an Access form and the next steps of the program depend on these=
>  results.

See the FOUND variable (if you just want to test zero or not-zero rows
processed) or the SET DIAGNOSTICS statement to get the row count.

There is no way to trap an error inside a plpgsql function, however.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Kind of error-handler in a pgsql function
  2001-07-04 19:37 Kind of error-handler in a pgsql function DI Hasenöhrl <i.hasenoehrl@aon.at>
@ 2001-07-05 03:12 ` Alex Pilosov <alex@pilosoft.com>
  2001-07-05 08:02   ` Re: Kind of error-handler in a pgsql function DI Hasenöhrl <i.hasenoehrl@aon.at>
  1 sibling, 1 reply; 5+ messages in thread

From: Alex Pilosov @ 2001-07-05 03:12 UTC (permalink / raw)
  To: DI Hasenöhrl <i.hasenoehrl@aon.at>; +Cc: pgsql-sql

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
> 




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Kind of error-handler in a pgsql function
  2001-07-04 19:37 Kind of error-handler in a pgsql function DI Hasenöhrl <i.hasenoehrl@aon.at>
  2001-07-05 03:12 ` Re: Kind of error-handler in a pgsql function Alex Pilosov <alex@pilosoft.com>
@ 2001-07-05 08:02   ` DI Hasenöhrl <i.hasenoehrl@aon.at>
  2001-07-05 14:29     ` Re: Kind of error-handler in a pgsql function Alex Pilosov <alex@pilosoft.com>
  0 siblings, 1 reply; 5+ messages in thread

From: DI Hasenöhrl @ 2001-07-05 08:02 UTC (permalink / raw)
  To: Alex Pilosov <alex@pilosoft.com>; +Cc: pgsql-sql

Thank you for your example.
I understand, in order to avoid inserting *duplicate keys*, first I try to update, when it fails make an insert. But other errors may occur, is there a possibility to distinguish between errors by an error-number.
Please can you tell me, where I can find system functions, variables like *diagnostics, row_count, found, and so on.....*
I studied the docu in http://www.ca.postgresql.org/devel-corner/docs/postgres/programmer.html, but I didn't find

Many thanks in advance
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
  > 


  ---------------------------(end of broadcast)---------------------------
  TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Kind of error-handler in a pgsql function
  2001-07-04 19:37 Kind of error-handler in a pgsql function DI Hasenöhrl <i.hasenoehrl@aon.at>
  2001-07-05 03:12 ` Re: Kind of error-handler in a pgsql function Alex Pilosov <alex@pilosoft.com>
  2001-07-05 08:02   ` Re: Kind of error-handler in a pgsql function DI Hasenöhrl <i.hasenoehrl@aon.at>
@ 2001-07-05 14:29     ` Alex Pilosov <alex@pilosoft.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Alex Pilosov @ 2001-07-05 14:29 UTC (permalink / raw)
  To: DI Hasenöhrl <i.hasenoehrl@aon.at>; +Cc: pgsql-sql

There is _no error handling_ in plpgsql

You can find documentation at:
http://www.postgresql.org/idocs/index.php?plpgsql.html

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

> Thank you for your example. I understand, in order to avoid inserting
> *duplicate keys*, first I try to update, when it fails make an insert.
> But other errors may occur, is there a possibility to distinguish
> between errors by an error-number. Please can you tell me, where I can
> find system functions, variables like *diagnostics, row_count, found,
> and so on.....* I studied the docu in
> http://www.ca.postgresql.org/devel-corner/docs/postgres/programmer.html,
> but I didn't find
> 
> Many thanks in advance
> 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
>   > 
> 
> 
>   ---------------------------(end of broadcast)---------------------------
>   TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> 
> 




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2001-07-05 14:29 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-07-04 19:37 Kind of error-handler in a pgsql function DI Hasenöhrl <i.hasenoehrl@aon.at>
2001-07-05 00:38 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-07-05 03:12 ` Alex Pilosov <alex@pilosoft.com>
2001-07-05 08:02   ` DI Hasenöhrl <i.hasenoehrl@aon.at>
2001-07-05 14:29     ` Alex Pilosov <alex@pilosoft.com>

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