I want to export some data (i.e. some queries results') from an old oracle database into a file, values separated with a specified character or sequence of characters. A CSV file is an example.
Only integers, varchars (without newline included), dates, other basic values (?) into result set, so no LOBs or another fancy stuff. Just plain data, NO PADDING, NO WRAPPING, fields separated and maybe enclosed.
Practicaly, I'm looking for a simple "SELECT ... INTO OUTFILE FIELDS TERMINATED BY ... [OPTIONALLY] ENCLOSED BY 'char'" from mysql, but for Oracle.
I tried to use sqlplus, but any program should be ok, as long as it runs on linux console and it's not java based :-P.
Unfortunately I cannot use "SET SQLFORMAT csv", seems to be not supported.
So far I've got best results with something like:
printf "set echo off newpage 0 pagesize 0 arraysize 5000 feed off head off trimspool on trimout on\nselect field1 || '|' || field2 from table;" | sqlplus -S database > output_file
Set arraysize 5000 (maximum) gives the best performance, unfortunately wraps the result if lines are longer than 80 chars.
Set linesize 30000 (or even 32767) cancels wrapping, unfortunately the performance becomes horrible. I don't want to specify a maximum linesize for each query (calculating maximum size for each field in result), hoping that performance will rise.
After reading tons of answers I'm still not getting close to a solution that should be both CORRECT and PERFORMANT. My next call will be writing a php file to do the job, but it's a nonsense, IMHO using the default application sqlplus should give the best performances ...
Any ideas ?
I can't tell whether this set of SET commands will help (I use it, usually), but - try it.
set termout off
set trimspool on
set echo off
set verify off
set autoprint off
set serveroutput off
set arraysize 1000
set pagesize 0
set linesize 100
set long 10000
set numwidth 10
set feedback off
set colsep ';'
col empno format 99999
col ename format a10
col sal format 999G990
spool emp.txt
select empno, ename, sal from emp;
spool off
At the bottom, you can see some COL formattings; remove it if you don't need it.
Don't set linesize to that high number (30.000) if you don't really need it.
Also, there's the set colsep ';' which will separate columns by that separator (semi-colon in this example; can be any other character, such as | you mentioned).
In order to speed up spool:
set termout off
set trimspool on
run it on server, not on the client PC
Ok, for everyone that had this problem, here is the answer:
Make sure you have at least oracle client 12.2 installed.
If not, you can extract the files from instantclient-basic-linux.x64-12.2.0.1.0.zip (library files) and from jar archives from sqlplus related directories from linuxx64_12201_client.zip (client/stage/Components/oracle.sqlplus*). Those two zips are available to download for free (registration required) at oracle download site. I didn't want to install that version of the client because it could mixed up with local installed one. It's kind of an ugly hack, but at least no file on system was modified.
From 12.2 onwards the "set markup csv" command is supported, and the command was:
printf "set head off\nset feedback off\nset arraysize 5000\nselect field1, field2 from table;" | LD_LIBRARY_PATH=/path/to/oracle12/lib ORACLE_HOME=/path/to/oracle12 /path/to/oracle12/bin/sqlplus -M "CSV ON" -S database
adjust the arraysize variable (5000 is maximum and fastest, but pay attention to the memory), choose the "SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]" parameters and enjoy
Related
Is this the right way to fetch request id in shell script
v_request_id=`sqlplus -s /nolog <<END
connect $FCP_LOGIN
set echo off
set head off
set verify off
set feedback off
set trimspool on
SELECT FND_GLOBAL.CONC_REQUEST_ID
FROM DUAL
END`
Not entirely.
1.) put a semicolon after dual
2.) why use the old ugly backticks?
3.) make it more compact: put all the set options in one line:
SET echo off head off feed off verify off trimspool on
4.) Why break the SELECT into several lines?
5.) You will get more than one line as result, because sqlplus always creates an empty header line, so you have to get rid of that. Maybe by using tail -1
6.) If your result is numerical, you are in for a surprise: sqlplus will pad it with spaces. Best way: convert it into a string with TO_CHAR
So the result could be this here:
v_request_id=$(sqlplus -s /nolog <<END | tail -1
connect $FCP_LOGIN
set echo off head off verify off feedback off trimspool on
SELECT TO_CHAR(FND_GLOBAL.CONC_REQUEST_ID) FROM DUAL;
END)
I'm having issues with spooling out results from queries, more or less it shows always no real cost, shows rows as one and no cost or anything added, and not the statistics of the actual query.
I'm using this setup:
current_date=$(date +%Y-%m-%d)
sqlplus -S "Username/password#mydatabase.bag" <<EOF >/output/Testoutput_$current_date.log
set verify off;
set colsep ,
SET AUTOTRACE ON
set headsep off
set pagesize 0
set trimspool on
set termout off
col v_spool noprint new_value v_spool
select 'Spoolfile'||
to_char(sysdate,'yyyy_mm_dd_hh24_mi_ss')||'.csv' v_spool from dual;
set termout on
spool /folder/subfolder/&&v_spool
set lines 12345 pages 12345;
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
SET TIMING OFF
spool off
EXIT
EOF
My wish is to somehow get the explain results spooled to a file, what do I need to modify to achieve this or there is a different approach?
If you try running this in SQL*Plus:
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
You'll notice that it just outputs:
Explained.
You have to actually select the results of the EXPLAIN PLAN using DBMS_XPLAN - that will get output and spooled to your file.
-- generate the explain plan
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
-- actually display the results
select plan_table_output
from table(dbms_xplan.display('plan_table',null,'typical'));
Hello i want to connect to following dbs in loop and execute statements on each:
conn support/support#sp0666to
conn support/support#sp0667to
conn support/support#sp0668to
Is there any way to do this in sqlplus?
Thank you for your answers in advance!
Create one script (doWork.sql) that contains the majority of what you want to do:
conn &1/&2#&3
select EMPLOYEE, AUTHORIZED, TIME, DAT, WORKSTATION
from EMPLOYEE
where status = 25;
In a separate script (goToWork.sql):
set lines 1500 pages 10000
set colsep ';'
set sqlprompt ''
set heading on
set headsep off
set newpage none column tm new_value file_time noprint
select to_char(sysdate, 'DDMMYYYY_HH24.MI') tm from dual;
accept user
accept pass
spool C:\Users\NANCHEV\Desktop\parked.csv
##doWork &user &pass sp0666to
##doWork &user &pass sp0667to
##doWork &user &pass sp0668to
spool off;
exit
If you want separate files, then move the two spool commands to the doWork.sql file.
Assuming you want to run the same set of queries for each database, I'd create a script file (e.g. main_statements.sql) containing those statements.
Then, if the list of databases was static, I'd create a second script file (e.g. run_me.sql) in the same directory, with contents along the lines of:
connect &&user/&&password#db1
##main_statements.sql
connect &&user/&&password#db2
##main_statements.sql
connect &&user/&&password#db3
##main_statements.sql
...
If, however, the databases are static but the list is contained in a database somewhere, then I'd write a script (e.g. run_me.sql) that generates a script, something like:
set echo off
set feedback off
set verify off
spool databases_to_run_through.sql
select 'connect '||username||'/'||password||'#'||database_name||chr(10)||
'##main_statements.sql'
from list_of_databases_to_query;
spool off;
##databases.run_through.sql
N.B. untested. Also, I have assumed that your table contains the usernames and passwords for each db that needs to be connected to; if that's not the case, you'll have to work out how to handle them; maybe they're all the same (in which case, you can hardcode them - or better yet, use substitution variables (e.g. &&username) to avoid having to store them in a plain file. You'd then have to enter them at runtime.
You'll also need to run the script from the same directory, otherwise you could end up with the generated script not being created in the same directory as your main_statements.sql equivalent script.
Yes it's possible, you can use oracle DBLink to connect to different dbs just like your example.
I run this code from a unix file.
Please Find Below my code
set colsep ,
set heading off
set pagesize 0
set feedback off
set linesize 32767
set trimspool on
set tab off
spool My_File
select * from my_table; /* it has 45 records*/
spool off;
Once I run this code, I have my output file generated, but the columns are seperated with very big spaces and even the records are seperated with big spaces. As a result I get a very huge file.
Is there any workaround for this ... Thanks in advance..
What i do in these situations is not to use colsep and and do the concatenation myself.
set heading off
set pagesize 0
set feedback off
set linesize 32767
set trimspool on
set tab off
spool My_File
select column1||','||column2||','||column3.... from my_table;
spool off;
I've a table with around 500,000 records. I need all records to be exported in excel. When I query I'm not able to get all as I was said as Out Of memory
Table doesn't have any primary key/Index.
Is there any way to extract?
it would be very easy in to write file output form sqlplus .
mycsv.sql:
SET DEFINE OFF
SET ECHO OFF
SET SERVEROUTPUT OFF
SET TERMOUT OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET PAGESIZE 10000
SET ARRAYSIZE 5000
REM SET HEAD OFF
SET LINE 500
spool /tmp/mycsvfile.csv;
select * from MY_table;
spool off;
exit;
and from Linux prompt you can run like
$> sqlplus username/password #/tmp/mycsv.sql