[Oracle] Insert All的妙用


無條件的插入

Oracle中的insert all是指把同一批數據插入到不同的表中,假如如今有個需求:把t表中的數據分別插入t1,t2,假設你不知道insert all,你可能會使用insert插入2次,例如以下所看到的:

insert  into t1(object_name,object_id) select * from t;
insert  into t2(object_name,object_id) select * from t;
commit;
其實,以上這樣的寫法是錯誤的,由於在兩次insert的過程中,t表的數據有可能已經發生了變化,也就是說,t1,t2表得到的數據有可能不一樣,正確的寫法應該是採用insert all:

insert all
into t1(object_name,object_id)
into t2(object_name,object_id)
select * from t;
commit;


有條件的插入

insert first/all 是對每一行來進行推斷
兩者差別:
insert first:對於每一行數據,僅僅插入到第一個when條件成立的表,不繼續檢查其它條件。
insert all : 對於每一行數據,對每個when條件都進行檢查,假設滿足條件就運行插入操作。 
看以下的樣例:

--insert first
--前面等於1的條件被<=5含在內,FIRST就表示前面插入了,后面不會再插入了。
insert first
when object_id = 1 then
into t1(object_name,object_id)
when object_id <=5 then                      
into t2(object_name,object_id)
select * from t;
commit;

select * from t1;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
ICOL$                              1

select * from t2;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
I_USER1                            2
CON$                               3
UNDO$                              4
C_COBJ#                            5

--insert all
insert all
when object_id = 1 then
into t1(object_name,object_id)
when object_id <=5 then                      
into t2(object_name,object_id)
select * from t;
commit;

SQL> select * from t1;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
ICOL$                              1
SQL> select * from t2;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
ICOL$                              1
I_USER1                            2
CON$                               3
UNDO$                              4
C_COBJ#                            5

行轉列插入

insert all還能夠實現行轉列插入:
select * from sales_source_data;
EMPLOYEE_ID    WEEK_ID  SALES_MON  SALES_TUE  SALES_WED SALES_THUR  SALES_FRI
----------- ---------- ---------- ---------- ---------- ---------- ----------
        176          6       2000       3000       4000       5000       6000

insert all
into sales_info values(employee_id,week_id,sales_mon)
into sales_info values(employee_id,week_id,sales_tue)
into sales_info values(employee_id,week_id,sales_wed)
into sales_info values(employee_id,week_id,sales_thur)
into sales_info values(employee_id,week_id,sales_fri)
select employee_id,week_id,sales_mon,sales_tue,
sales_wed,sales_thur,sales_fri
from sales_source_data;

select * from sales_info;
EMPLOYEE_ID       WEEK      SALES
----------- ---------- ----------
        176          6       2000
        176          6       3000
        176          6       4000
        176          6       5000
        176          6       6000

多表插入語句的限制條件

 1. 僅僅能對表運行多表插入語句,不能對視圖或物化視圖運行;
 2. 不能對遠端表運行多表插入語句;
 3. 不能使用表集合表達式;
 4. 不能超過999個目標列;
 5. 在RAC環境中或目標表是索引組織表或目標表上建有BITMAP索引時,多表插入語句不能並行運行;
 6. 多表插入語句不支持運行計划穩定性;
 7. 多表插入語句中的子查詢不能使用序列。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM