Export table to csv file by using procedure (csv name with timestamp)

how to export data from oracle table to csv file using pl/sql
oracle procedure to generate csv file
oracle query output to csv file
oracle report output in csv format
pl/sql output to file
generate flat file from oracle table
oracle base generating csv files
plsql csv

I want to export one table to each csv file and name the csv file with timestamp. For example, if I have a table t1, after export, a csv file name t1.YYYYMMDDHHMISS.csv is generated. Here is my code:

create or replace procedure 
T_to_CSV(Tname varchar2,Directory varchar2)

BEGIN

set colsep ,     -- separate columns with a comma
set pagesize 0   -- No header rows
set trimspool on -- remove trailing blanks
set headsep off  -- this may or may not be useful...depends on your 

spool timestamp.csv --don't know how to write in directory

select *
  from Tname

end

Here are my questions:

(1) I don't know how to output the csv file into the requried directory, how to fix the spool code please?

Should I use spool D:\path\filename.csv please?

(2) In the spool line, how to change the name of the csv file using the timestamp now() please?

Export table to csv file by using procedure (csv name with timestamp), There are a few steps: create a directory using `CREATE DIRECTORY my_dir as 'C:\dir';; make sure that Oracle has read,write on the folder on the computer  I want to export one table to each csv file and name the csv file with timestamp. For example, if I have a table t1, after export, a csv file name t1.YYYYMMDDHHMISS.csv is generated. Here is my cod

MySQL Export Table to CSV, MySQL provides an easy way to export the query's result into a CSV file that resides in the database Exporting data to a CSV file whose filename contains timestamp Third, we executed the statement by using the EXECUTE command. What I would like is adding the date and timestamp to the file name when creating and then open the new file at the end of my script. I know that I would have to add the following to export command:

Thanks kevin for sharing the procedure and it was very useful for me. I have customized the code:

  1. To Add the column names in the output csv file which was not working earlier.
  2. When i was passing the delimiter as parameter then it was adding the comma in the starting of every row(,1,2,3) which has been corrected.

I am also sharing the customized code which might help others. Customized code can be downloaded here.

  1. Customized Code to add column names
 FOR i IN t_describe.FIRST .. t_describe.LAST LOOP    
               IF i <> t_describe.LAST THEN          put('UTL_FILE.PUT(v_fh,'''||t_describe(i).col_name||'''||'''||v_delimiter||''');');
       ELSE    
          put('            UTL_FILE.PUT(v_fh,'''||t_describe(i).col_name||''');');    
       END IF;    
       END LOOP;    
       put('            UTL_FILE.NEW_LINE(v_fh);');
  1. Customized Code for delimiter

    IF i <> t_describe.LAST THEN put(' UTL_FILE.PUT(v_fh,"'||t_describe(i).col_name||'"(i) ||'''||v_delimiter||''');'); ELSE put(' UTL_FILE.PUT(v_fh,"'||t_describe(i).col_name||'"(i));'); END IF;

And the correct way to call the procedure is to bind the variable with values

data_dump(query_in => 'Select 1 from dual',file_in => 'file.csv',directory_in => 'MY_DIR', delimiter_in => '|' );

Thanks

Naveen

Export a PostgreSQL Table to a CSV File, Export data from a table to CSV using COPY statement Notice that the CSV file name that you specify in the COPY command must be written directly by the  In case you don’t have superuser privilege or you need to import CSV file from a client machine into a table in PostgreSQL server, you can use pgAdmin GUI tool to do that. Let’s remove all data of the persons table so that we can re-import data and see the effect. TRUNCATE TABLE persons; First, right click on the persons table, choose the

Creating dynamically generated CSV files containing SQL Server data, Being a strong advocate of using the SSIS toolbox, I experimented with an alternative solution. We shall utilize data from the FASB table (see below). We give our output file the name “FASB_” and set its type to csv (see above). based on existing stored procedures with SQL Server Reporting Services  we want to import oracle data into csv file so that later we can applied it on sql server. we have used below procedure to create csv file ,every thing is fine but the problem is that when we open csv file with excel the data in it is not in consistent format procedure is-----create or replace procedure dump_table_to_csv( p_tname in varchar2,

Writing select result to a csv file, Then use SSIS to export the results of that table to .csv . This is really handy for using a | delimiter in instances where the This procedure will generate a CSV file with the output of the query you pass as a parameter. The 2nd parameter is your CSV file name, make sure you pass the file name with a valid  Hi i am trying to print timestamp in yyyyMMdd format in file name. So in result i need to export csv file in this format , Also i want to store this into remote location how can i use path variable with this filename . Filename_yyyy.MM.dd.csv . Thanks

How can we export data to a CSV file whose filename name , Sometimes we need to export data into a CSV file whose name has a timestamp at To illustrate it we are using the following example − the data from table '​student_info' to the CSV file having a timestamp in its name. Is there a way using a DB2 database to systematically export all tables from a schema to separate csv files ? What I mean is to export all the tables in a schema without specifying table names. Maybe somehow getting all table names in a schema, then applying the export on those

Comments
  • Spool is for sqlplus and can only write to a file on your computer or a mapped drive. Directories are for writing files to the server the database is on or to a UNC path. Spool will not work inside a procedure but you can use DBMS_OUTPUT which the spool command will pick up the output of.
  • Maybe this will help: How to create a oracle sql scrpt spool file
  • Thanks ponder for this, it is very helpful. Do you know how run the script in link please?
  • No, sorry. But question looks very similar to yours - (directories, timestamps).
  • WOW, thanks kevin for the link. The sql code is much too complicated. Maybe I just need a simple version to achieve my simple goal. Can I fix my code a bit to get that please?
  • Hi, kevin. I have tried your method. What to input in my_dir please? I use D: or D:/, but return error. Thanks.
  • You need to use the name of the directory you created in the first step of my answer
  • Thanks for your patience. There are two issues (1) v_fh UTL_FILE.FILE_TYPE; doesn't pass compilation. (2) create directory should be in another command line please?
  • 1) you need to create a directory first, you can do this from sqlplus. the user must have CREATE DIRECTORY privilege 2) please provide the exact oracle error
  • You could also use csv.write_file() from www.williamrobertson.net/documents/refcursor-to-csv.shtml for a more flexible solution.