我试图将两个日期之间的差异分成SQL Server 2005中的int。我收到错误 -
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'SELECT'.
SQL -
DECLARE @id varchar(10)
DECLARE @diff int
SET @id = 'D5234'
SET @diff = SELECT DATEDIFF(minute, [PROC].[START_DATE], [PROC].[END_DATE])
FROM [PROCESSES] AS [PROC]
WHERE [PROC].[ID] = @id
我该如何解决?
答案 0 :(得分:2)
您需要使用:
SELECT @diff = DATEDIFF(minute, [PROC].[START_DATE], [PROC].[END_DATE])
FROM [PROCESSES] AS [PROC]
WHERE [PROC].[ID] = @id;
或者
SET @diff = (SELECT DATEDIFF(minute, [PROC].[START_DATE], [PROC].[END_DATE])
FROM [PROCESSES] AS [PROC]
WHERE [PROC].[ID] = @id);
(用括号中的select语句)。
如果您有多个行匹配,前者只会将@diff设置为遇到的最后一行,如果您有多行,后者将失败。