你好,我有一个下面的查询,用于检查代码,以确定代码是何时输入或查看的。
declare @timestamp datetime;
select
case @timestamp
when a.updatedDate =1760 then 'Entered on' +a.updatedDate
when a.updatedDate=1710 then 'Viewed on' +a.updatedDate
else 'Last Updated on'+ a.updatedDate
end
from t_mainTable a
where a.id=@Id;
当我尝试运行该查询时,它给出了错误信息
Msg 102, Level 15, State 1, Procedure p_xxxx, line 40
Incorrect syntax near '='.
when 行中有一些语法错误。请告诉我如何更正 谢谢
有两种编写 case 语句的方法,你似乎使用了这两种方法的组合
case a.updatedDate
when 1760 then 'Entered on' + a.updatedDate
when 1710 then 'Viewed on' + a.updatedDate
else 'Last Updated on' + a.updateDate
end
或
case
when a.updatedDate = 1760 then 'Entered on' + a.updatedDate
when a.updatedDate = 1710 then 'Viewed on' + a.updatedDate
else 'Last Updated on' + a.updateDate
end
是等价的。它们可能无法工作,因为您可能需要将日期类型转换为 varchars,以便将其追加到其他 varchars。