oracle批量插入

cod*_*ver 6 sql oracle plsql bulkinsert

我不熟悉PLSQL,但是我必须为任务执行批量插入.

基本上我必须查询表一来获得一列,然后在另一个表上使用它来插入它.像这样的东西:

for (ids in a file As cur_id)
{
Select DISTINCT column1 As col1_list from table1 where id=cur_id

for (cols in col1_list as cur_col)
  Insert into table2 values ('cur_id','cur_col','214','234','first 3 chars of cur_col')
}
Run Code Online (Sandbox Code Playgroud)

现在,我在文件中有大约4k + id,每个id将具有不同范围的不同col1:最大值:1.65亿,min~2k

我试图实现这个"从一个表中读取并插入其他使用批量插入",如果这可以在一夜之间运行等等.

我从网上的一些研究中得到了这个脚本:

CREATE OR REPLACE PROCEDURE test_proc
IS
TYPE TObjectTable IS TABLE OF ALL_OBJECTS%ROWTYPE;
ObjectTable$ TObjectTable;

BEGIN
   SELECT * BULK COLLECT INTO ObjectTable$
     FROM ALL_OBJECTS;

     FORALL x in ObjectTable$.First..ObjectTable$.Last
     INSERT INTO t1 VALUES ObjectTable$(x) ;
END;
Run Code Online (Sandbox Code Playgroud)

我认为这可能对我的情况有用,但我不太了解语义.我在哪里提到column1 ...对于插入值也表示为ObjectTable $(x)而不是值(..,..,..).

有人可以向我解释脚本,并帮助我使用我在我的示例中提到的table1,table2,col1,ids等变量将其修改为我的用例.

DB为10g

谢谢 !

HAL*_*000 5

您根本不需要批量收集.哎呀,你甚至不需要PL/SQL - SQL也可以进行批量插入!

只需创建一个与目标表的%rowtype匹配的视图

create view v_Table1_Table2 as
 (select   id,
           max(case when /* condition for column1 */ 
                    then /* expression for column1 */ 
                    else null; 
               end) as column1,
           max(case when /* condition for column2 */ 
                    then /* expression for column2 */ 
                    else null; 
               end) as column2,
           max(case when /* condition for column3 */ 
                    then /* expression for column3 */ 
                    else null; 
               end) as column3
  from     table1
  group by id
 )
Run Code Online (Sandbox Code Playgroud)

然后

insert into table2 (select * from v_Table1_Table2 where id = :cur_id);
Run Code Online (Sandbox Code Playgroud)