在存储过程SQL Server中将变量分配给案例语句

时间:2015-03-25 16:25:27

标签: sql-server stored-procedures case

我正在尝试将变量分配给存储过程中的case语句。

这是我的代码:

CREATE Procedure [dbo].[myProcedure] 
(
    @StartDate DATETIME = NULL,
    @EndDate DATETIME = NULL,
)
AS
    DECLARE @Type varchar
    SET @Type = NULL
BEGIN

    SELECT DISTINCT 
       @Type = CASE 
                  WHEN myTable.myVariable = '1' 
                    THEN 'No' 
               END AS 'Test'
    FROM myTable
    WHERE myTable.myDate BETWEEN @StartDate AND @EndDate
END

我收到的错误是myTable.myVariable多部分标识符无法绑定....我可能这样做完全错了虽然...我只是想把我的case语句分配给变量...

1 个答案:

答案 0 :(得分:0)

这对我有用:

DECLARE @I1 BIGINT
SET @I1 = (
SELECT  Top  1  CASE WHEN LIST_PRICE=75 THEN  [EAN]
      ELSE 0 END SPECIAL
  FROM dbo.x1)
  SELECT @I1