动态表格/动态SQL语句查询【oracle数据库】

代码来源:宜居公租房管理-->统计分析-->出租情况表
存储过程:
create or replace procedure GZF_RentalSituationList
(
    v_xqmc in varchar2,--小区名称
    v_datas in varchar2,--日期字符串
    v_cursor out sys_refcursor
) 
is
v_wherecuase varchar2(6000);--where字符串
dynamicSQL varchar2(6000);--sql字符串
arr YIJU_SRCCIMS.t_Array;--自定义类型
v_sumall varchar2(6000);--之前
v_sum varchar2(6000);
datamonthday varchar(40);
begin
  
v_wherecuase:='from u_agreementdetailrecordinfo a
    left join u_agreementrecordinfo b on a.htbh=b.htbh
    inner join u_tenementsinfo c on a.fwh=c.fwh
    where b.sfsh=''Y'' 
    and (c.xqmc='''||v_xqmc||''' or '''||v_xqmc||''' is null)
    group by c.xqmc
    order by c.xqmc';
 arr:=YIJU_SRCCIMS.f_Split(v_datas,'|');//函数分割字符串
 
if(arr is not null) then
     for i in 1.. arr.count loop
       begin 
         datamonthday:=substr(arr(i),0,7);
         v_sumall:=v_sumall||'sum(case when  (a.qsrq<=to_date('''||arr(i)||''',''yyyy-MM-dd'') and  a.zzrq>=to_date('''||arr(i)||''',''yyyy-MM-dd'') and a.ZT=''Z'') then 1 else 0 end ) sum'||replace(arr(i),'-','')||',';
         if(i!=1) then 
             v_sum:=v_sum||'sum(case when (to_char(a.qsrq,''yyyy-MM'')='''||datamonthday||''' and a.ZT=''Z'') then 1 else 0 end) a'||replace(datamonthday,'-','')||',';
             v_sum:=v_sum||'sum(case when (to_char(a.zzrq,''yyyy-MM'')='''||datamonthday||''' and a.ZT=''T'') then 1 else 0 end) b'||replace(datamonthday,'-','')||',';
         end if;
       end;
       end loop;
end if;
   v_sum:=substr(v_sum,0,length(v_sum)-1);
   
   dynamicSQL:='select c.xqmc,'||v_sumall||v_sum||' '||v_wherecuase;
   DBMS_OUTPUT.put_line(dynamicSQL);
   open v_cursor for dynamicSQL;
  
end GZF_RentalSituationList;
执行存储过程:参数 v_xqmc v_datas:='2017-08-31|2017-09-30|2017-10-31|2017-11-30|2017-12-31';
拼接的SQL语句:
select c.xqmc
,sum(case when  (a.qsrq<=to_date('2017-08-31','yyyy-MM-dd') and  a.zzrq>=to_date('2017-08-31','yyyy-MM-dd') and a.ZT='Z') then 1 else 0 end ) sum20170831
,sum(case when  (a.qsrq<=to_date('2017-09-30','yyyy-MM-dd') and  a.zzrq>=to_date('2017-09-30','yyyy-MM-dd') and a.ZT='Z') then 1 else 0 end ) sum20170930
,sum(case when  (a.qsrq<=to_date('2017-10-31','yyyy-MM-dd') and  a.zzrq>=to_date('2017-10-31','yyyy-MM-dd') and a.ZT='Z') then 1 else 0 end ) sum20171031
,sum(case when  (a.qsrq<=to_date('2017-11-30','yyyy-MM-dd') and  a.zzrq>=to_date('2017-11-30','yyyy-MM-dd') and a.ZT='Z') then 1 else 0 end ) sum20171130
,sum(case when  (a.qsrq<=to_date('2017-12-31','yyyy-MM-dd') and  a.zzrq>=to_date('2017-12-31','yyyy-MM-dd') and a.ZT='Z') then 1 else 0 end ) sum20171231
,sum(case when (to_char(a.qsrq,'yyyy-MM')='2017-09' and a.ZT='Z') then 1 else 0 end) a201709
,sum(case when (to_char(a.zzrq,'yyyy-MM')='2017-09' and a.ZT='T') then 1 else 0 end) b201709
,sum(case when (to_char(a.qsrq,'yyyy-MM')='2017-10' and a.ZT='Z') then 1 else 0 end) a201710
,sum(case when (to_char(a.zzrq,'yyyy-MM')='2017-10' and a.ZT='T') then 1 else 0 end) b201710
,sum(case when (to_char(a.qsrq,'yyyy-MM')='2017-11' and a.ZT='Z') then 1 else 0 end) a201711
,sum(case when (to_char(a.zzrq,'yyyy-MM')='2017-11' and a.ZT='T') then 1 else 0 end) b201711
,sum(case when (to_char(a.qsrq,'yyyy-MM')='2017-12' and a.ZT='Z') then 1 else 0 end) a201712
,sum(case when (to_char(a.zzrq,'yyyy-MM')='2017-12' and a.ZT='T') then 1 else 0 end) b201712
from u_agreementdetailrecordinfo a
    left join u_agreementrecordinfo b on a.htbh=b.htbh
    inner join u_tenementsinfo c on a.fwh=c.fwh
    where b.sfsh='Y' 
    and (c.xqmc='' or '' is null)
    group by c.xqmc
    order by c.xqmc;
执行结果:
ORACLEDB.cs 代码请参考:http://blog.csdn.net/qq_33903684/article/details/78779767http://blog.csdn.net/qq_33903684/article/details/78779767
DAL代码:
#region 出租情况表
        /// 
        /// 出租情况表
        /// 
        /// 小区名称
        /// 时间拼接字符串
        /// 
        public DataTable RentalSituationList(string xqmc, string datas)
        {
            DataSet ds = new DataSet();
            ORACLEDB oracledb = new ORACLEDB(null);
            OracleParameter[] parms ={
                     oracledb.MakeInParam("v_xqmc",OracleType.NVarChar,32,xqmc),
                     oracledb.MakeInParam("v_datas",OracleType.NVarChar,1000,datas)
            };
            ds = oracledb.GetProcDataSet("GZF_RentalSituationList", parms);
            return ds.Tables[0];
        }
        #endregion

 

 



你可能感兴趣的:(ORACLE)