我有一个查询,在MySQL工作得很好,但当我在Oracle上运行它时,我得到以下错误:
SQL错误:ORA-00933: SQL命令未正确结束 00933. 00000 - "SQL命令未正确结束"
查询为:
UPDATE table1
INNER JOIN table2 ON table1.value = table2.DESC
SET table1.value = table2.CODE
WHERE table1.UPDATETYPE='blah';
我有一个查询,在MySQL工作得很好,但当我在Oracle上运行它时,我得到以下错误:
SQL错误:ORA-00933: SQL命令未正确结束 00933. 00000 - "SQL命令未正确结束"
查询为:
UPDATE table1
INNER JOIN table2 ON table1.value = table2.DESC
SET table1.value = table2.CODE
WHERE table1.UPDATETYPE='blah';
当前回答
甲骨文基地在这方面做得很好。
https://oracle-base.com/articles/misc/updates-based-on-queries
从这个链接-我使用了上述查询的修改,这对我来说没有作用(答案来自mathguy,它使用rowid)
MERGE /*+ APPEND PARALLEL(8) */ INTO dest_table tt
USING source_table st
ON (tt.identifier = st.identifier)
WHEN MATCHED THEN
UPDATE SET tt.number = st.number;
这里我有两个表:source和dest,它们都有一个共同的varchar字段,我将源标识字段(PK)添加到dest表中。
其他回答
UPDATE (SELECT T.FIELD A, S.FIELD B
FROM TABLE_T T INNER JOIN TABLE_S S
ON T.ID = S.ID)
SET B = A;
A和B是别名字段,不需要指向表。
该语法在Oracle中无效。你可以这样做:
UPDATE table1 SET table1.value = (SELECT table2.CODE
FROM table2
WHERE table1.value = table2.DESC)
WHERE table1.UPDATETYPE='blah'
AND EXISTS (SELECT table2.CODE
FROM table2
WHERE table1.value = table2.DESC);
或者你可以这样做:
UPDATE
(SELECT table1.value as OLD, table2.CODE as NEW
FROM table1
INNER JOIN table2
ON table1.value = table2.DESC
WHERE table1.UPDATETYPE='blah'
) t
SET t.OLD = t.NEW
这取决于Oracle是否认为内联视图是可更新的 (第二个语句是否可更新取决于列出的一些规则 这里)。
UPDATE ( SELECT t1.value, t2.CODE
FROM table1 t1
INNER JOIN table2 t2 ON t1.Value = t2.DESC
WHERE t1.UPDATETYPE='blah')
SET t1.Value= t2.CODE
UPDATE table1 t1
SET t1.value =
(select t2.CODE from table2 t2
where t1.value = t2.DESC)
WHERE t1.UPDATETYPE='blah';
用这个:
MERGE
INTO table1 trg
USING (
SELECT t1.rowid AS rid, t2.code
FROM table1 t1
JOIN table2 t2
ON table1.value = table2.DESC
WHERE table1.UPDATETYPE='blah'
) src
ON (trg.rowid = src.rid)
WHEN MATCHED THEN UPDATE
SET trg.value = code;