批量收集Oracle

时间:2013-04-25 18:15:54

标签: oracle oracle11g

如果数据从表B插入表A并且选择数据时,可以进行批量收集吗,在获取的列上使用了substr,instr,trunc函数?

INSERT INTO A
SELECT
DISTINCT 
    SUBSTR(b.component, 1, INSTR(b.component, ':', 1)  - 1),
    TRUNC(c.end_dt, 'DDD'),
FROM 
    B b,
    C c
WHERE 
    TRUNC(c.end_dt)=TRUNC(b.last_Date, 'DDD') ;

如何使用批量收集将数据插入表A?

1 个答案:

答案 0 :(得分:1)

使用Bulk Collect和FORALL插入行的唯一原因是您绝对需要以块的形式处理/插入行。否则总是使用SQL。

DECLARE 
   CURSOR c_data IS
   SELECT * FROM source_tab;
--
  TYPE t_source_tab IS TABLE OF source_tab%ROWTYPE;
  l_tab t_source_tab;
  v_limit  NUMBER:= 1000;
BEGIN
  OPEN c_data;
  LOOP
    FETCH c_data BULK COLLECT INTO l_tab LIMIT v_limit; 
    EXIT WHEN l_tab.count = 0;
    -- Insert --    
    FORALL i IN l_tab.first .. l_tab.last
      INSERT INTO destination_tab VALUES l_tab(i);
      COMMIT;

      -- prints number of rows processed --
      DBMS_OUTPUT.PUT_LINE ('Inserted ' || SQL%ROWCOUNT || ' rows:'); 

    -- Print nested table of records - optional. 
    -- May overflow the buffer and slow down the performance if you process many rows.
    -- Use for testing only and limit the rows with Rownum or Row_Number() in cursor query:
    FOR i IN l_tab.FIRST..l_tab.LAST -- or l_tab.COUNT
    LOOP
      DBMS_OUTPUT.PUT_LINE (l_tab(i).hire_date ||chr(9)||l_tab(i).last_name ||chr(9)||l_tab(i).first_name);
    END LOOP;    

 END LOOP;
CLOSE c_data;
END
/