可读性太低的SQL语句
最近做开发,大多的功能都在sql上完成,用sql直接求出所需要的报表,然后DataSource一下,功能就完成了,CS开发简洁,速度也快。
但后续的发现让我头痛不已,SQL表套表套的太多了,可读性非常差,想修改一个功能,增加一个字段,或出现一个BUG,都寸步难行。
1 select b.仓位,cast(a.应发 as decimal(10, 2)) as 应发,b.型号,b.仓位内码,b.物料名称,b.物料内码,a.原单单号,a.原单分录,a.原单类型,b.仓库内码, 2 'SEOUT002431' as 单据号,b.日期,c.仓库数量,a.发货单分录 from ( 3 select (i.FQty-i.FAuxStockQty) as 应发,FItemID as 物料内码,i.FSourceBillNo as 原单单号,i.FSourceEntryID as 原单分录, 4 i.FSourceInterId as 原单内码,i.FSourceTranType as 原单类型,i.FEntryID as 发货单分录 5 from SEOutStock h inner join SEOutStockEntry i on h.FInterID=i.FInterID where h.FBillNo='SEOUT002431' 6 ) a inner join ( 7 select c.FName as 仓位,b.FModel as 型号 ,b.FName as 物料名称,a.materialID as 物料内码, 8 a.wareID as 仓位内码,a.wareHouseID as 仓库内码,(cast(y as varchar(5))+'-'+cast(m as varchar(5))) as 日期 9 from ( 10 select SUM(qty) 仓库数量,materialID,wareID,wareHouseID,YEAR(prodate) as y,MONTH(prodate) as m 11 from xt_CodeInfo group by materialID,wareHouseID,wareID,wareHouseID,YEAR(prodate), MONTH(prodate) 12 ) a left join t_ICItem b on a.materialID=b.FItemID 13 left join t_StockPlace c on a.wareID=c.FSPID 14 where a.wareHouseID!=0 and a.materialID in 15 (select i.FItemID from SEOutStock h inner join SEOutStockEntry i on h.FInterID=i.FInterID where h.FBillNo='SEOUT002431' ) 16 ) b on a.物料内码=b.物料内码 left join ( 17 select SUM(qty) 仓库数量,materialID,wareID,wareHouseID,cast(YEAR(prodate) as varchar(10)) +'-'+ cast(MONTH(prodate) as varchar(20)) as 日期 18 from xt_CodeInfo where flag=1 group by materialID,wareHouseID,wareID,wareHouseID,YEAR(prodate), MONTH(prodate) 19 ) c on b.物料内码=c.materialID and b.日期=c.日期 and b.仓位内码=c.wareID where 应发>0 order by a.原单分录,b.日期,b.物料内码,b.仓位内码
目前网上没有找到可读性强的SQL写法,我觉得SQL还是写简单点好,虽然效率低了点,但好修改