一個Oracle動態SQL拼接
create or replace function getpjxfjd1(columnxh in varchar2,columnxnxq in varchar2,columntab in varchar2) return number is
existsCol number(6,2);
v_sql varchar2(30000) :='';
begin
v_sql:='select nvl(case' || chr(10) ||
' when sum(injdxf) <= 0 then' || chr(10) ||
' 0' || chr(10) ||
' else' || chr(10) ||
' round(sum(injdxf * (case' || chr(10) ||
' when jd < 0 then' || chr(10) ||
' 0' || chr(10) ||
' else' || chr(10) ||
' jd' || chr(10) ||
' end) * zl) / SUM(injdxf),' || chr(10) ||
' 2)' || chr(10) ||
' end,' || chr(10) ||
' 0) as pjxfjd from (' || chr(10) ||
'SELECT' || chr(10) ||
'decode((SELECT 1' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' WHERE nvl(Instr(j1.bjskcxz, v.KCXZ), 0) > 0),' || chr(10) ||
' null,' || chr(10) ||
' v.xf,' || chr(10) ||
' 0) as injdxf,' || chr(10) ||
' Decode((SELECT 1' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND Instr(j1.xsfs, ''tsfs'') > 0),' || chr(10) ||
' NULL,' || chr(10) ||
' (CASE' || chr(10) ||
' WHEN v.ksxzid != ''0'' then' || chr(10) ||
' (case' || chr(10) ||
' when (SELECT 1' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND Instr(j1.xsfs, ''ksxz'') > 0) > 0 then' || chr(10) ||
' (SELECT j2.jd' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1.cjjd01id = j2.cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.ksxzjdkckz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.ksxzdjfsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.ksxzjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.ksxzdjjdkcxzkz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.ksxzdjjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.ksxzdjjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.ksxzdjjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.ksxzdjjdkccj OR' || chr(10) ||
' Nvl(Instr(j1.ksxzdjjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.ksxzdjjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''ksxz''' || chr(10) ||
' AND j2.qsfs <= v.ZCJ' || chr(10) ||
' AND j2.jsfs >= v.ZCJ)' || chr(10) ||
' when v.cjfs is not null and v.cjfs != ''D'' and' || chr(10) ||
' (SELECT 1' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND Instr(j1.xsfs, ''djfs'') > 0) > 0 then' || chr(10) ||
' (SELECT j2.jd' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1.cjjd01id = j2.cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.djjdkckz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.djfsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.djjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.djjdkcxzkz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.djjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.djjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.djjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.djjdkccj OR' || chr(10) ||
' Nvl(Instr(j1.djjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.djjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''djfs''' || chr(10) ||
' AND j2.fsdj = t5.djfsmc)' || chr(10) ||
' else' || chr(10) ||
' (SELECT j2.jd' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1. cjjd01id = j2. cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.fsjdkckz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.fsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.fsjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkcxzkz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.fsjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.fsjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.fsjdkccj OR' || chr(10) ||
' Nvl(Instr(j1.fsjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.fsjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''fsfs''' || chr(10) ||
' AND j2.qsfs <= v.ZCJ' || chr(10) ||
' AND j2.jsfs >= v.ZCJ)' || chr(10) ||
' end)' || chr(10) ||
' else' || chr(10) ||
' (case' || chr(10) ||
' when v.cjfs is not null and v.cjfs != ''D'' and' || chr(10) ||
' (SELECT 1' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND Instr(j1.xsfs, ''djfs'') > 0) > 0 then' || chr(10) ||
' (SELECT j2.jd' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1.cjjd01id = j2.cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.djjdkckz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.djfsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.djjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.djjdkcxzkz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.djjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.djjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.djjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.djjdkccj OR' || chr(10) ||
' Nvl(Instr(j1.djjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.djjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''djfs''' || chr(10) ||
' AND j2.fsdj = t5.djfsmc)' || chr(10) ||
' else' || chr(10) ||
' (SELECT j2.jd' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1. cjjd01id = j2. cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.fsjdkckz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.fsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.fsjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkcxzkz = ''1'' AND' || chr(10) ||
' Nvl(Instr(j1.fsjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.fsjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.fsjdkccj OR' || chr(10) ||
' Nvl(Instr(j1.fsjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.fsjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''fsfs''' || chr(10) ||
' AND j2.qsfs <= v.ZCJ' || chr(10) ||
' AND j2.jsfs >= v.ZCJ)' || chr(10) ||
' end)' || chr(10) ||
' end),' || chr(10) ||
' (SELECT (CASE' || chr(10) ||
' WHEN ((v.zcj / j2.tsfsvalue) - j2.tsfsvalue1) < 1 THEN' || chr(10) ||
' 0' || chr(10) ||
' ELSE' || chr(10) ||
' (v.zcj / j2.tsfsvalue) - j2.tsfsvalue1' || chr(10) ||
' END)' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1. cjjd01id = j2. cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND j2.fslx = ''tsfs'')) jd,--------------------' || chr(10) ||
' decode((select 1' || chr(10) ||
' from cjjd01 j1' || chr(10) ||
' where j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' and instr(j1.xsfs, ''tsfs'') > 0),' || chr(10) ||
' null,' || chr(10) ||
' (CASE' || chr(10) ||
' WHEN v.ksxzid != ''0'' then' || chr(10) ||
' (case' || chr(10) ||
' when (SELECT 1' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND Instr(j1.xsfs, ''ksxz'') > 0) > 0 then' || chr(10) ||
' (SELECT j2.zl' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1.cjjd01id = j2.cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.ksxzjdkckz = ''1'' AND' || chr(10) ||
' nvl(Instr(j1.ksxzdjfsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.ksxzjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.ksxzdjjdkcxzkz = ''1'' AND' || chr(10) ||
' nvl(Instr(j1.ksxzdjjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.ksxzdjjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.ksxzdjjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.ksxzdjjdkccj OR' || chr(10) ||
' nvl(Instr(j1.ksxzdjjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.ksxzdjjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''ksxz''' || chr(10) ||
' AND j2.qsfs <= v.ZCJ' || chr(10) ||
' AND j2.jsfs >= v.ZCJ)' || chr(10) ||
' else' || chr(10) ||
' (SELECT j2.zl' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1. cjjd01id = j2.cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.fsjdkckz = ''1'' AND' || chr(10) ||
' nvl(Instr(j1.fsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.fsjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkcxzkz = ''1'' AND' || chr(10) ||
' nvl(Instr(j1.fsjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.fsjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.fsjdkccj OR' || chr(10) ||
' nvl(Instr(j1.fsjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.fsjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''fsfs''' || chr(10) ||
' AND j2.qsfs <= v.ZCJ' || chr(10) ||
' AND j2.jsfs >= v.ZCJ)' || chr(10) ||
' end)' || chr(10) ||
' else' || chr(10) ||
' (SELECT j2.zl' || chr(10) ||
' FROM cjjd01 j1' || chr(10) ||
' INNER JOIN cjjd02 j2' || chr(10) ||
' ON j1. cjjd01id = j2. cjjd01id' || chr(10) ||
' WHERE j1.synd LIKE ''%'' || x2.rxnf || ''%''' || chr(10) ||
' AND (j1.syzy LIKE ''%'' || j.jx01ndid || ''%'' or' || chr(10) ||
' j1.iszykz = ''1'')' || chr(10) ||
' and nvl(Instr(j1.bjskcxz, v.KCXZ), 0) = 0' || chr(10) ||
' AND ((j1.fsjdkckz = ''1'' AND' || chr(10) ||
' nvl(Instr(j1.fsjdkc, v.kcid), 0) = 0) OR' || chr(10) ||
' j1.fsjdkckz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkcxzkz = ''1'' AND' || chr(10) ||
' nvl(Instr(j1.fsjdkcxz, v.KCXZ), 0) = 0) OR' || chr(10) ||
' j1.fsjdkcxzkz <> ''1'')' || chr(10) ||
' AND ((j1.fsjdkccjkz = ''1'' AND' || chr(10) ||
' (v.ZCJ >= j1.fsjdkccj OR' || chr(10) ||
' nvl(Instr(j1.fsjdkccjxz, v.KCXZ), 0) = 0)) OR' || chr(10) ||
' j1.fsjdkccjkz <> ''1'')' || chr(10) ||
' AND j2.fslx = ''fsfs''' || chr(10) ||
' AND j2.qsfs <= v.ZCJ' || chr(10) ||
' AND j2.jsfs >= v.ZCJ)' || chr(10) ||
' end),' || chr(10) ||
' 1) zl' || chr(10) ||
' FROM (select CJ0708_max.*,' || chr(10) ||
' (case' || chr(10) ||
' when CJ0708_max.zcj >= 60 then' || chr(10) ||
' CJ0708_max.xf' || chr(10) ||
' else' || chr(10) ||
' 0' || chr(10) ||
' end) as cjxf,' || chr(10) ||
' CJ0708_max.kclb as cjkclb' || chr(10) ||
' from ';
if columntab = 'V_CJ0708_BJMAX' then
v_sql := v_sql||' V_CJ0708_BJMAX CJ0708_max ';
elsif columntab ='v_cj0708_normal' then
v_sql := v_sql||' v_cj0708_normal CJ0708_max ';
elsif columntab ='v_cj0708_pyfa' then
v_sql := v_sql||' v_cj0708_pyfa CJ0708_max ';
elsif columntab ='v_cj0708_pyfanormal' then
v_sql := v_sql||' v_cj0708_pyfanormal CJ0708_max ';
else v_sql := v_sql||' V_CJ0708_BJMAX CJ0708_max ';
end if;
v_sql := v_sql||'where CJ0708_max.Xnxqid in('||columnxnxq||')' || chr(10) ||
' ) v' || chr(10) ||
' INNER JOIN Xs0101 x0' || chr(10) ||
' ON v.xs0101id = x0.xs0101id' || chr(10) ||
' INNER JOIN Xs0701 x1' || chr(10) ||
' ON x0.xs0101id = x1.xs0101id' || chr(10) ||
' INNER JOIN xx04 x2' || chr(10) ||
' ON x2.xx04id = x1.bjbh' || chr(10) ||
' INNER JOIN jx01nd j' || chr(10) ||
' ON j.jx01ndid = x2.jx01ndid' || chr(10) ||
' LEFT JOIN zzddjmc t5' || chr(10) ||
' ON v.CJFS = t5.cjfslx' || chr(10) ||
' AND (t5.dyfs = v.zcj OR t5.cjfslx = ''D'')' || chr(10) ||
' left join zzdpyccb pycc' || chr(10) ||
' on x1.pyccm = pycc.dm' || chr(10) ||
' WHERE 1 = 1' || chr(10) ||
' and nvl(v.fxkc, ''data'') <> ''1''' || chr(10) ||
' and x0.xh =:1'|| chr(10) ||
' and exists' || chr(10) ||
' (select ''x''' || chr(10) ||
' from cjjd01 j1' || chr(10) ||
' where j1.synd LIKE ''%'' || x2.rxnf || ''%''))';
execute immediate v_sql into existsCol using columnxh;
return(existsCol);
end getpjxfjd1;