我确信这很简单,但我似乎无法弄明白。为什么这段代码不起作用?
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.
答案 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