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

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;

Related

when using spool how to remove the blank spaces where the the column has null values. I am using Toad and Oracle 11g

I am trying to spool the data of a table into a CSV file.There are white spaces where the column has null values. how to remove that.
SET COLSEP ,
SET PAGESIZE 5000
SET WRAP OFF
SET HEADSEP OFF
SET UNDERLINE OFF
SET TRIMSPOOL ON
SET TRIMOUT ON
SET LINESIZE 5000
SET TERMOUT OFF
SET FEEDBACK OFF
SPOOL C:\oracle\emp1.csv
SELECT * FROM emp1
SPOOL OFF
Emp_No,Emp_name,Project_Id,Group_Id,Department,Department_Id
1000, AAA, F123, B768, , 68976
1001, BBB, F222, B897, , 98689
I recommend you use [SQLcl][1] to create a CSV file.
SET SQLFORMAT CSV
SPOOL file.csv
SELECT * FROM T;
SPOOL OFF
HOWTO Blog by the PM Manager of SQL*Developer ( thatJeffSmith-Oracle )
http://www.thatjeffsmith.com/archive/2015/02/a-quick-4-1-trick-set-sqlformat/ http://dbdude.net/oracle-sqlcl-use-sqlcl-to-simply-export-via-set-sqlformat-csv-and-import-via-load/

How to export/spool large table to file in Oracle

What are the convenient way to export/spool large table (43 million records) to single file in Oracle?
(Once we get the exported file, the purpose is to handle it in a Shell Script running in Solaris with kshell, but it is another history).
Our first try generated an empty file:
SET NEWPAGE 0;
SET LINESIZE 169;
SET PAGESIZE 0;
SET VERIFY OFF;
SET TERMOUT OFF;
SET COLSEP '';
SET FEEDBACK OFF;
SET HEADING OFF;
SPOOL THE_MONSTER_FILE.txt;
SELECT
a.field1, a.field2, b.field1, b.field2
FROM
tableA a, tableB b,
WHERE
a.id = b.id(+);
SPOOL OFF;
COMMIT;
EXIT;
You can export the result set into a CSV file. Create a shell script and let it running on background, like this:
#!/bin/bash
cat <<EOF > THE_MONSTER_SCRIPT.sql
SET COLSEP ;
SET HEADSEP OFF
SET VERIFY OFF
SET HEADING OFF
SET ECHO OFF
SET FEEDBACK OFF
SET LONG 2000000000
SET LINESIZE 1000
SET PAGESIZE 0
SET TRIMSPOOL ON
SPOOL THE_MONSTER_FILE.csv;
SELECT
a.field1, a.field2, b.field1, b.field2
FROM
tableA a, tableB b,
WHERE
a.id = b.id(+);
SPOOL OFF
EXIT
EOF
nohup sqlplus system/password#INSTNAME #THE_MONSTER_SCRIPT.sql &
If any of the columns on the result set have the column separator in its value, you have to enclose the columns with double quotes, replacing double quotes in the column value with two double quotes. Example:
/*
* ----------------
* | col1 | col2 |
* ----------------
* | a"a;a | aaa |
* | bbb;b | b"b |
* ----------------
*/
will turn into:
/*
* "a""a;a";"aaa"
* "bbb;b";"b""b"
*/
You can easily do with help of SSIS packages.
Check link Here.

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

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

Automatically export result sets into csv

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;