什么相当于 Firebird SQL 'for do' 和 'select into' for postgresql

What is equivalent of Firebird SQL 'for do' and 'select into' for postgresql

在 Firebird SQL 存储过程中,我在 'for do' 循环中使用了 'select into',但我没有找到 pg 函数的等效项。

         for  select purchase.quantity, purchase.purchasevalue, purchase.purchased, purchase.id from purchase
                            join cellarbook cb on purchase.fk_cellarbook_id = cb.id
                            join bottle bot on cb.fk_bottle_id = bot.id
                            where bot.id = :bottleid
                            order by purchase.purchased ASC
                            into :purquantity, :purvalue, :purdate, :purid
                do
                begin
                    /* calculate quantity on hand at point of purchase
                       here come some more 'select' and calculations and
                       then and 'update' */
                     select sum(psum.quantity) as purquantitysum from purchase
                         join cellarbook cb on psum.fk_cellarbook_id = cb.id
                         join bottle bot on cb.fk_bottle_id = bot.id
                         where bot.id = bottleid and psum.purchased <=  pur.purchased and psum.id <> pur.id
into :purquantitysum
                end

我认为它是 'for in loop',但我对 'select into' 的等价物很困惑。

您需要为此使用记录变量:

declare
  r record;
begin
   for r in  
     select col_1, col_2 from some_table;
   loop
      select sum(x)
      from other_table
      where id = r.col_1;
   end loop;
end;

手册中有更多示例:
http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING


当您在循环内使用 运行 updateselect 语句时,通常会产生代码味道 ("row-by-row processing")。在大多数情况下,在单个语句中对所有内容进行批量处理效率更高。

我会使用光标。同一主题有多种变体。我通常使用这个:

declare mycursor cursor for select a, b from c;
declare d, e bigint;
begin
  loop
    fetch from mycursor into d, e
    exit when not found;
    -- do your thing here
  end loop;
  close mycursor;
  -- maybe do some other stuff
end;