Re: plpgsql: help with RECORD type - Mailing list pgsql-novice

From Vyacheslav Kalinin
Subject Re: plpgsql: help with RECORD type
Date
Msg-id 9b1af80e0903031319m123f1f39r3f1886c7ba467970@mail.gmail.com
Whole thread Raw
In response to Re: plpgsql: help with RECORD type  (Andreas Kretschmer <akretschmer@spamfence.net>)
Responses Re: plpgsql: help with RECORD type
List pgsql-novice
There is no set variables in plpgsql. If you want to retrieve set of rows for futher manipulation either use a CURSOR or ARRAY:

declare
  cur refcursor;
begin
  open cur for
    select * from table where ...;

  loop
    fetch cur into var1,var2...;
    exit when not found;
  end loop;



declare
  arr offsite_batch[];
  i int;
begin
   select array(
    select * from offsite_batch where ...
   ) into arr;

  for i in 1..array_upper(arr,1)
  loop
    var1 := arr[i].id;
    ...
  end loop;

Note that ARRAYs of composite type only appeared in 8.3.

I believe what you want to achieve can be done with simple query result looping:

for batch in (
  select *  from offsite_batch
   where closed is NULL
   order by age(opened) ASC
) loop
  batch_found := true;
  IF NOT unique THEN
      RAISE WARNING 'more than one open batch found, closing...';
      UPDATE offsite_batch
             SET     closed = now()
        WHERE   batch_id = batch.id;
  ELSE
     latest_batch    := batch;
     unique          := FALSE;
  END IF;
end loop;

if not batch_found then
  RAISE NOTICE 'creating new offsite batch';
  ...
end if;

pgsql-novice by date:

Previous
From: Andreas Kretschmer
Date:
Subject: Re: plpgsql: help with RECORD type
Next
From: Semyon Chaichenets
Date:
Subject: Re: plpgsql: help with RECORD type