简体   繁体   中英

Reading And Writing to CSV in SQL

In oracle DB I have a table say T which has columns id, att1, att2, att3. Now i also have a CSV file which has lots of id's in it along with one additional parameter.How to insert att2 corresponding to each id in the DB. initially, csv is like

   id, random
   id, random
   .
   .

ie after running the PL/SQL script csv file will look like:

   id, random, att2
   id, random, att2
   .
   .
   .

UTL_FILE is a good option for such tasks.

There is also another option, EXTERNAL TABLE , but you cannot write to a CSV file using it.

Make sure you have CREATE DIRECTORY privilege, which is the most important first step for both these options.

Refer this link for more details:

https://oracle-base.com/articles/9i/external-tables-9i

Here's an example.

As a prerequisite, connected as a privileged user (SYS), I'm creating a directory (Oracle object) which points to a director on my disk, granting required privileges to user which will be using it. Also, I'm granting EXECUTE on UTL_FILE package to the same user.

SQL> connect sys/pwd@xe as sysdba
Connected.
SQL> create directory ext_dir as 'c:\temp';

Directory created.

SQL> grant read, write on directory ext_dir to hr;

Grant succeeded.

SQL> grant execute on utl_file to hr;

Grant succeeded.

SQL>

Input file is called SPORT.TXT and looks like this:

SQL> $type c:\temp\sport.txt
10,handball
20,football
30,tennis

A procedure, which utilizes UTL_FILE to read from and write into files, creates the SPORT_OUT.TXT file which has another value inserted between the first two values of the input file.

It contains comments, I hope you'll be able to follow the execution.

SQL> connect hr/hr@xe
Connected.
SQL> declare
  2    -- input and output file types
  3    l_file_in      utl_file.file_type;
  4    l_file_out     utl_file.file_type;
  5    -- directory object
  6    l_dir          varchar2(30) := 'EXT_DIR';
  7    -- input and output file name
  8    l_filename_in  varchar2(30) := 'sport.txt';
  9    l_filename_out varchar2(30) := 'sport_out.txt';
 10    -- the whole line from the input file
 11    l_text         varchar2(32767);
 12    -- ID from the input file
 13    l_id           varchar2(3);
 14    -- your "random" value from the input file
 15    l_random       varchar2(30);
 16    -- value from a table which will be inserted between L_ID and L_RANDOM
 17    l_dname        departments.department_name%type;
 18  begin
 19    -- open files
 20    l_file_in := utl_file.fopen(l_dir, l_filename_in, 'r', 32767);
 21    l_file_out := utl_file.fopen(l_dir, l_filename_out, 'w', 32767);
 22
 23    -- loop through input file
 24    begin
 25      loop
 26        -- fetch line by line
 27        utl_file.get_line(l_file_in, l_text, 32767);
 28        -- extract ID and "random" value
 29        l_id := substr(l_text, 1, instr(l_text, ',') - 1);
 30        l_random := substr(l_text, instr(l_text, ',') + 1, 30);
 31
 32        -- select value from DEPARTMENTS that matches the L_ID value
 33        select max(department_name )
 34          into l_dname
 35          from departments
 36          where department_id = l_id;
 37
 38        -- write line into the output file
 39        utl_file.put_line(l_file_out, l_id ||','||l_dname||','||l_random);
 40      end loop;
 41    exception
 42      when no_data_found then
 43        null;
 44    end;
 45    -- close files
 46    utl_file.fclose(l_file_in);
 47    utl_file.fclose(l_file_out);
 48  end;
 49  /

PL/SQL procedure successfully completed.

Let's check the result:

SQL> $type c:\temp\sport_out.txt
10,Administration,handball
20,Marketing,football
30,Purchasing,tennis

SQL>

Seems to be OK, kind of; now, this is just a simple example. It would be OK if you read UTL_FILE documentation and further explore its capabilities.

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