1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| 四种情况: a. 无条件 insert b. 有条件 insert all c. 转置 insert d. 有条件 insert first
a. 无条件的insert 创建两个测试表 create or replace table_b as select a,b,c from table_a where 1=2 create or replace table_c as select a,b,e from table_a where 1=2 insert all into table_b (a,b,c) values (a,b,c) into table_c (a,b,e) values (a,b,e) select a,b,c,e from table_a where a in ('a_1','a_2') 没有条件,所以table_b和table_c的条数一样 b.有条件的insert all delete table_b; delect table_c; 清空表table_b,table_c的数据 insert all when c in ('c_1','c-2') then into table_b (a,b,c) values (a,b,c) when e > 20 then into table_c (a,b,e) values (a,b,e) select a,b,c,e from table_a where a in ('a_1','a_2') d. 有条件的 insert frist insert first when c in ('c_1','c-2') then into table_b (a,b,c) values (a,b,c) when e > 20 then into table_c (a,b,e) values (a,b,e) select a,b,c,e from table_a where a in ('a_1','a_2') 当符合第一个条件之后插入table_b,即使满足第二个条件也不会插入table_c内,这是和 insert all 和 insert first 区别所在
e. 转置 insert (以其一个分类,不然说是insert all 的一个用法) 将不同列的数据插入到同一张表的不同行内 drop table t1; drop table t2; create table t2 (d varchar2(10), des varchar2(50)); create table t1 as select '熊1,精神不佳' as d1, '熊2,精神不佳' as d2, '熊3,精神不佳' as d3, '熊4,精神不佳' as d4, '熊5,精神不佳' as d5 from dual; insert all into t2(d,des) values ('周1',d1) into t2(d,des) values ('周2',d2) into t2(d,des) values ('周3',d3) into t2(d,des) values ('周4',d4) into t2(d,des) values ('周5',d5) select d1,d2,d3,d4,d5 form t1; select * from t2 周1 熊1,精神不佳 周2 熊2,精神不佳 周3 熊3,精神不佳 周4 熊4,精神不佳 周5 熊5,精神不佳 等价于sql语句 insert t2 (d,des) select '周1',d1 from t1 union all select '周2',d2 from t1 union all select '周3',d3 from t1 union all select '周4',d4 from t1 union all select '周5',d5 from t1 ;
|