Postgresql从Table返回SETOF行

时间:2015-01-15 07:25:10

标签: java database postgresql stored-procedures

我在DBMS(Postgresql)中有这个存储过程

CREATE OR REPLACE FUNCTION getallentree()
  RETURNS SETOF entree AS
$BODY$
begin 
   select * from entree ; 
end;
$BODY$
  LANGUAGE plpgsql 

调用此程序后:

select *  from getAllEntree(); 

我收到此错误:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function getallentree() line 3 at SQL statement

********** Error **********

ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Context: PL/pgSQL function getallentree() line 3 at SQL statement

2 个答案:

答案 0 :(得分:2)

对于这样一个简单的查询,最好使用SQL函数。它们可以通过查询优化器内联并更好地优化,而不是PL / pgSQL函数:

CREATE OR REPLACE FUNCTION getallentree()
  RETURNS SETOF entree AS
$BODY$
   select * from entree ; 
$BODY$
  LANGUAGE sql;

如果您确实需要PL / pgSQL进行其他一些您没有向我们展示的处理,那么Mureinik关于使用return query的答案是可行的。

答案 1 :(得分:1)

您只需要在return query声明中添加select

CREATE OR REPLACE FUNCTION getallentree()
  RETURNS SETOF entree AS
$BODY$
begin 
   return query
   select * from entree ; 
end;
$BODY$
  LANGUAGE plpgsql