这是SQL UPDATE我正在尝试执行:
UPDATE T
SET T.CurrentStopNumber = TS.CurrentStopNumber
FROM Trip T
INNER JOIN (SELECT TripId, MIN(StopNumber) CurrentStopNumber
FROM TripStop
WHERE TripId = '106504'
AND (IsPickup = 1 OR IsDrop = 1)
AND StopNumber > (SELECT COALESCE(max(StopNumber), 0)
FROM TripUpdate
WHERE TripId = '106504'
AND Type = 2)) TS ON T.TripId = TS.TripId
我在第二行收到错误:
/* Error message: SQL script is wrong mismatched input . expecting "EQ" */
我熟悉SQL Server,我确信它可以在SQL Server上运行得很好。子查询运行正常并返回1行,如我所料。我只需要用该值更新表。有什么问题?
答案 0 :(得分:2)
使用:
UPDATE TRIP
SET currentstopnumber = (SELECT MIN(ts.stopnumber)
FROM TRIPSTOP ts
WHERE ts.tripid = TRIP.tripid
AND ts.tripid = '106504'
AND 1 IN (ts.ispickup, ts.isdrop)
AND ts.stopnumber > (SELECT COALESCE(MAX(tu.stopnumber), 0)
FROM TRIPUPDATE tu
WHERE tu.tripid = ts.tripid
AND tu.type = 2)
GROUP BY ts.tripid)
WHERE EXISTS (SELECT NULL
FROM TRIPSTOP ts
WHERE ts.tripid = TRIP.tripid
AND ts.tripid = '106504'
AND 1 IN (ts.ispickup, ts.isdrop)
AND ts.stopnumber > (SELECT COALESCE(MAX(tu.stopnumber), 0)
FROM TRIPUPDATE tu
WHERE tu.tripid = ts.tripid
AND tu.type = 2))
最好使用JOIN,但语法不受广泛支持。
答案 1 :(得分:0)
我认为你不应该为你设置的字段指定表,因为数据库已经知道要更新哪个表:
SET CurrentStopNumber = TS.CurrentStopNumber
(IIRC,SQL Server中也不允许这样做。)