日期:2014-05-18  浏览次数:20486 次

下列SQL语句,如何转化成存储过程?(带参数)
如题:下列是一个库存查询语句,凡以粗体显示的字符,均通过参数传递实现


select tbsptm.f_sptm sptm,tbspda.f_spmc spmc,c.qmsl,(tbspda.f_zhjj*(1+tbspda.f_sl/100)) 

zhjj,tbspda.f_xsdj xsdj,c.qmsl*(tbspda.f_zhjj*(1+tbspda.f_sl/100)) qmjjje ,c.qmsl*tbspda.f_xsdj qmje

into #kccx
from (select a.f_spbm spbm,(a.qcsl+b.fssl) qmsl
from (select f_spbm,sum(f_qcsl) qcsl
from tb200710_yhj where f_bmbm like '2020%' and f_spbm in (select f_spbm 
from tbsptm where f_xh='1' and f_sptm like '%' and f_spbm in (select f_spbm from tbgysdz where f_gysbm='001013')) group by f_spbm) a,
(select f_spbm,(sum(f_jhsl)+sum(f_Nbjhsl)+sum(f_Brsl)+sum(f_Jgbrsl)+sum(f_Sysl)+sum(f_Pssl)-sum(f_Xssl)-sum(f_Nbxssl)-sum(f_Bcsl)-sum(f_Jgbcsl)-sum(f_Shsl)-sum(f_Phsl)) fssl
 from tb200710_rhj where f_rq<='20071018' and f_bmbm like '2020%' and f_spbm 

in 
(select f_spbm from tbsptm where f_xh='1' and f_sptm like '%' and f_spbm in 
(select f_spbm from tbgysdz where f_gysbm='001013')) group by f_spbm) b 
where a.f_spbm=b.f_spbm) c,tbspda,tbsptm where c.spbm=tbspda.f_spbm 
and c.spbm=tbsptm.f_spbm and tbsptm.f_xh='1' and f_sptm like '%'


insert into #kccx 
select tbsptm.f_sptm sptm,tbspda.f_spmc spmc,a.qcsl,(tbspda.f_zhjj*(1+tbspda.f_sl/100)) zhjj,tbspda.f_xsdj xsdj,a.qcsl*(tbspda.f_zhjj*(1+tbspda.f_sl/100)) qmjjje,a.qcsl*tbspda.f_xsdj
from (select f_spbm,f_qcsl qcsl
from tb200710_yhj where f_bmbm like '2020%' and f_spbm in (select f_spbm 
from tbsptm where f_xh='1' and f_sptm like '%' and f_spbm in (select f_spbm from tbgysdz where f_gysbm='001013'))
and f_spbm not in (select f_spbm from tb200710_rhj where f_rq<='20071018' and f_bmbm like '2020%'
and f_spbm in (select f_spbm from tbgysdz where f_gysbm='001013'))) a,tbspda,tbsptm where a.f_spbm=tbspda.f_spbm 
and a.f_spbm=tbsptm.f_spbm and tbsptm.f_xh='1' and tbsptm.f_sptm like '%'


select * from #kccx order by sptm

drop table #kccx

------解决方案--------------------
把用到的条件作为参照传参
------解决方案--------------------
拼揍字符串动态执行:有传参变量作为字符串加起来执行
在执行之前用print 显示检查执行的语句