Skip to Main Content
  • Questions
  • Automate export of result data to defined path

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Arif.

Asked: January 11, 2018 - 11:27 am UTC

Last updated: January 12, 2018 - 11:12 am UTC

Version: Oracle PL/SQL Developer 11g

Viewed 1000+ times

You Asked

Hi,

I am using Oracle 11g and mostly works on tasks which requires data extraction for clients. I have been trying to automate the process of extraction and I found the below code in one of your answers.

---------------------------------------------------------------------------------
create or replace directory csv_dir as '/destination/for/csv';

create or replace procedure write_file is
begin
  file_handle := utl_file.fopen('CSV_DIR', 'csv_filename.csv', 'w', 32767);
  for rws in (
    select * from t -- your query here
  ) loop
    utl_file.put_line(file_handle, 
      rws.c1 || ',' || rws.c2 || ',' || rws.c3 -- your columns here
    );
  end loop;
  utl_file.fclose(file_handle);
end write_file;
/

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name => 'EXPORT_CSV_J',
    job_type => 'PLSQL_BLOCK',
    job_action => 'begin write_file; end;',
    number_of_arguments => 0,
    start_date => NULL,
    repeat_interval => 'FREQ=DAILY',
    end_date => NULL,
    enabled => FALSE,
    auto_drop => FALSE);

  DBMS_SCHEDULER.SET_ATTRIBUTE( 
    name => 'EXPORT_CSV_J', 
    attribute => 'logging_level', 
    value => DBMS_SCHEDULER.LOGGING_RUNS);
  
  DBMS_SCHEDULER.enable(
    name => 'EXPORT_CSV_J');
END;
/

-----------------------------------------------------------------------------------------


But, when this will be executed, the directory will be created on server side which requires admin privileges.
I just want to know that is there a way so that I can create a directory on my local machine, so that I don't require admin privileges to extract and export the data to my defined path at local machine ??

Regards,
Arif Sher Khan

and Chris said...

No. UTL_FILE only writes to locations on the server.

If you want to unload data to your local machine, you need to use a client tool to do this.

If you're looking for a tool to help, Oracle SQL Developer has many options for exporting data:

https://docs.oracle.com/cd/E17781_01/server.112/e18804/impexp.htm#BABFDCDC
https://www.thatjeffsmith.com/archive/2013/10/defaults-for-exporting-data-in-oracle-sql-developer/

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library