【问题标题】:How to retrieve multiple result rows in a Postgres functions?如何在 Postgres 函数中检索多个结果行?
【发布时间】:2019-10-31 04:59:30
【问题描述】:

其实我只想使用下面的代码返回多个结果行。但是在调用函数时我只收到了一行结果集(日期不是问题,因为我在正常的选择查询中检查了相同的日期——它检索了多个结果行)。

如何使用以下函数检索多个结果行?

CREATE OR REPLACE FUNCTION fun_audit_trail(in as_on_date date, out mail_id varchar, out user_id varchar, out user_name varchar, 
        out last_login_time timestamp, out last_logout_time timestamp, out logout_flag varchar, out user_available_flag varchar)
 AS 
$BODY$

BEGIN

    select am.am_usrmailid,am.am_usr_loginid,am.am_usr_name,am.am_last_login_time,am.am_last_logout_time,am.am_logout_flag,am.am_usr_available_flag 
    into mail_id,user_id,user_name,last_login_time,last_logout_time,logout_flag,user_available_flag
    from auth_monitor am where am_last_login_time <= as_on_date;

END $BODY$
  LANGUAGE 'plpgsql' COST 100.0 SECURITY INVOKER;

【问题讨论】:

  • 我认为您需要返回一个表而不是将变量放入out vars。见stackoverflow.com/questions/32824785/…
  • 使用游标,一个游标可以容纳多条记录
  • 谢谢,带输出参数的返回表工作正常。

标签: postgresql stored-functions table-functions


【解决方案1】:

你所要做的就是添加

RETURNS SETOF record

在参数列表之后。

【讨论】:

  • 谢谢,我也试过了(setof),但它显示了一些错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 2018-02-17
  • 2018-03-13
  • 1970-01-01
相关资源
最近更新 更多