postgresql – postgres中的递归函数

postgresql – postgres中的递归函数,第1张

概述如何将下面的查询映射到postgres函数. WITH RECURSIVE source (counter, product) AS (SELECT1, 1UNION ALLSELECTcounter + 1, product * (counter + 1)FROM sourceWHEREcounter < 10)SELECT counter, product FROM sou 如何将下面的查询映射到postgres函数.

WITH RECURSIVE source (counter,product) AS (SELECT1,1UNION ALLSELECTcounter + 1,product * (counter + 1)FROM sourceWHEREcounter < 10)SELECT counter,product FROM source;

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

解决方法 递归函数

create or replace function recursive_function (ct int,pr int)returns table (counter int,product int)language plpgsqlas $$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 plpgsqlas $$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 ();
总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1160127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存