这对我有用:
merge into table1
using table2
on (table1.column1 = table2.column1 and rownum = 1)
when mateched then
update
set column2 = table2.column2;
例如(Oracle 11gR2):
CREATE TABLE TST_MERGE
(ID NUMBER,
VAL NUMBER);
INSERT INTO TST_MERGE (ID, VAL)
VALUES (1,1);
INSERT INTO TST_MERGE (ID, VAL)
VALUES (1,2);
INSERT INTO TST_MERGE (ID, VAL)
VALUES (2,2);
INSERT INTO TST_MERGE (ID, VAL)
VALUES (2,3);
CREATE TABLE TST_MERGE1
(ID NUMBER,
VAL NUMBER);
INSERT INTO TST_MERGE1 (ID, VAL)
VALUES (1,-1);
INSERT INTO TST_MERGE1 (ID, VAL)
VALUES (2,-1);
MERGE INTO TST_MERGE a
USING (SELECT * FROM TST_MERGE1
WHERE ID = 1) b
ON (a.id = b.id AND rownum = 1)
WHEN MATCHED THEN UPDATE
SET a.val = b.val;
MERGE INTO TST_MERGE a
USING (SELECT * FROM TST_MERGE1
WHERE ID = 2) b
ON (a.id = b.id AND rownum = 1)
WHEN MATCHED THEN UPDATE
SET a.val = b.val;
SELECT * FROM TST_MERGE;
ID VAL
1 -1
1 2
2 -1
2 3