user3224907
user3224907

Reputation: 768

Oracle 18c - PL/SQL limiting records written to file

I have a procedure which creates a file with as many records returned from a cursor as possible. Due to downstream file size issues, I want to limit each file to ~500k(n) records (value to derive from a parameter table). If there are more than 500k(n) records in the cursor, a new file with a newer timestamp should be created. The procedure uses the following steps.

set v_date = to_char(SYSDATE, 'YYYYMMDDHH24MISS');
set new_file_name = 'new_file_'|| v_date
  open record cursor
  loop
    fetch bulk collect with 1000 record limit
    for loop
      write records to file
    end loop
    clear cache
  close cursor loop

Before down-voting, please leave a comment if you need more information. I am looking for the easiest solution as well as the most efficient solution.

Upvotes: 1

Views: 340

Answers (1)

APC
APC

Reputation: 146329

You need three loops. The outer loop manages each file, the middle loop fetches the records, the inner loop writes the records. So, adapting your pseudo-code....

open record cursor    
loop

    fetch bulk collect with 1000 record limit
    exit when 0

    record_count := 0;
    utl_file.fopen(dir, 'new_file_'||to_char(SYSDATE, 'YYYYMMDDHH24MISS'), 'W');

    loop

        for loop
             write records to file
        end loop

        record_count := record_count + 1000;
        if record_count = 50000 then exit;

        fetch bulk collect with 1000 record limit
        exit when 0

    end loop

    close file

end loop;
close record cursor;

Upvotes: 3

Related Questions