Automatically export result sets into csv - sql

I have to run several queries on an oracle 11g database, within SQLDeveloper 3.1.
For example:
select * from product;
select * from customer;
select * from prices;
At the moment I am exporting the resultsets "per hand", I simply right-clickonto the result and thenexport` it.
I would like to automatically save the resultset of each query in a specific folder.
Any recommendation how I could do that?
UPDATE
I tried using the csv and als the txt extesion of testFile:
spool C:\Users\User\Desktop\testFile.csv --I tried also .txt extension here!!!
set colsep ';'
select * from product;
spool off;
However, when I open the file I get for csv and txt the following result:
> set colsep '
> select * from product
I appreciate your replies!

set echo off
set feedback off
set linesize 1000
set pagesize 0
set sqlprompt ''
set trimspool on
spool output.csv
select columnA || ',' || columnB || ',' || ......
from table
where ...
spool off;
exit 0;
Then create a shell script that calls the sql file
sqlplus >/dev/null 2>&1 "user/pass#DATABASE" << EOF
whenever sqlerror exit 1
#file.sql
EOF
UPDATE just saw you are on windows, same principle still applies, you probably will need to use PowerShell

You can use Spool, http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12043.htm
spool OutFile.txt
select row1||','||row2... from product; --format you prefer
spool off;

Related

How do I fix the formatting in my spooled csv file in sql*plus

I am using the following script to spool output of a sql query to a csv file. The query extracts data from a view. My sql*plus version is 12.1.0.2.0
set colsep ,
set headsep off
set pagesize 0
set trimspool on
set NULL ' '
spool myfile.csv
select * from my_view;
spool off
The table has few columns with null values and I am required to produce the output like below.
12345,,,ABC,01-JAN-2020
But my actual output looks like this.
12345
A B C ,01-JAN-2020
Why all these whitespaces are coming even between the column data? For null values, there are new lines inserted and commas are missing.
How do I fix this?
I modified the select * statement as following to get the desired output.
select ColA||','||ColB||','||ColC||','||ColD||','||ColE from my_view;
Still out of curiosity, I'd like to know if there's any other way of achieving the same.
Also using the above query messes up the header.

Export to Excel/csv by SQL query statement regularly on Windows?

I use Oracle and I would like to export some data to Excel/csv by SQL query statement regularly on Windows, SQL query statement as below:
SELECT A.e,
a.f,
a.g,
b.h
FROM A
JOIN C ON C.e=A.e
JOIN B ON C.j=B.j;
Thanks so much for any advice.
Example for Windows test_csv.bat
sqlplus -s user/password#net_alias #csv.sql
csv.sql
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ';'
spool test.csv
SELECT A.e,
a.f,
a.g,
b.h
FROM A
JOIN C ON C.e=A.e
JOIN B ON C.j=B.j;
spool off
exit;

Exporting sql request to csv instead of table data

I need to export data from my Oracle table to a csv file.
Below is the code that I am running under SQL Developer :
set termout off
set serveroutput off
set feedback off
set colsep ';'
set lines 100000
set pagesize 0
set echo off
set feedback off
spool D:\myfile.csv
select *
from Employee;
spool off
However the output of the above code in the csv file is :
select *
from Employee;
I want the data of the Employee table to be in the csv, not the sql statement.
Any idea what might be wrong in the above code? Thanks.
save your sql in a file emp.sql in a directory D:\scripts and use like below;
set term off
set feed off
spool D:\myfile.csv
#D:\scripts\emp.sql
spool off
You're in SQL Developer, so you don't have to write so much code.
SET SQLFORMAT csv
SPOOL C:\your_file.csv
select * from whatever;
spool off
Run with F5

"Unknown set option" in SQL developer which creating Spool script

My code is as below-
set define on;
set spool on;
set SQLFORMAT csv;
set fpath = &folderpath;
spool #fpath/mycsvfile1.csv
select * from I_PTY;
spool off;
spool #fpath/mycsvfile2.csv
select * from I_LEG;
spool off;
I want to pass folderpath value as 'C:\Documents\Spool'.
And use this value to pass to the variable fpath, so that the value passed in line5 equals to 'C:\Documents\Spool\mycsvfile1.csv'
However, I am getting error as: Unknown set option "fpath".
Where am I getting wrong? Kindly assist.
You could do this in two ways:
Just use &&folderpath in the spool commands directly, e.g.:
set spool on;
set sqlformat csv;
spool &&folderpath/mycsvfile1.csv
select * from i_pty;
spool off;
spool &&folderpath/mycsvfile2.csv
select * from i_leg;
spool off;
Or use DEFINE to assign the value (which you'd then have to reference in the same way as the previous answer, so you don't gain anything, really...):
set spool on;
set sqlformat csv;
define fpath = &folderpath
spool &&fpath/mycsvfile1.csv
select * from i_pty;
spool off;
spool &&fpath/mycsvfile2.csv
select * from i_leg;
spool off;
Using the double ampersand stops Oracle from prompting you for a value each time you reference the same substitution variable. If you want your next script or next run through of this script to be prompted again for the value of folderpath, then you will need to include undefine folderpath (and maybe undefine fpath) at the end of the script.
Also, you may want to include set verify off at the top of your script.
N.B. you were using # in your spool file name - that has no place there. It's only used when you're running a script.
that "set" should probably be a "define"
Try:
spool &folderpath./mycsvfile2.csv
Example from my code:
spool c:\temp\SQL_Details_Report_&FILE_NAME..html

sql oracle query data export

is it possible to export the data from a query using a script alone? i like to run the query and have the data exported automatically without the manual process of exporting the data into .cvs.
I've tried SPOOL but it doesn't export the fetched data, just the code itself.
Thanks
See below , replace table> with your table name and conditions> with your where conditions
set colsep '|'
set echo off
set feedback off
set linesize 1000
set pagesize 0
set sqlprompt ''
set trimspool on
set headsep off
spool output.dat
select '|', <table>.*, '|'
from <table>
where <conditions>
spool off