【问题标题】:Return multiple values from declared variables in postrgres function从 postgres 函数中声明的变量返回多个值
【发布时间】:2021-08-16 20:00:17
【问题描述】:

我需要返回存储在声明变量中的多个值,如何使用 postgres 执行此操作?也许我不需要使用变量,但我需要从 3 个表中返回值。示例中的查询和表已简化

我的例子:

CREATE OR REPLACE FUNCTION public.select_multiple_values(id_ bigint)
 RETURNS setof record
 LANGUAGE plpgsql
AS $function$
declare
id_child bigint;
id_parent bigint;
name_ varchar;
begin
    select id from public.req where id = id_ into id_child;
    select reg_id from public.req where id = id_ into id_parent;
    select "name" from public.reg where id = id_parent into name_;

    return query select id_child, id_parent, name_ ;
END;
$function$
;

【问题讨论】:

标签: postgresql


【解决方案1】:

试试这个 - 你可以使用 RETURN TABLE CONCEPT

CREATE OR REPLACE FUNCTION public.select_multiple_values(id_ bigint)
RETURNS TABLE(id_child bigint, id_parent bigint, name_ character varying ) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000
    
AS $BODY$
declare p_id_child bigint;
declare p_id_parent bigint;
declare p_name_ character varying;
begin

    select id  into p_id_child from public.req where id = id_ ;
    select reg_id   into p_id_parent from public.req where id = id_;
    select "name"  into p_name_ from public.reg where id = id_parent ;

    
    return query select p_id_child as id_child, p_id_parent as id_parent, p_name_ as name_ ;
    
END
$BODY$;

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 2011-07-24
    • 2023-02-01
    • 2012-04-30
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多