Postgre SQL   发布时间:2022-05-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了postgresql – postgres中的递归函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_450_0@ @H_607_2@ @H_450_0@
如何将下面的查询映射到postgres函数.

WITH REcursIVE source (counter,product) AS (
SELECT
1,1
union all
SELECT
counter + 1,product * (counter + 1)
FROM source
WHERE
counter < 10
)
SELECT counter,product FROM source;

我是POSTGRes的新手.我想使用PL / pgsql函数实现相同的功能.

@H_607_2@

解决方法

递归函数:

create or replace function recursive_function (ct int,pr int)
returns table (counter int,product int)
language plpgsql
as $$
begin
    return query SELEct ct,pr;
    if ct < 10 then
        return query SELEct * from recursive_function(ct+ 1,pr * (ct+ 1));
    end if;
end $$;

SELEct * from recursive_function (1,1);

循环功能:

create or replace function loop_function ()
returns table (counter int,product int)
language plpgsql
as $$
declare
    ct int;
    pr int = 1;
begin
    for ct in 1..10 loop
        pr = ct* pr;
        return query SELEct ct,pr;
    end loop;
end $$;

SELEct * from loop_function ();
@H_607_2@ @H_607_2@
@H_607_2@ @H_450_0@ @H_607_2@

大佬总结

以上是大佬教程为你收集整理的postgresql – postgres中的递归函数全部内容,希望文章能够帮你解决postgresql – postgres中的递归函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: