多表的insert語句
無條件的 INSERT ALL
insert all
into test1 (empno,ename) values(empno,ename)
into test1(ename,sal) values(ename,sal)
select * from emp;
有條件的INSERT ALL語句(對於每一行數據,對每一個when條件都進行檢查,如果滿足條件就執行插入操作)
-- 有條件的INSERT ALL語句
insert all
when sal>1000 then
into test1 (empno,sal,mgr) values (empno,sal,mgr)
when comm<500 then
into test1 (empno,comm,mgr) values(empno,comm,mgr)
select empno,sal,mgr,comm
from emp;
有條件的 FIRST INSERT語句(對於每一行數據,只插入到第一個when條件成立的表,不繼續檢查其他條件)
-- 有條件的 FIRST INSERT語句
-- 對於每一行數據,只插入到第一個when條件成立的表,不繼續檢查其他條件。
insert first
when sal>1000 then
into test1 (empno,sal,mgr) values (empno,sal,mgr)
when comm<500 then
into test1 (empno,comm,mgr) values(empno,comm,mgr)
select empno,sal,mgr,comm
from emp;
INSERT ALL 和INSERT FIRST對比
SQL> -- 有條件的INSERT ALL語句
SQL> insert all
2 when sal>1000 then
3 into test1 (empno,sal,mgr) values (empno,sal,mgr)
4 when comm<500 then
5 into test1 (empno,comm,mgr) values(empno,comm,mgr)
6 select empno,sal,mgr,comm
7 from emp;
已創建14行。
SQL> -- 有條件的 FIRST INSERT語句
SQL> -- 對於每一行數據,只插入到第一個when條件成立的表,不繼續檢查其他條件。
SQL> insert first
2 when sal>1000 then
3 into test1 (empno,sal,mgr) values (empno,sal,mgr)
4 when comm<500 then
5 into test1 (empno,comm,mgr) values(empno,comm,mgr)
6 select empno,sal,mgr,comm
7 from emp;
已創建12行。
旋轉INSERT (行轉列插入)
-- 旋轉INSERT (行轉列插入)
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_source_data;
EMPLOYEE_ID WEEK_ID SALES_MON SALES_TUE SALES_WED SALES_THUR SALES_FRI
----------- ---------- ---------- ---------- ---------- ---------- ----------
176 6 2000 3000 4000 5000 6000
-- 轉存後的表
select * from sales_info;
EMPLOYEE_ID WEEK SALES
----------- ---------- ----------
176 6 2000
176 6 3000
176 6 4000
176 6 5000
176 6 6000