コードがいつ入力されたか、またはいつ閲覧されたかをチェックする以下のクエリがあります。
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文には2つの書き方があり、あなたはその2つを組み合わせて使っているようだ。
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に変換する必要があるかもしれないからです。