pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
problem with function in plpgsql
2+ messages / 2 participants
[nested] [flat]

* problem with function in plpgsql
@ 1999-02-19 15:08  Eric BASIER <basier@ipgp.jussieu.fr>
  0 siblings, 1 reply; 2+ messages in thread

From: Eric BASIER @ 1999-02-19 15:08 UTC (permalink / raw)
  To: pgsql-sql

Hello;
I have a table pz like this :
Table    = pz
+----------------------------------+----------------------------------+-------+

|              Field               |              Type                |
Length|
+----------------------------------+----------------------------------+-------+

| cle                              | int4 not null
|     4 |
| titre                            | varchar() not null
|    25 |
| typ                              | char() not null
|     1 |
| in_unit                          | int4 not null
|     4 |
| out_unit                         | int4 not null
|     4 |
| pcterr                           | float8
|     8 |
| a0                               | float8
|     8 |
| af                               | float8
|     8 |
| desctype                         | varchar()
|     4 |
| description                      | int8
|     8 |
+----------------------------------+----------------------------------+-------+

I have to create a function who take me one row like that
 create function test_exist_pz (text,int4,int4,float,float) returns text
as '
declare
  pzrec pz%RowType;
begin
  select * into pzrec
   from pz
   where typ = $1
     and in_unit = $2
     and out_unit = $3
     and A0 = $4
     and AF = $5;
  if not found then
    pzrec.cle = -1;
  end if;
  return pzrec;
end;
' language 'plpgsql';
CREATE
When I try to work with the function it doesn't I have this result :
seed=> select test_exist_pz('a',1,1,1.1,1.2);
ERROR:  attribute 'pzrec' not found
I am not very familiar with plpgsql and so if there is somebody
who can help me or if thre is somebody who can say where can
I find documentation about plpgsql it is very well

Thanks


--
Basier Eric  e-mail : basier@ipgp.jussieu.fr
http://geoscope.ipgp.jussieu.fr
IPGP Observatoires Sismologie-Volcanologie
4,Place Jussieu 75005 Paris Tour 24-14 4eme Etage Tel 01 44 27 38 96






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

* Re: [SQL] problem with function in plpgsql
@ 1999-02-20 11:09  Anatoly K. Lasareff <tolik@icomm.ru>
  parent: Eric BASIER <basier@ipgp.jussieu.fr>
  0 siblings, 0 replies; 2+ messages in thread

From: tolik@icomm.ru @ 1999-02-20 11:09 UTC (permalink / raw)
  To: Eric BASIER <basier@ipgp.jussieu.fr>; +Cc: pgsql-sql

>>>>> "EB" == Eric BASIER <basier@ipgp.jussieu.fr> writes:

 EB> Hello;
 EB> I have a table pz like this :
 EB> Table    = pz
 EB> +----------------------------------+----------------------------------+-------+

. . .

 EB> I have to create a function who take me one row like that
 EB> create function test_exist_pz (text,int4,int4,float,float) returns text
 EB> as '
 EB> declare
 EB> pzrec pz%RowType;
 EB> begin
 EB> select * into pzrec
 EB> from pz
 EB> where typ = $1
 EB> and in_unit = $2
 EB> and out_unit = $3
 EB> and A0 = $4
 EB> and AF = $5;
 EB> if not found then
 EB> pzrec.cle = -1;
 EB> end if;
 EB> return pzrec;
 EB> end;
 EB> ' language 'plpgsql';
 EB> CREATE
 EB> When I try to work with the function it doesn't I have this result :
 EB> seed=> select test_exist_pz('a',1,1,1.1,1.2);
 EB> ERROR:  attribute 'pzrec' not found
 EB> I am not very familiar with plpgsql and so if there is somebody
 EB> who can help me or if thre is somebody who can say where can
 EB> I find documentation about plpgsql it is very well

There is at least one error in your text: function test_exist_pz
returns 'text' type, but you write 'return pzrec', value of
pz%RowType. If you need know - exist or no any row you can retype function:

  create function test_exist_pz (text,int4,int4,float,float) returns bool
  as '
  declare
    c int;
  begin

    select count(*) into pzrec
    from pz
    where typ = $1
    and in_unit = $2
    and out_unit = $3
    and A0 = $4
    and AF = $5;

    return (c > 0);
  end;
' language 'plpgsql';

But this function you can also code in 'sql' but no 'plpgsql' language.

-- 
Anatoly K. Lasareff              Email:       tolik@icomm.ru 
Senior programmer



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


end of thread, other threads:[~1999-02-20 11:09 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-02-19 15:08 problem with function in plpgsql Eric BASIER <basier@ipgp.jussieu.fr>
1999-02-20 11:09 ` Anatoly K. Lasareff <tolik@icomm.ru>

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