1. 程式人生 > >Oracle MERGE語句

Oracle MERGE語句

oracle merge

MERGE語句,它可以在一個SQL語句中對一張表或一個視圖同時做更新或插入操作,避免了多個INSERT、UPDATE和DELETE語句。它可以從一個或多個數據源中選擇數據對目標表做更新或插入操作,你可以指定在何種條件下更新或插入目標表或視圖。不可以對同一行做多次更新操作。例如:實現用B表來更新A表數據,如果A表中沒有,則把B表的數據插入A表。

11gR2語法如下:

MERGE [ hint ]   
   INTO [ schema. ] { table | view } [ t_alias ]   
   USING { [ schema. ] { table | view }
         | subquery 
         } [ t_alias ]   
   ON ( condition )
   [ merge_update_clause ]
   [ merge_insert_clause ]
   [ error_logging_clause ] ;
   
#merge_update_clause   
WHEN MATCHED THEN
UPDATE SET column = { expr | DEFAULT }
           [, column = { expr | DEFAULT } ]...
[ where_clause ]
[ DELETE where_clause ]

#merge_insert_clause
WHEN NOT MATCHED THEN
INSERT [ (column [, column ]...) ]
VALUES ({ expr | DEFAULT }
          [, { expr | DEFAULT } ]...
       )
[ where_clause ]

#error_logging_clause
LOG ERRORS 
  [ INTO [schema.] table ]
  [ (simple_expression) ]
  [ REJECT LIMIT { integer | UNLIMITED } ]

使用例子說明MERGE語句的作用:

創建測試表emp_m

[email protected]>create table emp_m as select * from emp where comm is not null;

Table created.

[email protected]>select * from emp_m;

     EMPNO ENAME			  JOB				     MGR HIREDATE		    SAL       COMM     DEPTNO
---------- ------------------------------ --------------------------- ---------- ------------------- ---------- ---------- ----------
      7499 ALLEN			  SALESMAN			    7698 1981-02-20 00:00:00	   1600        300	   30
      7521 WARD 			  SALESMAN			    7698 1981-02-22 00:00:00	   1250        500	   30
      7654 MARTIN			  SALESMAN			    7698 1981-09-28 00:00:00	   1250       1400	   30
      7844 TURNER			  SALESMAN			    7698 1981-09-08 00:00:00	   1500 	 0	   30

例子1:插入和更新操作

現在測試表emp_m中只有comm不為空的數據,現在要把在emp表而不在emp_m表中的數據插入到emp_m表中,並把emp_m表中現有的數據的comm增加10。

[email protected]>MERGE INTO EMP_M M
  2  USING EMP N
  3  ON (M.EMPNO = N.EMPNO)
  4  WHEN NOT MATCHED THEN
  5    INSERT
  6      (M.EMPNO, M.ENAME, M.JOB, M.MGR, M.HIREDATE, M.SAL, M.COMM, M.DEPTNO)
  7    VALUES
  8      (N.EMPNO, N.ENAME, N.JOB, N.MGR, N.HIREDATE, N.SAL, N.COMM, N.DEPTNO)
  9  WHEN MATCHED THEN
 10    UPDATE SET M.COMM = M.COMM + 10;

14 rows merged.

[email protected]
/* */>select * from emp_m; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ------------------------------ --------------------------- ---------- ------------------- ---------- ---------- ---------- 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 310 30 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 510 30 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1410 30 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 10 30 7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20 7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10 14 rows selected.

例子2:實現刪除操作

從emp_m表中刪除表emp中DEPTNO=30的記錄。

[email protected]>MERGE INTO EMP_M M
  2  USING (SELECT EMPNO FROM EMP WHERE DEPTNO = 30) N
  3  ON (M.EMPNO = N.EMPNO)
  4  WHEN MATCHED THEN
  5    UPDATE SET M.COMM = DEFAULT 
  6    DELETE WHERE M.EMPNO = N.EMPNO;

6 rows merged.

[email protected]>select * from emp_m;

     EMPNO ENAME                          JOB                                MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ------------------------------ --------------------------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH                          CLERK                             7902 1980-12-17 00:00:00        800                    20
      7566 JONES                          MANAGER                           7839 1981-04-02 00:00:00       2975                    20
      7782 CLARK                          MANAGER                           7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT                          ANALYST                           7566 1987-04-19 00:00:00       3000                    20
      7839 KING                           PRESIDENT                              1981-11-17 00:00:00       5000                    10
      7876 ADAMS                          CLERK                             7788 1987-05-23 00:00:00       1100                    20
      7902 FORD                           ANALYST                           7566 1981-12-03 00:00:00       3000                    20
      7934 MILLER                         CLERK                             7782 1982-01-23 00:00:00       1300                    10

使用註意事項:

  • 不能指定一個在它上面定義了INSTEAD OF觸發器的視圖為目標視圖。

  • USING子名指定源數據,可以是表、視圖或子查詢

  • UPDATE子句中的where條件可以指定目標表的條件也可以指定源表的條件

  • UPDATE子句中不能更新ON條件中的列,更新表時不能指定DEFAULT

  • UPDATE子句中指定DELETE WHERE用於刪除數據。

  • INSERT子句中,如果INSERT後省略列名,VALUES後的列的個數必須與目標表列個數一致

  • INSERT子句中,可以使用WHERE條件限制插入的數據


官方文檔:http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_9016.htm#SQLRF01606

本文出自 “DBA Fighting!” 博客,請務必保留此出處http://hbxztc.blog.51cto.com/1587495/1922868

Oracle MERGE語句