我遇到了必须用来返回多行的数据类型和方法的问题。我在数据库(存储过程)中有一个函数,它执行以下操作:
SELECT *
FROM table
INNER JOIN some_table ON value = value
INNER JOIN some_tabl a ON value = value
INNER JOIN some_tabl ON value = value
WHERE table.id= 20
此函数的输出给出1:n行。如何返回多行?如果调用这样的函数:
SELECT * FROM functioname()
它不起作用,因为我必须给列名。当我尝试在postgres中使函数输出参数正确时,它一直给我错误。有人可以帮我弄这个吗?非常感谢
答案 0 :(得分:1)
function vs stored procedure in postgresql
create table test(a int, b varchar(50));
create table test1(a int, b varchar(50));
DROP FUNCTION some_function(in int);
CREATE OR REPLACE FUNCTION some_function(in int)
RETURNS TABLE(a int, b character varying, c int, d character varying) AS
$BODY$
DECLARE
some_input ALIAS FOR $1;
BEGIN
RETURN QUERY SELECT * from test a inner join test1 b on a.a = b.a where a.a = some_input;
END$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION some_function(in int)
OWNER TO postgres;
COMMENT ON FUNCTION some_function(in int) IS 'FUNCTION JUST FOR TEST';
SELECT * FROM some_function(1);