Incorrect syntax near '<'

房东的猫 提交于 2019-12-13 06:48:57

问题


I have a task to get some code which is working correctly on SQL Server 2012 to work on SQL Server 2008 R2 as well. I got this error:

Additional information: Incorrect syntax near '<'

When I try to run my code I found out that something is wrong in this line of my SQL code

ALTER TABLE [dbo].[WorkTimeEntries] 
  ADD [TimeFinishedForRuntime] AS ISNULL([TimeFinished],
        IIF ([TimeStarted] < SYSUTCDATETIME(), [dbo].[udf_GetCurrentDateTimeOffsetInTimeZone](DATENAME(TZOFFSET, [TimeStarted])), [TimeStarted]));

I have read that in this cases took place some kind of error when people try to get date, but I'm not sure what's wrong in my case.


回答1:


There was no IIF in SQL Server 2008R2.

Replace it with CASE

ALTER TABLE [dbo].[WorkTimeEntries] ADD [TimeFinishedForRuntime] AS ISNULL(
    [TimeFinished],
    CASE WHEN [TimeStarted] < SYSUTCDATETIME() THEN [dbo].[udf_GetCurrentDateTimeOffsetInTimeZone](DATENAME(TZOFFSET, [TimeStarted])) ELSE [TimeStarted] END);


来源:https://stackoverflow.com/questions/39300057/incorrect-syntax-near

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!