在plpgsql中包含IF .. ELSE条件并生成动态查询

时间:2018-12-04 14:11:30

标签: postgresql plpgsql postgresql-11

大家好,我有2个表dcrhd(用于保存当前数据)和dcrhd_arc(用于保存历史数据), 我创建了一个从这些表中获取一些数据的函数。但是此函数仅满足我的一半要求(仅从dcrhd表中检查数据),我将在这里共享我的函数。

CREATE OR REPLACE FUNCTION dcr_report( --fin_year_flag,
 finid integer, prdid integer, comp_cd CHARACTER varying, divid integer, fsid integer) RETURNS refcursor LANGUAGE 'plpgsql' AS $BODY$
    DECLARE
    ref refcursor;
    BEGIN
    open ref for SELECT hd.report_no,
       hd.dcr_date,
       coalesce(pr2.para_descr,' ') work_type,
       coalesce(pr1.para_descr,' ') hq_type,
       coalesce(rm.route_name,' ') route_name,
       coalesce(hd.doctor_visits,0) doctor_visits,
       coalesce(hd.stockist_visits,0) stockist_visits,
       coalesce(hd.retailer_visits,0) retailer_visits,
       hd.dcr_id,
       fm.fs_name,
       hd.fstaff_id,
       CASE hd.status
            WHEN 'A'   THEN 'APPROVED'
            WHEN 'D'   THEN 'DISCARDED'
            WHEN 'F'   THEN 'FORWARDED'
            WHEN 'E'   THEN 'DRAFT'
       END
  status,
       zsm.fs_name report1,
       rsm.fs_name report2,
       fm.geog_lvl1_hq,
       fm.level_code,
       coalesce(pm.para_descr,'SELF') joint_work,
       fm.fs_code,
       fm.emp_code,
       coalesce(hd.doc_other,0) doc_other
FROM dcrhd hd
LEFT OUTER JOIN parameters pm ON hd.jfw = pm.para_code AND pm.para_type = 'JFW'
LEFT OUTER JOIN route_master rm ON rm.fstaff_id = hd.fstaff_id AND rm.route_id = hd.route_id AND rm.company_cd
= comp_cd
LEFT OUTER JOIN parameters pr1 ON pr1.para_code = hd.hq_exhq AND pr1.para_type = 'HQ_',
     parameters pr2,
     field_master fm,
     field_master zsm,
     field_master rsm
WHERE hd.period_id = prdid AND hd.fin_year_id = finid AND hd.fstaff_id = fm.fs_id AND fm.mgr_level4 =
zsm.fs_id AND fm.mgr_level3 = rsm.fs_id AND fm.fs_id =
     CASE
          WHEN fsid = 0  THEN fm.fs_id
          ELSE fsid
     END
      AND fm.div_id =
     CASE
          WHEN divid = 0 THEN fm.div_id
          ELSE divid
     END
      AND fm.fs_id = hd.fstaff_id AND fm.level_code = '005' AND pr2.para_code = hd.work_type AND pr2.
      para_type = 'WTP' AND hd.company = comp_cd AND fm.company_cd = comp_cd
ORDER BY fm.fs_name,
         dcr_date;
    RETURN REF;
    END;
    $BODY$;

我的要求是我只想添加一个名为'fin_year_flag'的新参数 然后相应地选择主表(例如if fin_year_flag='current' 然后转到dcrhd,否则转到dcrhd_arc我可以实现这一目标吗??? 你们可以分享您的想法吗???并有其他方法可以满足我的要求吗?我是PostgreSQL的新用户,在互联网上搜索了很多次,但是找不到任何有用的信息。

1 个答案:

答案 0 :(得分:0)

您发布的代码非常多,所以让我向您演示如何动态使用表名并通过简化表名来返回CURSOR

我用示例行创建了两个表。

create table dcrhd     as select 'CURRENT' ::TEXT  as col;
create table dcrhd_arc as select 'ARCHIVED'::TEXT  as col;

此函数在动态生成的查询上使用OPEN <refcursor> FOR EXECUTE。您需要使用另一个引号或使用dollar quoting对主SQL中的单引号进行转义。

使用fin_year_flag表达式中的CASE设置表名。

CREATE OR REPLACE FUNCTION dcr_report( fin_year_flag TEXT) 
RETURNS refcursor LANGUAGE plpgsql 
AS $BODY$
    DECLARE
    ref refcursor;
    v_table_name TEXT := CASE fin_year_flag
                                   WHEN 'current' THEN 'dcrhd'
                                 ELSE 'dcrhd_arc' END;

    v_sql  text := format('select col from %s',v_table_name );
    BEGIN
    open ref for EXECUTE v_sql ;
    RETURN REF;
    END;
$BODY$;