使用MERGE INTO 修改数据的时候,突然报了这个错误 ORA-00928: missing SELECT keyword
表结构如下:
查询sql:
merge into wine a
using(abc) b
on (b.id = a.wine_id)
when matched then
update set a.name = a.name || ‘-’ || b.name
when not matched then
insert(name, code, type) values(b.name||‘茅台’,b.age||‘01’,b.age||‘01’);
怎么看都没有发现错误,然后去查询了一下MERGE的语法,如下:
MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND […]…)
WHEN MATCHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATCHED THEN [execute something else here ! ]
其中加粗处的:write your query here,在这儿填写你的查询,
注意,这个地方是要的查询结果,是字段,而我因为这些操作的时候单表是直接符合结果的,直接使用了表名,直接导致了报错。
把sql进行了调整:
merge into wine a
using(select id, name, age from abc) b
on (b.id = a.wine_id)
when matched then
update set a.name = a.name || ‘-’ || b.name
when not matched then
insert(name, code, type) values(b.name||‘茅台’,b.age||‘01’,b.age||‘01’);
如上,仅把对应的sql调整了一下,USING中放置查询结果,查询正常。
1、2、3三条数据匹配上了,仅在名称后面添加即可
4不存在 ,新增了数据。
问题报错了以后,去查百度、CSDN再对比问题挺花时间的,不如直接看一下语法,大部分问题基本上可以看出来。剩下的再去百度,效率要好一些。