如何从PostgreSQL返回多个引用游标?

时间:2018-09-28 13:29:43

标签: postgresql

我已经成功创建了一个函数。我想返回多个ref游标,如下所示。当我执行该功能时,会收到如下图所示的响应。我该如何解决这个问题?

CREATE OR REPLACE FUNCTION public.get_dashboard_graph(
    p_fromdate character varying,
    p_todate character varying)
    RETURNS SETOF refcursor 
    LANGUAGE 'plpgsql'
    COST 100.0
    VOLATILE 
    ROWS 1000.0
AS $function$

DECLARE
      process_wise_positrol refcursor;           
      process_wise_micro_audit refcursor;
      process_wise_positrol_line_stop refcursor;          
      process_wise_micro_audit_line_stop refcursor;
BEGIN

-- process wise positrol completed
OPEN process_wise_positrol FOR
    select count(*), d.dd_value from audit_transaction t, audit_master m, dd_type_details d 
    where t.audit_id = m.audit_id and m.activity_id = 9 and t.iscompleted = 'completed' and d.dd_id = m.process 
    and audit_start_time BETWEEN p_fromdate::timestamp AND p_todate::timestamp group by d.dd_value;
RETURN NEXT process_wise_positrol;

-- process wise Micro audit completed
OPEN process_wise_micro_audit FOR
    select count(*), d.dd_value from audit_transaction t, audit_master m, dd_type_details d 
    where t.audit_id = m.audit_id and m.activity_id = 8 and t.iscompleted = 'completed' 
    and d.dd_id = m.process 
    and audit_start_time BETWEEN p_fromdate::timestamp AND p_todate::timestamp group by d.dd_value;
RETURN NEXT process_wise_micro_audit;

-- process wise positrol line stop
OPEN process_wise_positrol_line_stop FOR
    select count(*), d.dd_value from audit_transaction t
    left join audit_master m on m.audit_id= t.audit_id
    left join dd_type_details d on d.dd_id= m.process
    left join audit_ques_link al on al.audit_trans_id= t.audit_trans_id
    where t.audit_id = m.audit_id and m.activity_id = 9 and al.line_stop = 0
    and t.iscompleted = 'completed' and d.dd_id = m.process 
    and audit_start_time BETWEEN p_fromdate::timestamp AND p_todate::timestamp 
    group by d.dd_value;
RETURN NEXT process_wise_positrol_line_stop;

-- process wise Micro audit line  stop
OPEN process_wise_micro_audit_line_stop FOR
    select count(*), d.dd_value from audit_transaction t
    left join audit_master m on m.audit_id= t.audit_id
    left join dd_type_details d on d.dd_id= m.process
    left join audit_ques_link al on al.audit_trans_id= t.audit_trans_id
    where t.audit_id = m.audit_id and m.activity_id = 8 and al.line_stop = 0
    and t.iscompleted = 'completed' and d.dd_id = m.process 
    and audit_start_time BETWEEN p_fromdate::timestamp AND p_todate::timestamp 
    group by d.dd_value;
RETURN NEXT process_wise_micro_audit_line_stop;

END;

$function$;

ALTER FUNCTION public.get_dashboard_graph(character varying, character varying)
    OWNER TO postgres;

当我执行上面的函数时,它返回如下输出。

select * from get_Dashboard_Graph('09/01/2018','09/28/2018');

enter image description here

1 个答案:

答案 0 :(得分:1)

您完全可以得到所需的内容-四个光标。

cou调用函数后,您可以运行SQL语句

FETCH NEXT FROM "<unnamed portal 26>";

如果您不喜欢游标的名称,请在函数体内分配一个不同的游标:

process_wise_micro_audit := 'auditcursor';

然后您可以在函数完成后像这样调用FETCH

FETCH NEXT FROM auditcursor;