export oracle table in a csv file [duplicate] - sql

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

Related

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

how to copy multiple table form server to another server in Oracle SQL Developer

So what Im trying to accomplish here is this:
SELECT DISTINCT COUNT(*) OBJECT NAME
FROM DBA_OJECTS#M310
WHERE OBJECT_TYPE = 'TABLE'
AND OWNER ='MSUSER'
=======> 6638
SELECT DISTINCT COUNT(*) OBJECT NAME
FROM DBA_OJECTS#M311
WHERE OBJECT_TYPE = 'TABLE'
AND OWNER ='MSUSER'
=======> 3833
So there is a difference of 2805 tables that are in M310 that are not in M311 server. I can find out what those tables are by using MINUS function. Without creating all the 2805 tables manually (one by one) how would I go about in doing this.
You can create a sqlplus script something like this:
set pagesize 0
set linesize 32767
set trimspool on
set timing off
set feedback off
set heading off
spool create_tables.sql
select 'create table '||object_name|| ' as select * from '||object_name||'#M310;'
from
( select object_name from dba_objects#m310
where object_type = 'TABLE' and owner = 'MSUSER'
minus
select object_name from dba_objects#m311
where object_type = 'TABLE' and owner = 'MSUSER'
)
;
spool off
This will spool a file called 'create_tables.sql' which you can run to create your missing tables. Note that this will populate them too. If you do not want them populated, then add a "where 1=2", such as...
select 'create table '||object_name|| ' as select * from '||object_name||'#M310 where 1=2;'

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

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

How to dump whole csv database to format readable by Excel (csv is OK).

This may be a futile excersise, but one of my client totally insisted that he needs whole database dump to perform analytics in Excel.
There are many answers to how to dump single table to csv (like this: Export to CSV and Compress with GZIP in postgres, Save PL/pgSQL output from PostgreSQL to a CSV file, Export Postgres table to CSV file with headings). There is even a closed question on this subject: https://stackoverflow.com/questions/9226229/how-to-take-whole-database-dump-in-csv-format-for-postgres. But there are no answers on how to dump whole database in single command.
Anyways, here is my script:
DO $DO$
DECLARE
r record;
BEGIN
FOR r IN select tablename from pg_tables where NOT (tablename LIKE 'pg%' OR tablename LIKE 'sql%') LOOP
EXECUTE 'copy (select * from "'|| r.tablename || '" ) to ''/tmp/dump/' || r.tablename || '.csv'' with csv header';
END LOOP;
END;
$DO$;
Some fine points:
It can be pasted into psql command and it will dump all tables in current schema to /tmp/dump directory (please create this directory first)
Query in the for loop (that is: select tablename from pg_tables where NOT (tablename LIKE 'pg%' OR tablename LIKE 'sql%') select all table names in current schema except for ones starting with pg and sql that will most likely be reserved names for postgres and SQL stuff. There most probably is a better way, but hell, who cares?