使用if else语句更新

时间:2014-08-22 06:54:46

标签: sql sql-server

我尝试使用下面的SQL查询,但收到错误消息:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'IF'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'THEN'.

这是SQL查询

UPDATE TBLGPS 
IF speed >= 0
THEN SET REMARKS = 'Running'
ELSE SET REMARKS = 'Stopped' 
WHERE PLATENO = 'ALCORAN-WIH312' AND TRXTIME = '13:16:20'

谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:3)

IF不能用于"常规" SQL语句,改为使用CASE

UPDATE TBLGPS 
   set remarks = case
                    when speed >= 0 then 'Running'
                    else 'Stopped'
                 end
WHERE PLATENO = 'ALCORAN-WIH312' 
AND TRXTIME = '13:16:20'