Heavy file generated on spooling in unix - oracle

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;

Related

How to spool explain results from Oracle via sqlplus?

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'));

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

Remove Column Header into the Output Text file

I want to create a flat file (text file) of my query from Oracle SQL Developer.
I have successfully created the text file using SPOOL, thru a script text file, but i want to remove the header of each column into my output.
I am getting this output:
Header000001 Header000002
------------ ------------
Adetail1 Bdetail1
Adetail2 Bdetail2
Adetail3 Bdetail3
But, I want to get this output:
Adetail1Bdetail1
Adetail2Bdetail2
Adetail3Bdetail3
I already tried the command "set heading off", but a message says:
"SQLPLUS COMMAND Skipped: set heading off".
These are the inputs I've issued:
spool on;
spool C:\SQLFiles\PSB_ATMLKP.txt;
set newpage 0;
set echo off;
set feedback off;
set heading off;
select terminal_number, terminal_name from terminal_table;
spool off;
SQLPLUS COMMAND Skipped: set heading off
That message is most likely because you are not executing it through SQL*Plus, but some GUI based tool. You are using SQLPlus command in SQL Developer. Not all SQL*Plus commands are guaranteed to work with SQL Developer.
I would suggest you execute the script in SQLPlus and you would see no issues.
You need:
SET HEADING OFF
This will not include the column headers in the output.
Alternatively, you could also do this:
SET PAGESIZE 0
Using SQL Developer Version 3.2.20.10:
spool ON
spool D:\test.txt
SET heading OFF
SELECT ename FROM emp;
spool off
Spool file got created with no issues:
> set heading OFF
> SELECT ename FROM emp
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
14 rows selected
Add:
set underline off
to the beginning of the SQL script.
In my SQL scripts I have:
SET TERMOUT OFF
set colsep |
set pagesize 0
set trimspool on
set pagesize 0 embedded on
SET heading on
SET UNDERLINE OFF
spool file_path
-- your SQL here
spool off
See this book for reference.

Oracle set multiple system variables

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.

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