2013-10-26 55 views
2

嗨我有一个以下查询,检查代码以确定它是何时进入或查看。在选择查询中使用多个case语句

 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 [email protected]; 

当我尝试运行此查询它给了我错误

Msg 102, Level 15, State 1, Procedure p_xxxx, line 40 
Incorrect syntax near '='. 

有一些错误了Syntex在时线。请让我知道如何纠正这种 感谢

+4

删除'@ timestamp'。 –

回答

18

有写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 

组合是等效。它们可能不起作用,因为您可能需要将日期类型转换为变量以将它们附加到其他变量。