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?
There are a few steps:
GRANT read,write on DIRECTORY my_dir to the_user;
I have used this and it works really nicely.
Usage
data_dump ( 'Select emp_name from emp',
CURRENT_TIMESTAMP||'filename.csv',
my_dir);
(vastly simplified sample!)
After creating the directory verify your work by running this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With