简体   繁体   English

批量收集Oracle

[英]Bulk Collect Oracle

Can bulk collect be done if data is getting inserted into table A from Table B and while selecting data, substr, instr, trunc functions has been used on columns fetched? 如果将数据从表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') ;

How can I insert data into table A using bulk collect? 如何使用批量收集将数据插入表A?

The only reason you'd use Bulk Collect and FORALL to insert rows is when you absolutely need to process/insert rows in chunks. 使用Bulk Collect和FORALL插入行的唯一原因是当您绝对需要以块的形式处理/插入行时。 Otherwise always use SQL. 否则,请始终使用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
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM