设置变量值

时间:2009-09-09 21:41:01

标签: sql sql-server tsql sql-server-2000

我确信这很简单,但我似乎无法弄明白。为什么这段代码不起作用?

DECLARE @FirstSaturday DATETIME
DECLARE @ENDDATE DATETIME

SELECT @FirstSaturday = min(RED1.DATE)
FROM REDFRIDAYDATES..TBLREDFRIDAYALLDATES RED1
WHERE Period = 9 AND year = 2009
SELECT CASE
WHEN getdate() < @FirstSaturday
THEN  set @ENDDATE = getdate()
ELSE  SET @enddate = @FirstSaturday


END

我只想将早期日期,今天或期间结束的值分配给@enddate。

有人能指出我正确的方向吗?我正在使用SQL 2000.

3 个答案:

答案 0 :(得分:3)

SELECT @enddate = CASE
WHEN getdate() < @FirstSaturday THEN  getdate()
ELSE  @FirstSaturday
END

T-SQL中的CASE是一个表达式(如C中的?:),而不是IF之类的流控制分支。

答案 1 :(得分:1)

将关键字'End'添加到Case语句的末尾

 DECLARE @FirstSaturday DATETIME
 DECLARE @ENDDATE DATETIME
 SELECT @FirstSaturday = min(RED1.DATE)
 FROM REDFRIDAYDATES..TBLREDFRIDAYALLDATES RED1
 WHERE Period = 9 
     AND year = 2009

 SELECT @ENDDATE = 
    Case
      WHEN getdate() < @FirstSaturday
      THEN  getdate()
      ELSE  @FirstSaturday 
    End

答案 2 :(得分:1)

SELECT CASE只是没有任何意义,旅行>

DECLARE @FirstSaturday DATETIME
DECLARE @ENDDATE DATETIME

SELECT @FirstSaturday = min(RED1.DATE)
FROM REDFRIDAYDATES..TBLREDFRIDAYALLDATES RED1
WHERE Period = 9 AND year = 2009

SELECT @ENDDATE = CASE
WHEN getdate() < @FirstSaturday
THEN  getdate()
ELSE  @FirstSaturday
END