gpt4 book ai didi

postgresql - 当返回类型为记录类型 postgres 时,函数只返回一列

转载 作者:行者123 更新时间:2023-11-29 11:47:48 27 4
gpt4 key购买 nike

我有一个返回记录类型的函数。

下面是我正在使用的函数,基本上函数所做的是,它接受输入参数并查询数据库以返回列:

drop function if exists test_proc(sr_number1 bigint);
create or replace function test_proc(sr_number1 bigint) RETURNS record /*SETOF tbl*/ AS $$

declare
i integer default 0;
record_type record;
begin
select sr_num,product_number,phone,addr into record_type from my_table where sr_num=sr_number1;
return record_type;
end
$$ LANGUAGE plpgsql;

不幸的是,当我执行函数时

选择测试过程(12345);我得到的结果是一个逗号分隔的列表,仅在一列中,例如 (sr_num,product_number,phone,addr)。但我希望它返回的是一个表行,其中包含列值及其各自的列名。

我也试过执行这个函数

select * from test_proc(12345); but get the following error

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

最佳答案

当查询返回记录的函数时,您必须指定要在结果中获得的记录类型

 select * from test_proc(12345) f(b bigint, t text);

这应该有效。

更好的解决方案是在函数中声明记录的类型

 CREATE OR REPLACE FUNCTION test_proc(sr_number1 bigint) 
RETURNS TABLE(b bigint, t text) AS $$ $$

关于postgresql - 当返回类型为记录类型 postgres 时,函数只返回一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27967135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com