Oracle set multiple system variables - oracle

Is it possible to set multiple system variables in a single command?
For e.g. set the below system variables in a single line?
SET COLSEP ' '
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING OFF
SET LINESIZE 10000
SET NEWPAGE NONE
SET TRIMSPOOL ON
Checked http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm#i2699283 but don't see an example of doing so.

Is it possible to set multiple system variables in a single command?
Yes you can. Simply specify variable name/value pairs using space as a delimiter between previous variable's new value and the next variable's name. Here is an example:
/* display variables' current values */
SQL> show colsep echo feedback heading linesize
colsep " "
echo OFF
FEEDBACK ON for 6 or more rows
heading ON
linesize 80
/* Assign new values */
SQL> set colsep "|" echo on echo on feedback off heading off linesize 1000;
/* Make sure changes are in effect */
SQL> show colsep echo feedback heading linesize
colsep "|"
echo ON
feedback OFF
heading OFF
linesize 1000
SQL> spool off;
Note: When number of system variables are more than a few, this approach will drastically reduce readability. So it would be better to dedicate each variable/value pair its own SET command.

Related

Export data as XSV from oracle

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

Heavy file generated on spooling in unix

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;

How to export half a million records from PL/SQL

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

Prevent Spool to add SQL query in output file

I want my script to stop printing the SQL query in the output file.
I have tried different method, but its just not happening.
sqlplus user/password#(TNS Entry) << EOF
SET head OFF;
SET feed OFF;
SET trimspool ON;
SET linesize 32767;
SET pagesize 32767;
SET echo OFF;
SET termout OFF;
SET verify OFF;
SET NEWPAGE NONE;
SET verify off;
#test.txt
spool file_name.csv
select * from Customer;
spool off
EXIT;
EOF
Could you help please , I want csv file to have just the result of the SQL query and nothing else.
Put your commands in a file. Then run that from SQLPUS.
So for instance make a file query.sql.
SQL>#query.sql
Now SQL won't put the query in the spoolfile.
This way SQLPLUS will listen to your set .. off commands. See the documentation.
*SET ECHO {ON | OFF}
Controls whether or not to echo commands in a script that is executed
with #, ## or START. ON displays the commands on screen. OFF
suppresses the display. ECHO does not affect the display of commands
you enter interactively or redirect to SQLPlus from the operating
system.**

blanks lines in between data rows show up when using SPOOL in SQLPlus

I'm trying to spool a large query in sqlplus but for some reason, I see 2 blank lines every 558 rows to be exact.
I spool as such:
SET TERMOUT OFF
SET ECHO OFF
SET LINES 1000
SET FEEDBACK off
SET HEADING OFF
SET ARRAYSIZE 10000
SET NEWPAGE NONE
SET PAGESIZE 0
SET TRIMSPOOL ON
Spool D:\IPORT15.csv
select query
Spool OFF;
EXIT
I thought SET NEWPAGE was supposed to take care of that??
I was able to resolve this by setting: "SET PAGES 0"

Resources