【问题标题】:Postgres Query has no desination for result data despite RETURN statement in function definition尽管函数定义中有 RETURN 语句,但 Postgres 查询没有结果数据的目的地
【发布时间】:2021-12-25 04:19:25
【问题描述】:

我创建了一个生成 HH:MM 时间戳并将其保存为函数的代码。但是当我尝试用SELECT random_timestamp(); 调用它时,会返回一个错误-> 查询没有结果数据的目的地

CREATE OR REPLACE FUNCTION random_timestamp() 
RETURNS text AS 
'
BEGIN
SELECT
         CASE
                WHEN Length(s1.hours) = 1
                AND    Length(s1.minutes) = 1 THEN Concat(''0'',s1.hours, '':'',''0'', s1.minutes)
                WHEN Length(s1.hours) = 1
                AND    Length(s1.minutes) = 2 THEN Concat(''0'',s1.hours, '':'', s1.minutes)
                WHEN Length(s1.hours) = 2
                AND    Length(s1.minutes) = 1 THEN Concat(s1.hours, '':'', ''0'', s1.minutes)
                ELSE Concat(s1.hours, '':'', s1.minutes)
         END
  FROM   (SELECT floor(Random() * (23-0-1) + 0)::text AS hours ,
                 floor(random() * (59-0-1) + 0)::text AS minutes) AS s1;
RETURN(SELECT random_timestamp());
END;
' 
language 'plpgsql';

【问题讨论】:

  • 1) 我建议在这里进行美元报价plpgsql structure 2) 你实际上不是RETURNing 3) 看这里Returning 43.6.1.2。返回下一个并返回查询 4) 从这里开始plpgsql 并至少通读所有部分一次。它将回答您未来的许多问题。

标签: sql postgresql function


【解决方案1】:

您的代码不正确。

更正:

CREATE OR REPLACE FUNCTION random_timestamp() 
RETURNS text
LANGUAGE plpgsql
AS $function$
declare 
    v_return text; 
BEGIN
        SELECT
        CASE
                WHEN Length(s1.hours) = 1
                AND    Length(s1.minutes) = 1 THEN Concat('0',s1.hours, ':','0', s1.minutes)
                WHEN Length(s1.hours) = 1
                AND    Length(s1.minutes) = 2 THEN Concat('0',s1.hours, ':', s1.minutes)
                WHEN Length(s1.hours) = 2
                AND    Length(s1.minutes) = 1 THEN Concat(s1.hours, ':', '0', s1.minutes)
                ELSE Concat(s1.hours, ':', s1.minutes)
        end into v_return
        FROM   (SELECT floor(Random() * (23-0-1) + 0)::text AS hours ,
                 floor(random() * (59-0-1) + 0)::text AS minutes) AS s1;
        
        return v_return;
 
END;
$function$
;

【讨论】:

    【解决方案2】:

    您实际上并不需要 PL/pgSQL。一个简单的 SQL 函数就可以了:

    CREATE OR REPLACE FUNCTION random_timestamp() 
    RETURNS text
    LANGUAGE sql
    AS 
    $function$
      SELECT
      CASE
          WHEN Length(s1.hours) = 1
          AND    Length(s1.minutes) = 1 THEN Concat('0',s1.hours, ':','0', s1.minutes)
          WHEN Length(s1.hours) = 1
          AND    Length(s1.minutes) = 2 THEN Concat('0',s1.hours, ':', s1.minutes)
          WHEN Length(s1.hours) = 2
          AND    Length(s1.minutes) = 1 THEN Concat(s1.hours, ':', '0', s1.minutes)
          ELSE Concat(s1.hours, ':', s1.minutes)
      end
      FROM   (SELECT floor(Random() * (23-0-1) + 0)::text AS hours ,
           floor(random() * (59-0-1) + 0)::text AS minutes) AS s1;
    $function$
    ;
    

    但是你把事情复杂化了。如果要生成随机时间值,可以使用:

    select time '00:00' + make_interval(mins => (random() * 1440)::int)
    

    如果需要,您可以将其放入函数中:

    create or replace function random_time()
       returns time
    as
    $$
      select time '00:00' + make_interval(mins => (random() * 1440)::int);
    $$
    language sql;
    

    如果您确实需要 text 值,可以使用 to_char() 对其进行格式化

    select to_char(random_time(), 'hh24:mi')
    

    你的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多