简体   繁体   中英

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?

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?

The only reason you'd use Bulk Collect and FORALL to insert rows is when you absolutely need to process/insert rows in chunks. Otherwise always use 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
/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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