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

储存过程执行出现的问题
代码如下:

ALTER Procedure [dbo].[ShipDateQtyPush_Plan]
@S Varchar(10),
@F Varchar(10),
@FnSam Varchar(15),
@FnQty Varchar(15),@Grade Float
As
Create Table #a
(
PO Varchar(20),
Season Varchar(10),
Style Varchar(20),
ShipDate Varchar(10),
Customer Varchar(10),
Color Varchar(50),
Qty Int,
CuttingQty Int,
Qty226 Int,
Sam Float,
ActQty Int,
DiffQty Int
)
Declare @str Varchar(8000)
Set @str=' Insert into #a
Select 
PO,Season,Style,Convert(Varchar(10),ShipDate,120) ShipDate,Customer,Color, 
Sum(Qty) Qty,Sum(CuttingQty) CuttingQty,Sum(Qty226) Qty226,'+@FnSam+' Sam, 
Sum('+@FnQty+') ActQty,Sum(Qty-'+@FnQty+') DiffQty
from 
ShipDateQtyPush 
where  
ShipDate>='''+@S+''' and ShipDate<='''+@F+'''
Group by
PO,Season,Style,ShipDate,Customer,Color,Sam331 '
Exec(@str)
--------------------------------------------------------------------
declare @sql varchar(8000)
set @sql='select PO,Season,Style,Customer,ShipDate,Color,Qty,CuttingQty,Qty226,Round(Sam,2) Sam,ActQty'
select @sql=@sql+',['+dd+'ろ计]=sum(case Convert(Varchar(10),ShipDate,120) when '''+dd+''' then DiffQty else 0 end)'
+',['+dd+'惠]=sum(case Convert(Varchar(10),ShipDate,120) when '''+dd+''' then Round((Qty-ActQty)*(Sam'+@Grade+')/60,2) else 0 end)'
from (select distinct dd=Convert(Varchar(10),ShipDate,120) from #a) ss
set @sql=@sql+'  from #a group by PO,Season,Style,Customer,ShipDate,Color,Qty,CuttingQty,Qty226,Sam,ActQty'
exec(@sql)
Drop table #a

执行:
ShipDateQtyPush_Plan 
'2013-06-13',
'2013-06-22',
'Sam331',
'Qty331',
1
然后提示

(58 row(s) affected)
Msg 8114, Level 16, State 5, Procedure ShipDateQtyPush_Plan, Line 38
Error converting data type varchar to float.
我就是加了@Grade这个变量之后出错的,想请问该怎么做?

------解决方案--------------------
可以先把@sql打印出来后到查询分析器中检查语法有没有错误了
------解决方案--------------------
--需要转@Grade为文本才能添加在@sql中

declare @sql varchar(8000)
set @sql='select PO,Season,Style,Customer,ShipDate,Color,Qty,CuttingQty,Qty226,Round(Sam,2) Sam,ActQty'
select @sql=@sql+',['+dd+'ろ计]=sum(case Convert(Varchar(10),ShipDate,120) when '''+dd+''' then DiffQty else 0 end)'
+',['+dd+'惠]=sum(case Convert(Varchar(10),ShipDate,120) when '''+dd+''' then Round((Qty-ActQty)*(Sam'+convert(varchar,@Grade)+')/60,2) else 0 end)' ----这里
from (select distinct dd=Convert(Varchar(10),ShipDate,120) from #a) ss
set @sql=@sql+'  from #a group by PO,Season,Style,Customer,ShipDate,Color,Qty,CuttingQty,Qty226,Sam,ActQty'