将Case语句值分配给变量

时间:2014-01-14 16:56:32

标签: sql case

我正在为PPK写一个函数。如果我消除了“END AS @PPK”,我可以使用这个Case语句,但是当我尝试将最终结果分配给我的变量时,我收到语法错误。谁能告诉我这里有什么问题:谢谢!

DECLARE @USL AS DECIMAL(5,2)
DECLARE @LSL AS DECIMAL(5,2)
DECLARE @AVG AS DECIMAL(5,2)
DECLARE @SDEV AS DECIMAL(5,2)

SET @USL = 6.143
SET @LSL = 2.9382
SET @AVG = 4.1
SET @SDEV = 1.6

DECLARE @PPK DECIMAL(5,2)

DECLARE @VAL1 DECIMAL(5,2)
DECLARE @VAL2 DECIMAL(5,2)

SET @VAL1 = (@USL - @AVG)/(3*@SDEV)
SET @VAL2 = (@AVG - @LSL)/(3*@SDEV)
SELECT @VAL1
SELECT @VAL2

SELECT CASE
    WHEN @VAL1 < @VAL2
    THEN @VAL1
    ELSE @VAL2 END
--END AS @PPK

1 个答案:

答案 0 :(得分:1)

SELECT @PPK = CASE
    WHEN @VAL1 < @VAL2
    THEN @VAL1
    ELSE @VAL2 END