存储过程新方法萌芽 *——*
2015-08-22 20:43
411 查看
alter procedure [LC0019999].[pro_zlzjzxzb] @dw varchar(88), @ksrq varchar(8), @jsrq varchar(8) as begin create table #T_01 ( F_01 varchar(8), /*单位编号*/ F_02 varchar(8), /*单据类型*/ F_03 varchar(60), /*账户编号*/ F_04 varchar(8), /*账户类型*/ F_05 varchar(8), /*账户组*/ F_06 varchar(60), /*对方账户*/ F_0601 varchar(8), /*对方单位*/ F_07 DECIMAL(18,4), /*增加额*/ F_08 DECIMAL(18,4), /*减少额*/ F_09 DECIMAL(18,4), /*调拨额*/ F_0101 varchar(80), /*单位名称*/ F_10 DECIMAL(18,4), /*上周账户余额*/ F_11 DECIMAL(18,4), /*内部户*/ F_12 DECIMAL(18,4), /*归集行*/ F_13 DECIMAL(18,4), /*其他账户*/ F_14 DECIMAL(18,4), /*合计*/ F_15 VARCHAR(4), /*货币编号*/ F_16 VARCHAR(8), /*货币名称*/ F_17 DECIMAL(18,4),/*汇率*/ F_20 varchar(8) ) (SELECT LSBZDW.LSBZDW_DWBH hsdw into #hsdw FROM LSBZDW LSBZDW WHERE ( LSBZDW.LSBZDW_DWBH IN (SELECT DISTINCT AO_DATA FROM GSPAURESULT WHERE BIZOBJID='GlobalId' AND BIZOPID='GlobalId' AND AO_ID='GSP_SCF_LSBZDW' AND OWNERID=@dw) ) ) INSERT INTO #T_01 select zjzh_dwbh F_01,'' F_02,zjzh_zhbh F_03,zjzh_lxbh F_04,zjzh_zhz F_05,'' F_06,'' F_0601,0 F_07,0 F_08,0 F_09,lsbzdw_dwmc F_0101, 0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'000' F_20 from zjzh left join lsbzdw on zjzh_dwbh=lsbzdw_dwbh left join zjzhxz ON zjzh_XZBH=ZJZHXZ_XZBH where ZJZH_DWBH in (select hsdw from #hsdw ) INSERT INTO #T_01 select zjzh_DWBH F_01,rq F_02,zjzh_zhbh F_03,zjzh_nwbz F_04,zjzh_zhz F_05,zjzh_lxbh F_06,'' F_0601,0 F_07,0 F_08,dqye F_09,lsbzdw_dwmc F_0101, 0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,bzbh F_15,'' F_16,0 F_17,'YE00' F_20 from( select zjyhzhyeb_rq rq,zjyhzhyeb_zhnm,zjzh_zhbh,zjzh_mc,zjzh_nwbz,zjzh_XZBH,ZJZHXZ_XZMC,zjzh_DWBH,LSBZDW_DWMC,zjzh_lxbh,zjzh_zhz, zjzhz_mc,zjyhzhyeb_bz bzbh,lswbzd_bzmc,zjyhzhyeb_dqye dqye from zjzh inner join zjyhzhyeb on zjzh_zhnm=zjyhzhyeb_zhnm inner join LSBZDW ON zjzh_DWBH=LSBZDW_DWBH inner join zjzhxz ON zjzh_XZBH=ZJZHXZ_XZBH inner join zjzhz on zjzhz_zhzbh=zjzh_zhz inner join lswbzd on lswbzd_bzbh=zjyhzhyeb_bz where 1=1 and zjyhzhyeb_rq between dateadd(d,-1,@ksrq) and @jsrq and ZJZH_DWBH in (select hsdw from #hsdw ) union all select zjzhryeb_rq rq,zjzhryeb_zhnm,zjzh_zhbh,zjzh_mc,zjzh_nwbz,zjzh_XZBH,ZJZHXZ_XZMC,zjzh_DWBH,LSBZDW_DWMC,zjzh_lxbh,zjzh_zhz, zjzhz_mc,zjzhryeb_bzbh bzbh,lswbzd_bzmc,abs(zjzhryeb_qcye) dqye from zjzh inner join zjzhryeb on zjzh_zhnm=zjzhryeb_zhnm and zjzh_nwbz='0' inner join LSBZDW ON zjzh_DWBH=LSBZDW_DWBH inner join zjzhxz ON zjzh_XZBH=ZJZHXZ_XZBH inner join lswbzd on lswbzd_bzbh=zjzhryeb_bzbh inner join zjzhz on zjzhz_zhzbh=zjzh_zhz where 1=1 and zjzhryeb_rq between dateadd(d,-1,@ksrq) and @jsrq and ZJZH_DWBH in (select hsdw from #hsdw ) ) yeb INSERT INTO #T_01 SELECT zh.zjzh_dwbh F1,zjjsdj_djlx F2,zh.ZJZH_ZHBH F3,zh.zjzh_lxbh F4,zh.zjzh_zhz F5,ZJJSDJ_DFZH F6,df.zjzh_dwbh F61, isnull(SUM(CASE WHEN ZJJSDJ_DJLX IN ('01','06','07','11','15') THEN ZJJSDJ_JE END),0) F7, isnull(SUM(CASE WHEN ZJJSDJ_DJLX IN ('02','05','09','12','15') THEN ZJJSDJ_JE END),0) F8, isnull(SUM(CASE WHEN ZJJSDJ_DJLX IN ('15') THEN ZJJSDJ_JE END),0) F9, LSBZDW_DWmc F_0101,0 F_10,0 F11,0 F_12,0 F_13,0 F_14,zjjsdj_yhbz F_15,'' F_16,1 F_17, '0' F20 FROM ZJZH zh LEFT JOIN ZJJSDJ ON ZJZH_ZHNM=ZJJSDJ_YHZH OR ZJZH_ZHNM=ZJJSDJ_NBZH left join zjzh DF on df.zjzh_zhbh=zjjsdj.zjjsdj_dfzh left join LSBZDW ON zh.zjzh_DWBH=LSBZDW_DWBH left join zjzhxz ON zh.zjzh_XZBH=ZJZHXZ_XZBH WHERE 1=1 and ZJJSDJ_DJZT='11' AND zjjsdj_ywrq>=@ksrq and zjjsdj_ywrq<=@jsrq and zh.ZJZH_DWBH in (select hsdw from #hsdw ) GROUP BY zh.zjzh_dwbh,zjjsdj_djlx,zh.ZJZH_ZHBH,ZJJSDJ_DFZH,zh.zjzh_lxbh,zh.zjzh_zhz,df.zjzh_dwbh,LSBZDW_DWmc,zjjsdj_yhbz,zh.zjzh_XZBH,ZJZHXZ_XZMC /*---汇率------------------------------------------------------*/ insert into #T_01 select '' F_01,'' F_02,'' F_03,'' F_04,'' F_05,'' F_06,'' F_0601,0 F_07,0 F_08,0 F_09,'' F_0101, 0 F_10,0 F_11,0 F_12,0 F_13,isnull(hl.lshl_hl,1) F_14,LSWBZD_BZbh F_15,LSWBZD_BZMC F_16,isnull(hl.lshl_qmhl,1) F_17,'HL00' F_20 from LSWBZD zd left join LSHLLX2015 lx on zd.LSWBZD_BZbh= lx.lshllx_ybz left join LSHL2015 hl on lx.LSHLLX_BH=hl.lshl_bh WHERE LSHL_QJXH='08' or zd.LSWBZD_BZbh='01' update #T_01 set F_14=HL.F_14,F_16=HL.F_16,F_17=HL.F_17 from #T_01 A,(select * from #T_01 B where B.F_20='HL00') HL where A.F_20 in ('0','YE00') and A.F_15=HL.F_15 --select top 1 * from #T_01 where F_20 in ('0','YE00') and F_15<>'01' and F_01='huaqi' and F_03='3602000109200173293' update #T_01 set F_07=F_07*F_17,F_08=F_08*F_17,F_09=F_09*F_17 where F_20 in ('0','YE00') --select top 1 * from #T_01 where F_20 in ('0','YE00') and F_15<>'01' and F_01='huaqi' and F_03='3602000109200173293' /*---上周账户余额-----*/ insert into #T_01 select F_01,'','','','','','',0,0,sum(F_09),F_0101,sum(F_09) F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'YE01' F_20 from #T_01 where F_02=dateadd(d,-1,'20150810') and F_20='YE00' and F_01<>'jszx' group by F_01,F_0101 /*---内部户-----*/ insert into #T_01 select F_01,'','','','','','',0,0,sum(F_09),F_0101,0 F_10,sum(F_09) F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'YE02' F_20 from #T_01 where F_02='20150816' and F_04='0' and F_20='YE00' and F_01<>'jszx' group by F_01,F_0101 /*---归集行-----*/ insert into #T_01 select F_01,'','','','','','',0,0,sum(F_09),F_0101,0 F_10,0 F_11,sum(F_09) F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'YE03' F_20 from #T_01 where F_02='20150816' and F_05 in ('01','02','06','07') and F_20='YE00' and F_01<>'jszx' group by F_01,F_0101 /*---其他户-----*/ insert into #T_01 select F_01,'','','','','','',0,0,sum(F_09),F_0101,0 F_10,0 F_11,0 F_12,sum(F_09) F_13,0 F_14,'' F_15,'' F_16,0 F_17,'YE04' F_20 from #T_01 where F_02='20150816' and F_05='04' and F_20='YE00' and F_01<>'jszx' group by F_01,F_0101 /*---1. +调拨-----------------------------------*/ insert into #T_01 select F_01,'','','','','','',sum(F_07),sum(F_08),sum(F_09),F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'1' F_20 from #T_01 where F_20='0' and F_01<>'jszx' group by F_01,F_0101 union all select F_01,'','','','','','',sum(F_07),sum(F_08),sum(F_09),F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'20' F_20 from #T_01 where F_20='0' and F_04='08' and F_05='03' and F_01<>'jszx' group by F_01,F_0101 /**/ /*---2.本周增+减---------------------*/ insert into #T_01 select F_01,'','','','','','',-abs(sum(F_07+F_08)),-abs(sum(F_07+F_08)),0,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'2' F_20 from #T_01 where F_20='20' and F_01<>'jszx' group by F_01,F_0101 /*---3.本企业内部银行账户转账-----------------*/ insert into #T_01 select F_01,'','','','','','',sum(F_07),0,0,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'31' F_20 from #T_01 where F_02 in ('01','11','15') and F_0601=F_01 and F_20='0' and F_01<>'jszx' group by F_01,F_0101 union all select F_01,'','','','','','',0,sum(F_08),0,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'31' F_20 from #T_01 where F_02 in ('02','12','15') and F_0601=F_01 and F_20='0' and F_01<>'jszx' group by F_01,F_0101 insert into #T_01 select F_01,'','','','','','',-abs(sum(F_07)),-abs(sum(F_08)),0,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'3' F_20 from #T_01 where F_20='31' group by F_01,F_0101 /*---4.汇总 4---------------------------------*/ insert into #T_01 select F_01,'','','','','','',sum(F_07),sum(F_08),0,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'4' F_20 from #T_01 where F_20 in ('1','2','3') group by F_01,F_0101 /*---5.汇总 增加、减少额、上周余额、内部... 5---------------------------------*/ insert into #T_01 select F_01,'','','','','','',sum(F_07),sum(F_08),0,F_0101,sum(F_10),sum(F_11),SUM(F_12),sum(F_13),Sum(F_14),'' F_15,'' F_16,0 F_17,'5' F_20 from #T_01 where F_20 in ('4','YE01','YE02','YE03','YE04') group by F_01,F_0101 /*==========JSZX=====================*/ INSERT INTO #T_01 select F_01,F_02,F_03,F_04,F_05,F_06,F_0601,F_07,F_08,F_09,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'JSZXYE' F_20 from #T_01 where F_01 ='jszx' and F_06='03' and F_20 in ('YE00') UNION ALL select F_01,F_02,F_03,F_04,F_05,F_06,F_0601,F_07,F_08,-ABS(F_09),F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'JSZXYE' F_20 from #T_01 where F_01 <>'jszx' and F_06='08' and F_05='03' and F_20 in ('YE00') /*---上周账户余额-----*/ INSERT INTO #T_01 select F_01,'','','','','','',0,0,0, F_0101,sum(F_09) F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'JSZX01' F_20 from #T_01 where F_02=dateadd(d,-1,'20150810') and F_20='JSZXYE' group by F_01,F_0101 /*---内部户-----*/ INSERT INTO #T_01 select F_01,'','','','','','',0,0,0, F_0101,0 F_10,sum(F_09) F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'JSZX02' F_20 from #T_01 where F_02='20150816' AND F_04='0' and F_20='JSZXYE' group by F_01,F_0101 /*---归集行-----没数据*/ INSERT INTO #T_01 select F_01,'','','','','','',0,0,0, F_0101,0 F_10,0 F_11,sum(F_09) F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'JSZX03' F_20 from #T_01 where F_02='20150816' and F_05 in ('01','02','06','07') and F_20='JSZXYE' group by F_01,F_0101 /*---其他户-----*/ insert into #T_01 select F_01,'','','','','','',0,0,0, F_0101,0 F_10,0 F_11,0 F_12,sum(F_09) F_13,0 F_14,'' F_15,'' F_16,0 F_17,'JSZX04' F_20 from #T_01 where F_02='20150816' and F_05='04' and F_20='JSZXYE' group by F_01,F_0101 insert into #T_01 SELECT '','','','','','','',0,0,0,'' F_0101,sum(F_10),sum(F_11),sum(F_12),sum(F_13),0 F_14,'' F_15,'' F_16,0 F_17,'JSZX05' F_20 FROM #T_01 WHERE F_20 IN ('JSZX01','JSZX02','jszx03','jszx04') group by F_01,F_0101 /*---1.本周增减额 -----*/ insert into #T_01 select F_01,F_02,F_03,F_04,F_05,F_06,F_0601,F_07,F_08,F_09,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'6' F_20 from #T_01 where F_01 ='jszx' and F_04='03' and F_20 in ('0') union all select F_01,F_02,F_03,F_04,F_05,F_06,F_0601,-ABS(F_07),-ABS(F_08),F_09,F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'6' F_20 from #T_01 where F_01<>'jszx' and F_04='08' and F_05='03' and F_20 in ('0') /*---2.汇总账户金额--------------------*/ insert into #T_01 select '','',F_03,'','','','',sum(F_07),sum(F_08),0,'' F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'7' F_20 from #T_01 where F_20='6' GROUP BY F_03 /*---3.本企业内部银行账户转账-------------*/ INSERT INTO #T_01 select '','','','','','','',sum(F_07),0,0,'' F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'30' F_20 from #T_01 where F_02 in ('01','11','15') and F_0601='JSZX' and F_20='6' union all select '','','','','','','',0,sum(F_08),0,'' F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'30' F_20 from #T_01 where F_02 in ('02','12','15') and F_0601='JSZX' and F_20='6' INSERT INTO #T_01 select F_01,'','','','','','',-abs(sum(F_07)),-abs(sum(F_08)),0,'' F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'8' F_20 from #T_01 where F_20='30' group by F_01 delete #T_01 where F_20='30' /*---4.JSZX 9 -----------------------------*/ insert into #T_01 select '','','','','','','',sum(F_07),sum(F_08),0,'' F_0101,0 F_10,0 F_11,0 F_12,0 F_13,0 F_14,'' F_15,'' F_16,0 F_17,'9' F_20 from #T_01 where F_20 IN ('7','8') /*---5.JSZX 9 -----------------------------*/ insert into #T_01 select 'JSZX','','','','','','',sum(F_07),sum(F_08),0, '广东省旅游控股集团有限公司资金结算中心' F_0101,sum(F_10),sum(F_11),SUM(F_12),sum(F_13),Sum(F_14),'' F_15,'' F_16,0 F_17,'jszx00' F_20 from #T_01 where F_20 in ('9','jszx05') /*--- -----------------------------*/ update #T_01 set F_02=zjzh_xzBH from #T_01 left join zjzh on F_01=zjzh_dwbh where F_20 in ('5','jszx00') update #T_01 set F_03=ZJZHXZ_XZmc from #T_01 left join zjzhxz ON F_02=ZJZHXZ_XZBH where F_20 in ('5','jszx00') insert into #T_01 select '▉小计' F_01,F_02+'01' F_02,'' F_03,'' F_04,'' F_05,'' F_06,'' F_0601,sum(F_07),sum(F_08),sum(F_09),F_03,sum(F_10),sum(F_11),sum(F_12),sum(F_13),sum (F_14), '' F_15,'' F_16,0 F_17,'jszxxj' F_20 from #T_01 where F_20 in ('5','jszx00') group by F_02,F_03 insert into #T_01 select '' F_01,'9000' F_02,'' F_03,'' F_04,'' F_05,'' F_06,'' F_0601,sum(F_07),sum(F_08),sum(F_09),'▉合计',sum(F_10),sum(F_11),sum(F_12),sum(F_13),sum (F_14), '' F_15,'' F_16,0 F_17,'jszxhj' F_20 from #T_01 where F_20 in ('5','jszxxj') insert into #T_01 SELECT '' F_01,'9010' F_02,'' F_03,'' F_04,'' F_05,'' F_06,'' F_0601,-abs(sum(zjjsdj_je)) F_07,0 F_08,0 F_09,'▉减:内部单位往来' F_0101,0 F_10,0 F_11, 0 F_12,0 F_13,0 F_14, '' F_15,'' F_16,0 F_17,'nbwl' F_20 FROM ZJZH zh LEFT JOIN ZJJSDJ ON ZJZH_ZHNM=ZJJSDJ_YHZH OR ZJZH_ZHNM=ZJJSDJ_NBZH WHERE 1=1 and ZJJSDJ_DJZT='11' AND zjjsdj_ywrq>=@ksrq and zjjsdj_ywrq<=@jsrq and zh.ZJZH_DWBH in (select hsdw from #hsdw ) and zjjsdj_wldwid in (select hsdw from #hsdw ) insert into #T_01 SELECT '' F_01,'9020' F_02,'' F_03,'' F_04,'' F_05,'' F_06,'' F_0601,sum(F_07) F_07,0 F_08,0 F_09,'▉合计' F_0101,0 F_10,0 F_11, 0 F_12,0 F_13,0 F_14, '' F_15,'' F_16,0 F_17,'nbwlhj' F_20 from #T_01 where F_20 in ('jszxhj','nbwl') update #T_01 set F_14=F_10+F_07-F_08 where F_20 in ('jszx00','5','jszxxj','jszxhj','nbwl','nbwlhj') select F_01,F_0101,F_10,F_07,F_08,F_11,F_12,F_13,F_14 from #T_01 where F_20 in ('jszx00','5','jszxxj','jszxhj','nbwl','nbwlhj') order by F_02 end go [pro_zlzjzxzb] '406fa89b-e1d2-4869-9380-2793b581aa0a','20150810','20150816'
相关文章推荐
- hdoj1754 I Hate It【线段树区间最大值维护+单点更新】
- sqlite3使用示例
- Git服务器搭建+上传代码
- poj1611 并查集 找感染者
- ssoj2386数字8
- 13 Roman to Integer
- c++/tcl编程总结
- Java生成CSV文件
- Android 儿子Activity在启动过程中的流程组件 && 儿子Activity在一个新的进程组件启动过程
- hdu 5294 Tricks Device
- 【LeetCode】263. Ugly Number
- chrome分辨率测试工具
- J2EE分布式事务中的提交、回滚方法调用异常。
- poj2299 查找逆序数 归并
- sql优化
- Mongodb集群之副本集
- bzoj4034
- 两个特殊的线性空间
- 数据仓库之ETL导读
- 高精度加法和乘法