日期:2014-05-17  浏览次数:20492 次

问问sql应该怎么写呢
sql应该怎么写,才是最佳的写法呢。(简洁易懂,效率高)
优化写法应该怎么优化呢,谢谢!

例如下面:
--exec sellorder 'HT-1207001'


alter PROCEDURE sellorder

@xqdaihao varchar(100)=''

as

select 

identity(int,1,1) as row, 
sod.autoid as 'sellid',
vm.modid as 'proid',
qm.autoid as 'bjid',
qmc.id as 'jyid',
qmr.id as 'blpid',
vmo.modid as 'bproid',
re.autoid as 'rukuid',
qq.autoid as 'fahuoid',
sb.sbvid as 'fapiaoid',
re1.autoid as 'brukuid',

sod.cdemandcode as '项目代号', 
sod.cdemandmemo as '项目名称',
sod.csocode as '销售订单号', 
sod.cinvcode as '存货编码',
i.cinvname as '存货名称',
sod.iquantity as '订单数量',

sod.dPreDate as '预发货日期',
so.dcreatesystime as '订单制单日期',
so.dverifydate as '订单审核日期',

bom.CreateDate as 'BOM创建日期',
bom.Relsdate as 'BOM审核日期',
bom.modifydate as 'BOM最后变更日期',
vm.mocode as '生产订单号',
VM.invcode as '母件编码',
i1.cinvname as '母件名称',
vm.qty as '生产订单数量',
vm.createdate as '生产订单制单日期',
vm.Relstime as '生产订单审核日期',


qm.cinspectcode as '报检单号',
qm.fquantity as '报检数量',
qm.dmaketime as '报检单制单日期',
qm.dverifytime as '报检单审核日期',

qmc.ccheckcode as '检验单号',
qmc.FREGQUANTITY as '合格接收数量',
qmc.dmaketime as '检验单制单日期',
qmc.dverifytime as '检验单审核日期',

re.ccode as '入库单号',
re.cinvcode as '产品编码',
re.iquantity as '入库数量',
re.dnmaketime as '入库单制单日期',
re.dnverifytime as '入库单审核日期',

qmr.crejectcode as '不良品处理单号',
qmr.FSUMQUANTITY as '待处理不良品数量',
qmr.DMAKETIME as '不良品处理单制单日期',
qmr.DVERIFYTIME as '不良品处理单审核日期',

vmo.mocode as '不良品生产订单',

re1.ccode as '入库单号B',
re1.cinvcode as '产品编码B',
re1.iquantity as '入库数量B',
re1.dnmaketime as '入库单制单日期B',
re1.dnverifytime as '入库单审核日期B',

qq.cdlcode as '发货单号',
qq.iQuantity as '发货数量',
qq.ddate as '发货单日期',


sb.cSBVCode as '销售发票号',
sb.dcreatesystime as '开票日期',
sb.dverifydate as '发票审核日期'



into #tmps

from SO_SODetails sod


left join Inventory i 
on i.cinvcode=sod.cinvcode

left join SO_SOMain so on so.csocode=sod.csocode 

left join DispatchLists dl
on dl.iSOsID=sod.iSOsID


left join QM_QREFsaLES qq
on qq.autoid =dl.autoid

left join saleBillVouch sb
on sb.cSOCode = sod.csocode


left join v_mom_modetail vm
on vm.demandcode=sod.cdemandcode and moclass='1' and (vm.invcode=sod.cinvcode or vm.invcode in 

  (
select invcode from v_bom_opcomponent_rpt where bomid in
(select bomid from v_bom_parent_rpt where invcode=sod.cinvcode) 
  )
)
left join Inventory i1 
on i1.cinvcode=vm.invcode

left join 
(
select vb.invcode, bb.CreateDate,bb. RelsDate,bb.modifydate from v_bom_parent_rpt vb ,bom_bom bb
where vb.bomid=bb.bomid and bb.bomtype=1
) bom
on (vm.invcode<>'' and bom.invcode=vm.invcode) or (vm.invcode='' and bom.invcode=sod.cinvcode)

left join QM_QPROINSPECTLIST qm
on qm.sourceautoid=vm.modid

left join QMCHECKVOUCHER qmc
on qmc. inspectautoid =qm.autoid 


left join recordinlist re
on re.icheckidbaks=qmc.id 


left join QMREJECTVOUCHER qmr
on qmr.sourceautoid =qmc. sourceautoid

left join v_mom_modetail vmo
on vmo.sourcemodid=qmr.sourceautoid 

left join recordinlist re1
on (re1.icheckidbaks in 

(select qmc1.id from QMCHECKVOUCHER qmc1 where qmc1.inspectautoid in 

( select qm1.autoid from QM_QPROINSPECTLIST qm1 where qm1.sourceautoid=vmo.modid)
)
  )

where sod.cdem