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 --前面等于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
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