【问题标题】:How to return multiple rows from PL/pgSQL function?如何从 PL/pgSQL 函数返回多行?
【发布时间】:2013-06-19 02:48:33
【问题描述】:

我花了很多时间试图解决这个问题,但我无法解决它。所以,我需要你的帮助。

我正在尝试编写一个返回多行的 PL/pgSQL 函数。我写的函数如下图所示。但它不起作用。

CREATE OR REPLACE FUNCTION get_object_fields()
RETURNS SETOF RECORD
AS 
$$
DECLARE result_record keyMetrics;
BEGIN
    return QUERY SELECT department_id into result_record.visits 
    from fact_department_daily 
    where report_date='2013-06-07';
    --return result_record;
END

$$ LANGUAGE plpgsql; 

SELECT * FROM get_object_fields;

返回此错误:

错误:RETURN 在函数返回集中不能有参数;
在“QUERY”处或附近使用 RETURN NEXT

【问题讨论】:

  • 一如既往,您的 Postgres 版本是必不可少的细节。
  • 这个问题在stackexchange得到解答

标签: postgresql plpgsql


【解决方案1】:

修复错误@Pavel pointed out后,还要正确定义返回类型,否则每次调用都必须提供列定义列表。

这个电话:

SELECT * FROM get_object_fields()

... 假设 Postgres 知道如何扩展 *。由于您要返回匿名记录,因此您会遇到异常:

ERROR:  a column definition list is required for functions returning "record"

解决这个问题的一种方法是使用RETURNS TABLE(Postgres 8.4+):

CREATE OR REPLACE FUNCTION get_object_fields()
  RETURNS TABLE (department_id int) AS 
$func$
BEGIN
   RETURN QUERY
   SELECT department_id
   FROM   fact_department_daily 
   WHERE  report_date = '2013-06-07';
END
$func$ LANGUAGE plpgsql;

同样适用于 SQL 函数。

相关:

【讨论】:

    【解决方案2】:

    这是一种方法

    drop function if exists get_test_type();
    
    drop type if exists test_comp;
    drop type if exists test_type;
    drop type if exists test_person;
    
    create type test_type as (
      foo int, 
      bar int
    );
    
    create type test_person as (
      first_name text, 
      last_name text
    );
    
    create type test_comp as 
    (
      prop_a test_type[], 
      prop_b test_person[]
    );
    
    
    create or replace function get_test_type()
    returns test_comp
    as $$
    declare
      a test_type[];
      b test_person[];
      x test_comp;
    begin
    
      a := array(
        select row (m.message_id, m.message_id) 
        from message m
      );
    
      -- alternative 'strongly typed'
      b := array[
        row('Bob', 'Jones')::test_person,
        row('Mike', 'Reid')::test_person
      ]::test_person[];
    
      -- alternative 'loosely typed'
      b := array[
        row('Bob', 'Jones'),
        row('Mike', 'Reid')
      ];
    
      -- using a select
      b := array (
        select row ('Jake', 'Scott')
        union all 
        select row ('Suraksha', 'Setty')
      );  
    
      x := row(a, b);
    
      return x;  
    end;
    $$
    language 'plpgsql' stable;
    
    
    select * from get_test_type();
    

    【讨论】:

      【解决方案3】:

      我看到了更多错误:

      首先,SET RETURNING FUNCTIONS 调用具有以下语法

      选择 * 从 get_object_fields()

      second - RETURN QUERY 将查询结果直接转发到输出。您不能将此结果存储到变量中 - 现在在 PostgreSQL 中是不可能的。

      开始 返回查询选择 ....; -- 结果直接转发输出 返回; -- 不会有下一个结果,结束执行 结尾;

      第三——这些简单的函数最好用 SQL 语言实现

      创建或替换函数 get_object_fields() 返回记录为 $$ 选择部门 ID WHERE ... $$ 语言 sql 稳定;

      【讨论】:

        猜你喜欢
        • 2012-08-05
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-24
        相关资源
        最近更新 更多