有人可以在SQL中的IF语句中帮助我使用select语法。当我在IF中使用BEGIN和END时,我收到错误。
ALTER PROCEDURE Proc1 @MonthSelection AS INT = 0
AS
BEGIN
IF (@MonthSelection = 0)
BEGIN
SELECT
YEAR([EventStartDate]) AS [Year]
,MONTH([EventStartDate]) AS [Month Number]
,DATENAME(MONTH,DateAdd( month ,MONTH([EventStartDate]), 0 ) - 1) AS [Month]
,COUNT([UserID]) AS [Total Number of Logins]
FROM [Table1]
WHERE eventtype ='login'
GROUP BY YEAR([EventStartDate])
,MONTH([EventStartDate])
ORDER BY YEAR([EventStartDate])
,MONTH([EventStartDate]
END
ELSE
BEGIN
SELECT
YEAR([EventStartDate]) AS [Year]
,MONTH([EventStartDate]) AS [Month Number]
,DATENAME(MONTH,DateAdd( month ,MONTH([EventStartDate]), 0 ) - 1) AS [Month]
,COUNT([UserID]) AS [Total Number of Logins]
FROM [Table1]
WHERE eventtype ='login'
AND MONTH([EventStartDate]) = @MonthSelection
GROUP BY YEAR([EventStartDate])
,MONTH([EventStartDate])
ORDER BY YEAR([EventStartDate])
,MONTH([EventStartDate]
END
END
我确实尝试过使用Begin和ENd,但我仍然收到错误。这段代码放在存储过程中。
答案 0 :(得分:2)
您在END
关键字之前错过了一个右括号。
此:
...
ORDER BY YEAR([EventStartDate])
,MONTH([EventStartDate]
END
END
要:
...
ORDER BY YEAR([EventStartDate])
,MONTH([EventStartDate])
END
END