plpgsql函数的动态部分

dimebucker91

我有一个遍历所有点并将其与其他点进行比较的函数(是的,我知道这不需要在plpgsql中完成-这是一个玩具MWE)。该函数返回具有最大x坐标的点:

create type point as (x integer, y integer);
create or replace function test() returns set of Point as
$$
declare
    p1 point;
    p2 point;
    bool integer;
begin
for p1 in select * from table loop
    bool := 0;
    for p2 in select * from table loop
        if p2.x > p1.x then bool :=1;
        exit;
        end if;
    end loop;
    if bool = 0 then return next p1;
    end if;
end loop;
end;
$$ language 'plpgsql';

哪个有效。我想做的是能够将表名作为函数的参数,对于将execute语句放置在何处我感到困惑

沃尊

https://www.postgresql.org/docs/current/static/plpgsql-control-structures.html

FOR-IN-EXECUTE语句是另一种遍历行的方法:

t=# do
$$
declare
  _t text := 'pg_tables';
  _r record;
begin
  for _r in execute format('select * from %I limit 4',_t) loop
    raise info '%',_r.tablename;
  end loop;
end;
$$
;
INFO:  s141
INFO:  events
INFO:  tg_rep_que
INFO:  t4
DO

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章