Динамический запрос от оракула к postgreSQL
create or replace PROCEDURE EXPENSE_ANALYSIS(DATASET OUT SYS_REFCURSOR,
FIN_YEAR_FLAG VARCHAR2 , FINID NUMBER , COMP_CD VARCHAR2 ,
DIVID VARCHAR2 , FSID VARCHAR2 ,LOG_FSID VARCHAR2 , LOG_FSLVL VARCHAR2 )
AS
STR VARCHAR2(32000);
BEGIN
STR := ' SELECT exp.fs_name, exp.level_code, exp.basic, exp.staff_id,
gm.loc_name, fm1.fs_name report1,
fm2.fs_name report2, fm.emp_code, exp.sr_no, jan, feb, mar, apr, may,
jun, jul, aug
FROM field_master fm, geographical_master gm, field_master fm1,
field_master fm2, ';
IF FIN_YEAR_FLAG = 'CURRENT' THEN
STR := STR || ' exp_summary exp ' ;
ELSE
STR := STR || ' exp_summary_arc exp ' ;
END IF ;
STR := STR || ' WHERE fm.fs_id = exp.staff_id AND exp.level_code =
''005''
AND fm.fs_id = CASE WHEN '||FSID||'=0 THEN FM.FS_ID ELSE '||FSID||' END
and exp.div_id = CASE WHEN '||DIVID||'= 0 THEN exp.DIV_ID ELSE
'||DIVID||' END ' ;
IF LOG_FSLVL = '001' THEN
STR := STR || ' AND FM.MGR_LEVEL1 = '||LOG_FSID||' ';
ELSIF LOG_FSLVL = '002' THEN
STR := STR || ' AND FM.MGR_LEVEL2 = '||LOG_FSID||' ';
ELSIF LOG_FSLVL = '003' THEN
STR := STR || ' AND FM.MGR_LEVEL3 = '||LOG_FSID||' ';
ELSIF LOG_FSLVL = '004' THEN
STR := STR || ' AND FM.MGR_LEVEL4 = '||LOG_FSID||' ';
ELSIF LOG_FSLVL = '005' THEN
STR := STR || ' AND FM.FS_ID = '||LOG_FSID||' ';
END IF;
STR := STR || ' AND fm1.fs_id = fm.mgr_level4 AND fm2.fs_id =
fm.mgr_level3
AND fm.geog_lvl1_hq = gm.loc_id AND exp.fin_year_id = '||FINID||'
AND exp.company_cd = '''||COMP_CD||'''
ORDER BY exp.fs_name, exp.sr_no ';
DBMS_OUTPUT.ENABLE(1000000) ;
DBMS_OUTPUT.PUT_LINE ( STR ) ;
OPEN DATASET FOR STR ;
END EXPENSE_ANALYSIS ;
Когда я выполняю эту процедуру, она генерирует требуемый запрос и сохраняет вывод в переменной STR (A SYS_REFCURSOR
используется для переменной для извлечения данных из str)
Ниже приведен запрос, сгенерированный из процедуры.
SELECT exp.fs_name, exp.level_code, exp.basic, exp.staff_id,
gm.loc_name, fm1.fs_name report1,
fm2.fs_name report2, fm.emp_code, exp.sr_no, jan, feb, mar, apr, may,
jun, jul, aug
FROM field_master fm, geographical_master gm, field_master fm1,
field_master fm2,exp_summary exp
WHERE fm.fs_id = exp.staff_id AND exp.level_code = '005'
AND fm.fs_id = CASE WHEN 0=0 THEN FM.FS_ID ELSE 0 END
and exp.div_id = CASE WHEN 327= 0 THEN exp.DIV_ID ELSE 327 END
AND FM.MGR_LEVEL4 = 40 AND fm1.fs_id = fm.mgr_level4 AND
fm2.fs_id = fm.mgr_level3
AND fm.geog_lvl1_hq = gm.loc_id AND exp.fin_year_id = 12
AND exp.company_cd = 'VET'
ORDER BY exp.fs_name, exp.sr_no
Мое требование: как можно написать ту же процедуру в PostgresQL?