首页 > 解决方案 > 如何从 PL/pgSQL 函数中的 select 中获取请求的列?

问题描述

有什么方法可以获取 Postgres 函数中请求的列列表?举例说明:

create type a_type as (id int, val text, expensive int[]);

create function find_by_id(rid int) returns setof a_type as $$
  declare
    atyp a_type%rowtype;
  begin
    select id,val from a_table where id=rid into atyp.id,atyp.val;
    select array(select int_column from another_table where foreign_id=rid) into atyp.expensive;
    return next atyp;
  end;
$$ language plpgsql;

int[]这是用我们主表上的外键查询另一个表的查询填充返回类型的字段,当然用select [COLUMN LIST] from find_by_id(ID). 有什么方法可以[COLUMN LIST]从函数内部访问吗?如果expensive未在列列表中指定,那么我不需要进行第二次选择并填充一个只会在返回结果之前被 Postgres 丢弃的字段。

标签: postgresqlplpgsql

解决方案


推荐阅读