How do I spool to a CSV file using SQL Developer? - sql

i have this kind of script that i want to spool the final output to a csv file. can you please help?
with first_sub as
select etc
,
second_sub as
select etc
select first_column from first_sub* first_column from second_sub etc.
......................................
..basically, i have 2 or more sub queries that i do maths on in my 'final query'
what i need is to be able to spool the output as a csv.
sorry but im not able to post any specific code
ok, to clarify, i CAN ALREADY spool a 'Simple' query
i.e `select *from employees'
what i have is like this
with sub_1 as
select * from employees
,
sub_2 as
select * from other_employees
select something from sub1 * something_else from sub_2
The last bit is what i want to take out to a .csv file please

-- SQL developer
You can use /*csv*/ hint in SQL developer. As #Aleksej suggested, you can see the linked thread for further options.
select /*csv*/ * from employees;
--SQL Plus
set feedback off
set heading on
set underline off
set colsep ','
spool 'mytab.csv'
select * from tab;
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.

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

spool for insert in sql developer not working

I have below select query for which the result of this query i want to create insert scripts and saved it in files. I have used spool.
set long 10000
set lines 100000
set sqlformat insert
spool c:\temp\Insert_TEST_GRP.sql
select ID,NAME,TEST_DATE from TEST_GRP sd
where TEST_DATE =
( select min(TEST_DATE)
from TEST_GRP sd2
where sd.ID = sd2.ID
)
and sd.TEST_DATE <> TO_DATE ('01.01.2000', 'dd.mm.yyyy');
spool off
The file has been created. But when i view the file i am getting the result which is not in the form of insert statements as i want to run this insert statement again.
Below hows the data looks like in file which looks in incorrect format:
We don't have access to your table or your data.
But here it is working with the demo schema HR and its EMPLOYEES table
set sqlformat insert
spool c:\users\jdsmith\desktop\SO_inserts.sql
select * from employees;
spool off
You're using SET LONG - does your table have LOBS in it?
Also, I noticed you asked this same question on the OTN Forums...
The set sqlformat method to format your query results was added in version 4.1.
If you're on an earlier version (e.g. 3.0 as you said in a comment) then it would complain, which you seem to have overlooked; e.g. in 4.0:
set sqlformat insert
gets this in the script output window:
line 1: SQLPLUS Command Skipped: set sqlformat insert
The /*insert*/ method was available earlier than that:
select /*insert*/ * from dual;
which gets
REM INSERTING into dual
SET DEFINE OFF;
Insert into "dual" (DUMMY) values ('X');
(don't really attempt to insert into dual, of course). You can also use the export wizard (tools->database export); or run your query with control-enter, right-click on the output grid and choose 'export' (though it may repeat the query).
Upgrading to the current version is the sensible thing to do though.
You need to return a string that is the INSERT statement formatted with the columns you need. Example
set long 10000
set lines 100000
set sqlformat insert
spool c:\temp\Insert_TEST_GRP.sql
select 'INSERT INTO TEST_GRP (ID,NAME,TEST_DATE) VALUES (' ||
ID||','||NAME||',' || TEST_DATE||');'
from TEST_GRP sd
where TEST_DATE =
( select min(TEST_DATE)
from TEST_GRP sd2
where sd.ID = sd2.ID
)
and sd.TEST_DATE <> TO_DATE ('01.01.2000', 'dd.mm.yyyy');
spool off
If you are using sqldeveloper, then you can just use the built-in export function and export the result grid as inserts.
I used SQL Developer. Make sure to click Run script(F5) instead of run statement.
For multi statement in sample file use "/" between statement.
SET FEEDBACK OFF
set sqlformat insert
spool C:\Necessary_file\Reqular_task\QDE\profile_const.sql
select * from export_profile where profile_name='SPS_DIAG';
/
select * from profile_const where profile_name='SPS_DIAG';
/
select * from profile_panel where profile_name='SPS_DIAG' order by 5
/
spool off

Oracle Spool returns query script and no results

I'm running the following script to pull some results to a text table. When I run the script, it returns the script to the text file and no query results. Any idea as to why?
set feedback off
set heading off
set echo off
set define off
set linesize 500
spool \\1.1.1.1\w$\Customer_Service\Outgoing\Missing_PO_NN.txt
select p.po
from sv_order_check_nn p
where not exists (
select 1
from ordusctes o
where o.usctes_po = p.po);
/
SPOOL OFF
When I run the script, it returns the script to the text file and no query results. Any idea as to why?
Depends whether your query actually returns any rows. Did you first execute your query and check if it actually returns any rows?
select p.po
from sv_order_check_nn p
where not exists (
select 1
from ordusctes o
where o.usctes_po = p.po);
/
You are executing the query twice. You are using slash / as terminator in the end, which is going to execute whatever is in the buffer.
See this answer for similar problem and fix.
In your case, your query will be executed twice.

export oracle table in a csv file [duplicate]

This question already has answers here:
How do I spool to a CSV formatted file using SQLPLUS?
(16 answers)
Closed 5 years ago.
I am writting an sql script to export the content of tables in csv files (one table per file). I managed to write a successful script, thanks to the spool function like this :
spool export.csv append
select 'fielda;fieldb;...' from dual
select fielda,fieldb,.... from table
spool off.
The first problem with this, is that I have to do a select from dual to get only the fields name on the first line.
The second problem with this, is that I have to write each field, and it becomes very painfull when you have 10 tables each having more than 20 fields. So my question was, is there any pl sql function, that takes in parameter only the table name, and export the full content in a csv file.
Thanks in advance.
Below might work for you
set termout off
set serveroutput off
set feedback off
set colsep ';'
set lines 100000
set pagesize 0
set echo off
set feedback off
spool on
spool D:\csv_generator_tmp.sql
select qr from
(select 'select '||a.column_name||'||'';''||' qr,a.COLUMN_ID
from user_tab_cols a
where a.table_name = upper('cust')
and a.column_id=1
union
select b.column_name||'||'';''||',b.COLUMN_ID
from user_tab_cols b
where b.table_name = upper('cust') and b.column_id<>1
and b.column_id<>(select max(c.column_id) from user_tab_cols c)
union
select d.column_name||' from cust;',d.COLUMN_ID
from user_tab_cols d
where d.table_name = upper('cust')
and d.column_id=(select max(d.column_id) from user_tab_cols d))
order by column_id asc;
spool off;
spool on
spool D:\cust.csv
#D:\csv_generator_tmp.sql
spool off;
/
No but you can use user_tables and user_tab_cols to select the columns for tables of interest:
select utc.table_name, utc.column_name
from user_tab_cols utc
where utc.table_name = 'CIRCUIT'
You could manage this through a cursor and generate your select columns. You can then execute this query.
Its better to go with UTL_FILE. I would refer to user the Ask tom link https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::p11_question_id:88212348059
with that you can create the file regularly by calling the function.
You can use Python and cx_Oracle module to extract data to disk in CSV format.
Here’s how you connect to Oracle using cx_Oracle:
constr='scott/tiger#localhost:1521/ORCL12'
con = cx_Oracle.connect(constr)
cur = con.cursor()
After data fetch you can loop through Python list and save data in CSV format.
for i, chunk in enumerate(chunks(cur)):
f_out.write('\n'.join([column_delimiter.join(row[0]) for row in chunk]))
f_out.write('\n')
I used this approach when I wrote TableHunter-For-Oracle