ITPub博客

首页 > 数据库 > > greenplum 递归函数

greenplum 递归函数

作者:TTT1234 时间:2023-03-17 15:59:54 0 删除 编辑

在低版本的greenplum中不支持递归.所以可以写一个函数来支持

CREATE OR REPLACE FUNCTION with_recursive(
id bigint[],
table_name character varying,
id_name character varying,
pid_name character varying)
RETURNS bigint[] AS
$BODY$
declare
vsql text;
ids int8[];
begin
if(id is null) then return null; end if;

vsql ='select array_agg('|| id_name ||') from ' || table_name ||
' where 1=1 and ' || pid_name || '=any(array['|| array_to_string(id,',') || '])';
--raise notice 'vsql:%',vsql;

if(vsql is not null) then execute vsql into ids;end if;

vsql ='select subids from (select array_cat(array['|| array_to_string(id,',') ||
']::int8[],with_recursive(array['|| array_to_string(ids,',') ||
'],'''|| table_name ||''','''||id_name||''','''||pid_name||''')) subids) t';
--raise notice 'vsql:%',vsql;

if(vsql is not null) then execute vsql into ids;end if;

return coalesce(ids,id);

end $BODY$
LANGUAGE plpgsql VOLATILE;
ALTER FUNCTION with_recursive(bigint[], character varying, character varying, character varying)
OWNER TO gpadmin;
COMMENT ON FUNCTION with_recursive(bigint[], character varying, character varying, character varying) IS '递归函数';

使用

select unnest(with_recursive(array[4],'chains','cid','pid')) subchains;




本文来源: http://udn.yyuap.com/thread-114853-1-1.html


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69953058/viewspace-3155641/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-11-13

  • 博文量
    566
  • 访问量
    5199