oracle 批量insert into
看了ITPUB博主的文章也才知道oracle也这种操作,记录一下。。
1.for 循环批量insert
create table T_TABLE_SQL ( ids NUMBER, col_name VARCHAR2(20) );
insert into t_table_sql values(001,'trans'); insert into t_table_sql values(002,'json'); create table t_table_sql_2 as select * from t_table_sql where 1=2; select * from t_table_sql; begin for i in (select * from t_table_sql) loop --一直都以为oracle不支持把"行"变量直接和insert操作关联起来使用,这里"行"变量这个名字是我给起的,不知道是否准确,只要大家能理解就ok. insert into t_table_sql_2 values i; --才晓得,oracle还有这种操作 commit; end loop; end; select * from t_table_sql_2; SQL> select * from t_table_sql_2; IDS COL_NAME ---------- -------------------- 1 trans 2 json
2.游标批量insert
2.1
truncate table t_table_sql_2; select * from t_table_sql_2; declare cursor cur_data is select * from t_table_sql; v_tab t_table_sql%rowtype; begin open cur_data; fetch cur_data into v_tab; while cur_data%found loop insert into t_table_sql_2 values v_tab; fetch cur_data into v_tab; end loop; close cur_data; commit; end;
2.2
truncate table t_table_sql_2; select * from t_table_sql_2; declare cursor cur_data is select * from t_table_sql; type t_tab_rec is record( ids t_table_sql.ids%type, col_name t_table_sql.col_name%type); t_tab t_tab_rec; begin open cur_data; fetch cur_data into t_tab; while cur_data%found loop insert into t_table_sql_2 values t_tab; fetch cur_data into t_tab; end loop; close cur_data; commit; end;