合并命令会导致“缺少关键字”错误

时间:2012-07-04 08:11:46

标签: sql oracle plsqldeveloper

我对此查询有疑问:

  MERGE INTO qot
  USING dual
  ON (qot_id = 1023125885)
  WHEN MATCHED AND qot_exc_id = 4 THEN UPDATE SET qot_exc_id = 259
  WHEN MATCHED AND qot_exc_id = 6 THEN UPDATE SET qot_exc_id = 131;

我收到了错误消息:'缺少关键字'。 有人能给我任何线索吗?

谢谢!

问候,

1 个答案:

答案 0 :(得分:4)

我不知道为什么你只想更新时试图使用MERGE,从不插入。您不能像正在做的那样在WHEN子句中添加条件 - 请参阅documentation

为什么不这样做:

update qot
set qot_exc_id = case qot_exc_id
                    when 4 then 259
                    when 6 then 131
                    end
where qot_id = 1023125885
and qot_exc_id in (4,6);

如果你真的需要一个MERGE,那么你需要类似的东西:

MERGE INTO qot
USING dual
ON (qot_id = 1023125885)
WHEN MATCHED THEN 
  UPDATE SET qot_exc_id = case qot_exc_id
                            when 4 then 259
                            when 6 then 131
                            else qot_exc_id
                            end;

或者也许:

MERGE INTO qot
USING dual
ON (qot_id = 1023125885 and qot_exc_id in (4,6))
WHEN MATCHED THEN 
  UPDATE SET qot_exc_id = case qot_exc_id
                            when 4 then 259
                            when 6 then 131
                            end;