SQL Server 2008 r2: TSQL IIF 查询无法识别

SQL Server 2008 r2: TSQL IIF Query not recognized

我第一次尝试使用 IIF 而不是 CASE 和 SQL 服务器抛出此错误:

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '>'.

我的代码:

DECLARE @lDate date = CONVERT(date,GETDATE());
DECLARE @lMonth int = MONTH(@lDate);
DECLARE @lDay int = DAY(@lDate);

DECLARE @lPeriodStart date, @lPeriodEnd date, @lPayPeriod int, @lCutOffDay int = 14;

--there are 24 pay periods find which one
SET @lPayPeriod = @lMonth * 2 - IIF(@lDay > @lCutOffDay, 0, 1);

SELECT @lPayPeriod

我不明白这与 MSDN 的指南有何不同。

IIF 是在 SQL Server 2012 中引入的。当你用 SQL Server 2008 标记你的问题时,我会说这是问题的根源。

您的语法有效,代码可以在较新版本中运行。