Search This Blog

Wednesday, July 27, 2022

BULK COLLECT / TYPE Record in Oracle

DECLARE 
   sql_stmt_   VARCHAR2(32000);
   
   CURSOR get_data IS
      SELECT column_1, 
             column_2, 
             column_3, 
             column_4, 
             column_5
      FROM   some_tab
      WHERE  ROWNUM <= 10; -- ROWNUM is optional
   
   -- create type using a cursor
   TYPE temp_rec_type IS TABLE OF get_data%ROWTYPE INDEX BY BINARY_INTEGER;
   temp_rec_   temp_rec_type;
BEGIN
   -- Way 1 - Load data using normal cursor open fecth
   --OPEN  get_data;
   --FETCH get_data BULK COLLECT INTO temp_rec_;
   --CLOSE get_data;
   
   -- Way 2 - Load data using executing sql statement via EXECUTE IMMEDIATE
   sql_stmt_ := 'SELECT column_1, 
                        column_2, 
                        column_3, 
                        column_4, 
                        column_5
                 FROM   some_tab
                 WHERE  ROWNUM <= 10';
   
   EXECUTE IMMEDIATE sql_stmt_ BULK COLLECT INTO temp_rec_;
   
   -- loop of fetch data
   FOR i_ IN 1..temp_rec_.COUNT LOOP
      Dbms_Output.Put_Line(temp_rec_(i_).column_1 || ';' ||
                           temp_rec_(i_).column_2 || ';' ||
                           temp_rec_(i_).column_3 || ';' ||
                           temp_rec_(i_).column_4 || ';' ||
                           temp_rec_(i_).column_5 ||);
   END LOOP;
END;

Useful Links:

> Bulk Processing with PL/SQL

No comments:

Post a Comment

Read file content from an Oracle Directory in PL/SQL/ in IFS

Read single file DECLARE file_handle_ UTL_FILE.FILE_TYPE; directory_name_ CONSTANT all_directories.directory_name%TYPE := '...

Popular Posts