为搜索错误投入了大量时间。我使用了其他两个工作函数作为构建这个的指南,但是,唉,我到目前为止都被打败了。有人能告诉我为什么这会返回语法错误:
FUNCTION FN_USER_MATRIX_PriceTweak
(
@Cost float, @CostAvg float,
@Tier1MaxVal float, @Tier1Mult float,
@Tier2MaxVal float, @Tier2Mult float,
@Tier3MaxVal float, @Tier3Mult float,
@Tier4MaxVal float, @Tier4Mult float,
@Tier5MaxVal float, @Tier5Mult float,
@Above5Mult float
)
RETURNS float
AS
BEGIN
DECLARE @Result float
SET @Result =
(
IF @Cost <= @CostAvg
CASE
WHEN @Cost <= @Tier1MaxVal THEN @Cost * @Tier1Mult
WHEN @Cost <= @Tier2MaxVal THEN @Cost * @Tier2Mult
WHEN @Cost <= @Tier3MaxVal THEN @Cost * @Tier3Mult
WHEN @Cost <= @Tier4MaxVal THEN @Cost * @Tier4Mult
WHEN @Cost <= @Tier5MaxVal THEN @Cost * @Tier5Mult
ELSE @Cost * @Above5Mult
ELSE
CASE
WHEN @CostAvg <= @Tier1MaxVal THEN @CostAvg * @Tier1Mult
WHEN @CostAvg <= @Tier2MaxVal THEN @CostAvg * @Tier2Mult
WHEN @CostAvg <= @Tier3MaxVal THEN @CostAvg * @Tier3Mult
WHEN @CostAvg <= @Tier4MaxVal THEN @CostAvg * @Tier4Mult
WHEN @CostAvg <= @Tier5MaxVal THEN @CostAvg * @Tier5Mult
ELSE @CostAvg * @Above5Mult
)
RETURN @Result
END
我的错误讯息:
Msg 156,Level 15,State 1,Procedure FN_USER_MATRIX_PriceTweak,Line 21 关键字&#39; IF&#39;附近的语法不正确。
Msg 156,Level 15,State 1,Procedure FN_USER_MATRIX_PriceTweak,Line 22 关键字&#39; CASE&#39;附近的语法不正确。
答案 0 :(得分:1)
您应该使用嵌套CASE
,其中包含IF...ELSE
:
SET @Result =
(
CASE WHEN @Cost <= @CostAvg THEN (
CASE
WHEN @Cost <= @Tier1MaxVal THEN @Cost * @Tier1Mult
WHEN @Cost <= @Tier2MaxVal THEN @Cost * @Tier2Mult
WHEN @Cost <= @Tier3MaxVal THEN @Cost * @Tier3Mult
WHEN @Cost <= @Tier4MaxVal THEN @Cost * @Tier4Mult
WHEN @Cost <= @Tier5MaxVal THEN @Cost * @Tier5Mult
ELSE @Cost * @Above5Mult END)
ELSE
(CASE
WHEN @CostAvg <= @Tier1MaxVal THEN @CostAvg * @Tier1Mult
WHEN @CostAvg <= @Tier2MaxVal THEN @CostAvg * @Tier2Mult
WHEN @CostAvg <= @Tier3MaxVal THEN @CostAvg * @Tier3Mult
WHEN @CostAvg <= @Tier4MaxVal THEN @CostAvg * @Tier4Mult
WHEN @CostAvg <= @Tier5MaxVal THEN @CostAvg * @Tier5Mult
ELSE @CostAvg * @Above5Mult END)
END
)
答案 1 :(得分:1)
请试试这个:
IF @Cost <= @CostAvg
BEGIN
SET @Result = CASE
WHEN @Cost <= @Tier1MaxVal THEN @Cost * @Tier1Mult
WHEN @Cost <= @Tier2MaxVal THEN @Cost * @Tier2Mult
WHEN @Cost <= @Tier3MaxVal THEN @Cost * @Tier3Mult
WHEN @Cost <= @Tier4MaxVal THEN @Cost * @Tier4Mult
WHEN @Cost <= @Tier5MaxVal THEN @Cost * @Tier5Mult
ELSE @Cost * @Above5Mult
END
END
ELSE
BEGIN
SET @Result = CASE
WHEN @CostAvg <= @Tier1MaxVal THEN @CostAvg * @Tier1Mult
WHEN @CostAvg <= @Tier2MaxVal THEN @CostAvg * @Tier2Mult
WHEN @CostAvg <= @Tier3MaxVal THEN @CostAvg * @Tier3Mult
WHEN @CostAvg <= @Tier4MaxVal THEN @CostAvg * @Tier4Mult
WHEN @CostAvg <= @Tier5MaxVal THEN @CostAvg * @Tier5Mult
ELSE @CostAvg * @Above5Mult
END
END
RETURN @Result