从过程执行函数但获得转换失败错误(Varchar到Int)

时间:2015-01-23 20:37:55

标签: sql-server stored-procedures sql-function

我有一个在循环中调用函数的过程。 我正在使用sql server 2008

程序

Alter PROCEDURE CalculateIndividualPoint
@WinningTeam varchar(50),
@MatchId varchar(50)

AS
BEGIN
Declare @LoopCounter INT = 1
Declare @Result INT
Declare @TotalCount INT

Select @TotalCount = COUNT(MatchId) from tblPlayerPoints Where MatchId = @MatchId

While @LoopCounter < @TotalCount
Begin
    Select @Result = dbo.PointsLoop(@MatchId,@WinningTeam,@LoopCounter)
    Update tblPlayerPoints
    Set CalcPoints = @Result 
    Where UserId = @LoopCounter
End
END

Function

ALTER FUNCTION [dbo].[PointsLoop]
(
@matchId varchar(50),
@WinningTeam varchar(50),
@LoopCounter int
)
RETURNS INT
AS
BEGIN
DECLARE @pointer INT
Declare @MatchPoints1 INT
Declare @MatchPoints2 INT
Declare @FinalPoints Float
Declare @Xfactor varchar(50)

if @WinningTeam = 'Team1'
Begin
    Select @Xfactor = Team1Total from tblSchedule where MatchId = @matchId
    Select @Xfactor = CONVERT(Float,@Xfactor)
End
Else
Begin
    Select @Xfactor = Team2Total from tblSchedule Where MatchId = @matchId
    Select @Xfactor = CONVERT(Float,@Xfactor)
End

    Select @MatchPoints1 = Team1Points, @MatchPoints2 = Team2Points from tblPlayerPoints 
    where MatchId =@matchId and UserId = @LoopCounter
    Select @MatchPoints1 = CONVERT(float,@MatchPoints1)
    Select @MatchPoints2 = CONVERT(float,@MatchPoints2)
    if  @WinningTeam = 'Team1'
    Begin
        if @MatchPoints1 = 0 
        Begin
            Set @FinalPoints = - @MatchPoints2
        End
        Else
        Begin
            Set @FinalPoints = @MatchPoints1 * @Xfactor 
        End
    End 
    Else
    Begin 
        if @MatchPoints2 = 0 
        Begin
            Set @FinalPoints = - @MatchPoints1
        End
        Else
        Begin
            Set @FinalPoints = @MatchPoints2 * @Xfactor 
        End 
    End
Select @FinalPoints = CONVERT(int,@FinalPoints)
RETURN @FinalPoints

END

执行程序后,我收到以下错误。 Team1Total和Team2Total是varchar数据类型所以我声明Xfactor为varchar(我接收0.714286值)然后将其转换为float仍然得到相同的错误

  

转换varchar值时转换失败&#39; 0.714286&#39>数据类型int。

2 个答案:

答案 0 :(得分:0)

在下面的语句中,您试图将计算的FLOAT值推送到名为 @FinalPoints

的INTEGER数据类型变量中
Select @FinalPoints = @MatchPoints1 * @Xfactor

Select @FinalPoints = @MatchPoints2 * @Xfactor 

答案 1 :(得分:0)

为了计算结果,SQL需要将公式的所有部分转换为相同的数据类型。在这种情况下,由于结果是int,并且公式中的第一个变量也是int,@Xfactor被隐式转换为int,因为变量的值显然不是整数,所以它失败了。

试试这个,你会得到同样的错误:

DECLARE @Xfactor varchar(20) = '0.714286';
SELECT CONVERT(int, @Xfactor)

在你的情况下,我建议引入变量@Xfactor_float并在公式中使用它。

此外,我建议始终将所有变量显式地转换为所需的精度。例如,虽然SQL默认情况下会将数据类型转换为精度最高的数据类型,但它可能不是理想的方式。例如,您可能不希望将所有内容隐式转换为float而不是decimal。

要解决您的评论 - 请将您的代码更改为:

ALTER FUNCTION [dbo].[PointsLoop]
(
@matchId varchar(50),
@WinningTeam varchar(50),
@LoopCounter int
)
RETURNS INT
AS
BEGIN
DECLARE @pointer INT
Declare @MatchPoints1 INT
Declare @MatchPoints2 INT
Declare @FinalPoints Float
Declare @Xfactor varchar(50)
DECLARE @Xfactor_float float;

if @WinningTeam = 'Team1'
Begin
    Select @Xfactor = Team1Total from tblSchedule where MatchId = @matchId
    Select @Xfactor_float = CONVERT(Float,@Xfactor)
End
Else
Begin
    Select @Xfactor = Team2Total from tblSchedule Where MatchId = @matchId
    Select @Xfactor_float = CONVERT(Float,@Xfactor)
End

    Select @MatchPoints1 = Team1Points, @MatchPoints2 = Team2Points from tblPlayerPoints 
    where MatchId =@matchId and UserId = @LoopCounter
    Select @MatchPoints1 = CONVERT(float,@MatchPoints1)
    Select @MatchPoints2 = CONVERT(float,@MatchPoints2)
    if  @WinningTeam = 'Team1'
    Begin
        if @MatchPoints1 = 0 
        Begin
            Set @FinalPoints = - @MatchPoints2
        End
        Else
        Begin
            Set @FinalPoints = @MatchPoints1 * @Xfactor_float
        End
    End 
    Else
    Begin 
        if @MatchPoints2 = 0 
        Begin
            Set @FinalPoints = - @MatchPoints1
        End
        Else
        Begin
            Set @FinalPoints = @MatchPoints2 * @Xfactor_float 
        End 
    End
Select @FinalPoints = CONVERT(int,@FinalPoints)
RETURN @FinalPoints

END