1、将test表id=1的name值修改成id=2的值,同时也将id=2的name值修改成id=1的。
SQL> CREATE TABLE TEST (id number, name varchar2(20)); Table created. SQL> INSERT INTO test VALUES(1, 'a'); 1 row created. SQL> INSERT INTO test VALUES(2, 'b'); 1 row created. SQL> commit; Commit complete. SQL> SELECT * FROM test; ID NAME ---------- ---- 1 a 2 b思路:更具上面构造的表结构和输出的结果,我们需要构造一个虚表(T)起结构和输出结果如下:
SELECT 1 id, (SELECT name FROM test WHERE id=2) name FROM dual UNION ALL SELECT 2 id, (SELECT NAME FROM test WHERE id=1) name FROM dual ID NAME ---------- ---- 1 b 2 a使用merge通过构造的虚表(T)更新test表。
MERGE INTO test USING ( SELECT 1 id, (SELECT name FROM test WHERE id=2) name FROM dual UNION ALL SELECT 2 id, (SELECT NAME FROM test WHERE id=1) name FROM dual )t ON (test.id = t.id) WHEN MATCHED THEN UPDATE SET test.name = t.name; 2 rows merged. SQL> select * from test; ID NAME ---------- -------------------- 1 b 2 a2、使用第二种方案实现以上情况(结合rowid来实现)。
SQL> select * from test; ID NAME ---------- ---- 1 a 2 b MERGE INTO test USING ( WITH t AS ( SELECT 1 id, (SELECT name FROM test WHERE id=2) name FROM dual UNION ALL SELECT 2 id, (SELECT name FROM test WHERE id=1) name FROM dual ) SELECT test.id, test.rowid as rn, t.name FROM test, t WHERE test.id = t.id ) n ON (test.rowid = n.rn) WHEN MATCHED THEN UPDATE SET test.name = n.name; 2 rows merged. SQL> select * from test; ID NAME ---------- ---- 1 b 2 a