2011-12-09 190 views
6

我不熟悉PLSQL,但是我必须为任务执行批量插入操作。oracle批量插入

基本上我必须查询表一得到一列,然后在不同的表上使用它来插入它。事情是这样的:

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') 
} 

现在,我已经在文件中围绕4K + IDS和每个ID将有不同幅度的不同COL1的:最高:1.65亿,分〜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; 

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

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

该数据库为10g

谢谢!

+0

什么 “不同COL1的范围:最高:1.65亿,分〜2K” 一致的观点应该意思?请更好地描述table1和table2的列。 – Codo

回答

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 
) 

然后

insert into table2 (select * from v_Table1_Table2 where id = :cur_id);