【问题标题】:PL/pgSQL Loops IndefinitelyPL/pgSQL 无限循环
【发布时间】:2019-11-25 07:41:20
【问题描述】:

我想在两个日期之间循环,但我的 PL/pgSQL 代码进入了无限循环。我想我在这里遗漏了一些东西。

do $$
    declare
        the_dates date;
    begin
        select gs from generate_series('2019-11-01'::date, '2012-11-30', '1 day') as gs into the_dates;
        loop
            raise notice '%', the_dates;
        end loop;
    end
$$

我应该如何在这两个日期之间循环?

【问题讨论】:

    标签: postgresql stored-procedures plpgsql postgresql-11


    【解决方案1】:

    您似乎对循环的语法感到困惑。

    这里有两个不同的东西:

    1. 将零行(因为您的日期向后)选择到日期变量中的查询。
    2. 一个没有限制的循环将永远引发通知。

    https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING

    【讨论】:

      【解决方案2】:

      您没有循环 SELECT 语句的结果。

      如果你想循环查询的结果你need to usefor record in select ...

      do $$
      declare
        l_date_row record;
      begin
        for l_date_row in select gs 
                          from generate_series('2012-11-30'::date, '2019-11-01'::date, '1 day') as gs
          raise notice '%', l_date_row.gs;
        end loop;
      end
      $$
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 2013-02-22
        • 2023-03-28
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多