merge的妙用

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)起结构和输出结果如下:
        ID NAME
---------- ----
         1 b
         2 a
可以看出以上两个结果的值是相反的,现在我们只需要使用虚表(T)的结果去更新test表的数据就好了,
这里我们就可以使用merge去实现。
具体如下:
构造虚表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 a
2、使用第二种方案实现以上情况(结合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


你可能感兴趣的:(oracle,实战,merge,11g)