日期:2014-05-16  浏览次数:20396 次

简单存储过程改写,如何放入传入参数??
如何用存储过程,将下面的红字部分用给定的传入参数来代替?
红字部分为 diqu like '%%' and jhdanwei like '%%'   like后面的diqu传入与jhdanwei传入,''2014-01-01'' 与 ''2014-05-01'' 分别用传入的开启日期与结束日期来代替,请问该怎么写,另外调用该如何使用

DECLARE @sql VARCHAR(8000)  
DECLARE @str VARCHAR(8000)  
SELECT @sql = ISNULL(@sql , '') + N' sum(case when jhdanwei = N''' + jhdanwei + ''' then shuliang else 0 end) as [' +jhdanwei+N'|数量]' 
+N' , sum(case when jhdanwei = N''' + jhdanwei + N''' then ypjine else 0 end) as ['+jhdanwei+N'|金额]'+ N',' FROM (select distinct jhdanwei from Y_xskehu where diqu like '%%' and jhdanwei like '%%') t   
SET @sql = 'SELECT y_baseinfo.cpid , y_baseinfo.yppinming,'+ left(@sql , LEN(@sql) - 1 ) + N', 
ISNULL(sum(shuliang),0) as [合计|数量],
ISNULL(sum(ypjine),0) as [合计|金额]  
from (select cpid,yppinming,jinhuodanwei as jhdanwei,shuliang,ypjine from y_liushui where caozuobiaoshi<>''入库'' and convert(varchar(100),caozuoriqi,23)>=''2014-01-01'' and convert(varchar(100),caozuoriqi,23)<=''2014-05-01'') t  
right join y_baseinfo on t.cpid = y_baseinfo.cpid group by y_baseinfo.cpid,y_baseinfo.yppinming ' 
EXEC (@sql)

------解决方案--------------------
DECLARE @sql VARCHAR(8000)  
DECLARE @str VARCHAR(8000)  
SELECT @sql = ISNULL(@sql , '') + N' sum(case when jhdanwei = N''' + jhdanwei + ''' then shuliang else 0 end) as [' +jhdanwei+N'
------解决方案--------------------
数量]' 
+N' , sum(case when jhdanwei = N''' + jhdanwei + N''' then ypjine else 0 end) as ['+jhdanwei+N'
------解决方案--------------------
金额]'+ N',' FROM (select distinct jhdanwei from Y_xskehu where diqu like '%'''+@日期1+'''%' and jhdanwei like '%'''+@日期2+'''%') t   
SET @sql = 'SELECT y_baseinfo.cpid , y_baseinfo.yppinming,'+ left(@sql , LEN(@sql) - 1 ) + N', 
ISNULL(sum(shuliang),0) as [合计
------解决方案--------------------
数量],
ISNULL(sum(ypjine),0) as [合计
------解决方案--------------------
金额]  
from (select cpid,yppinming,jinhuodanwei as jhdanwei,shuliang,ypjine from y_liushui where caozuobiaoshi<>''入库'' and convert(varchar(100),caozuoriqi,23)>=''2014-01-01'' and convert(varchar(100),caozuoriqi,23)<=''2014-05-01'') t  
right join y_baseinfo on t.cpid = y_baseinfo.cpid group by y_baseinfo.cpid,y_baseinfo.yppinming ' 
EXEC (@sql)
试试这个