rows and columns truncating when spooling in sqlplus - oracle

*I am spooling some data from oracle database to CSV file using sqlplus, My resulting table has 44 columns and more than 7000 rows but when it spooling to csv it is displaying only 26 columns in excel(Index A to Z) also some rows are truncating. I want all columns should be print in a single line.
I have tried increasing 'linesize' but the maximum is 32767 so it's not working also I tried 'wrap on' but the columns after 26th(Z in Excel) index coming in next line.
SET echo off
set embedded on
SET linesize 32767
SET LONG 90000
SET LONGCHUNKSIZE 90000
SET wrap off
SET heading off
SET pagesize 1000;
SET feed off;
SET colsep ','
SET termout off;
set trimout on;
SET trimspool ON;
SELECT * FROM ix_web_user;
spool results.csv append;
SET newpage none;
/
spool off
I want all columns should print in a single line and rows should not be truncated.*

You can export several csv files with different columns, but one column in each file must be a unique row identifier.
For example, the first file contains 22 columns and the second file has 23 columns.
A simple script connects lines by identifier and it turns out a final file with 44 columns and a line length of more than 32768.
file1.csv
id,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20,col21,col22,col23,col24,col25,col26,col27,col28,col29,col30,col31,col32,col33,col34,col35,col36,col37,col38,col39,col40,col41,col42,col43
joe,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,2411111111111111111111111111111111111111111111111111111111111
sam,21,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,451111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
mary,31,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
file2.csv
id,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20,col21
joe,33311111111111111111111111111111111111111111111111111111111111111111111111111111111111,33311111111111111111111111111111111111111111111111111111111111111111111111111111111111
mary,5551111111111111111111111111111111111111111111111111111111111111111111111111111111111,5551111111111111111111111111111111111111111111111111111111111111111111111111111111111
sam,44411111111111111111111111111111111111111111111111111111111111111111111111111111111111,44411111111111111111111111111111111111111111111111111111111111111111111111111111111111
file merge.ps1
$file1=(import-csv file1.csv -header id,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,
col11,col12,col13,col14,col15,col16,col17,col18,col19,col20,
col21,col22,col23,col24,col25,col26,col27,col28,col29,col30,
col31,col32,col33,col34,col35,col36,col37,col38,col39,col40,
col41,col42,col43)[1..9999]
$file2=(import-csv file2.csv -header id,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,
col11,col12,col13,col14,col15,col16,col17,col18,col19,col20,
col21)[1..9999]
$file1|
%{
$id=$_.id
$m=$file2|?{$_.id -eq $id}
$_.col23=$m.col1
$_.col24=$m.col2
$_.col25=$m.col3
$_.col26=$m.col4
$_.col27=$m.col5
$_.col28=$m.col6
$_.col29=$m.col7
$_.col30=$m.col8
$_.col31=$m.col9
$_.col32=$m.col10
$_.col33=$m.col11
$_.col34=$m.col12
$_.col35=$m.col13
$_.col36=$m.col14
$_.col37=$m.col15
$_.col38=$m.col16
$_.col39=$m.col17
$_.col40=$m.col18
$_.col41=$m.col19
$_.col42=$m.col20
$_.col43=$m.col21
}
$file1|Export-Csv "file3.csv" -NoTypeInformation
output file3.csv
"id","col1","col2","col3","col4","col5","col6","col7","col8","col9","col10","col11","col12","col13","col14","col15","col16","col17","col18","col19","col20","col21","col22","col23","col24","col25","col26","col27","col28","col29","col30","col31","col32","col33","col34","col35","col36","col37","col38","col39","col40","col41","col42","col43"
"joe","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","2411111111111111111111111111111111111111111111111111111111111",,"33311111111111111111111111111111111111111111111111111111111111111111111111111111111111","33311111111111111111111111111111111111111111111111111111111111111111111111111111111111",,,,,,,,,,,,,,,,,,,
"sam","21","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","451111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",,"44411111111111111111111111111111111111111111111111111111111111111111111111111111111111","44411111111111111111111111111111111111111111111111111111111111111111111111111111111111",,,,,,,,,,,,,,,,,,,
"mary","31","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","30111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",,"5551111111111111111111111111111111111111111111111111111111111111111111111111111111111","5551111111111111111111111111111111111111111111111111111111111111111111111111111111111",,,,,,,,,,,,,,,,,,,

Related

Oracle SQL Spool Result file add spaces in columns

I am spooling a big result in a file but the spool add a lot of space between columns :
one line is more than 6850 characters (mostly empty space), one column take 500 characters, for instance the first one :
23053607 ;
Here is the query :
spool result.txt;
set pagesize 0
set linesize 8000
SET TRIMSPOOL ON
set heading off
set space 0
set trimout off
set termout off
set verify off
set echo off
set feed off
set trimspool on
-- set echo off;
-- set termout off;
set colsep ';'
set feedback off;
set pages 0;
spool result.txt;
select
trim(rec.Code) AS Code,
trim(A.label) AS Number,
trim(B.text) AS Syst
FROM record rec
join tableA A on A.rec_id = rec.rec_id
join tableB B on B.kla_id = rec.kla_id;
spool off;
exit;
In the database there is no space.
How to avoid any spaces ?
Simplest thing to do is to just concatenate the values. Remove colsep and try out:
SELECT rec.Code ||';'||
A.label ||';'||
B.text
FROM record rec
JOIN tableA A ON A.rec_id = rec.rec_id
JOIN tableB B ON B.kla_id = rec.kla_id;

Oracle SQL Spool Output Issues - Headers and Dashes

Good day, Stack Overflow folks. I have a question regarding some SQL code that I'm updated for some regulatory processes that my team has. When I run the following code, I get the export set up just fine, however, I am getting several header rows and several rows of nothing. The SQL code in question is below:
Set Heading On
Set Colsep '|'
Set NumFormat 999999999999.99
Set Echo Off
Spool 'X:\Cool\Drive\Space\Yo\Output.csv' Replace;
Select …
From …
Group By …
;
Spool Off;
The output looks something like this:
A| B| C|...
-|-------|------|...
with multiple instances of those rows repeating.
Does anyone out there know how to stop this from happening, and how I can trim the outputs so we don't have a bunch of white spaces before the actual data starts printing?
Thank you!
You need to add two things
SQL> set underline off
SQL> set pagesize 100
PAGESIZE says, how many rows to print before you print the header column names again. If you only want to see those once, set the pagesize larger than the number of rows.
Here's my query -
SQL> set heading on
SQL> set colsep '|'
SQL> set numformat 999999999999.99
SQL> select sum(salary), department_id
2 from employees
3 group by department_id
4 ;
And if I run that -
SUM(SALARY)| DEPARTMENT_ID
105970.33| 100.00
51214.47| 30.00
14380.48|
119020.33| 90.00
39014.85| 20.00
20532.81| 70.00
41680.87| 110.00
321867.32| 50.00
626338.39| 80.00
13355.08| 40.00
59187.52| 60.00
8228.13| 10.00
12 rows selected.

how to show data in CSV in different colums using plsql?

I have written sql to generate the output data in CSV format.I have used spool to generate in CSV.
SET LINESIZE 1000 TRIMSPOOL ON FEEDBACK OFF
SPOOL E:\oracle\extract\emp2.csv
SELECT emp_id,
emp_name
FROM offc.employee
ORDER BY emp_id;
SPOOL OFF
SET PAGESIZE 14
The output is coming in location.The output is generated correctly but when I see the CSV file ,then all the data are coming in same column A in CSV.
I want emp_id in column A and emp_name in columnB.Why they are coming in same column? What is the problem here?
Here, the issue is the space is considered as the separator for your column data and excel can not separate the column values which are space separated.
You can use colsep attribute along with other attributes as following:
set colsep , -- defines column data separator
set pagesize 14 -- defines size of the page. Keep it large so that header is not repeated
set trimspool on -- remove trailing blanks
set lines 1000 -- linesize should be more than sum of width of the all columns
set FEEDBACK OFF -- removes the comment at the end of the data
SPOOL E:\oracle\extract\emp2.csv
SELECT emp_id,
emp_name
FROM offc.employee
ORDER BY emp_id;
SPOOL OFF
Cheers!!
What is the problem here?
Excel expects a Comma Separated Values file to have values in columns separated by commas. Your query outputs two columns of data but doesn't include an explicit separator. So when Excel reads the file it doesn't find any commas so it renders a single column of data.
There are various ways of solving this. One is include your own explicit separator in the query:
SELECT emp_id
|| ', ' ||
emp_name
FROM offc.employee
ORDER BY emp_id;
Another is to use the SQL*Plus colsep command to format the output in the file. A third option is to use a tool like SQL Developer, whose Export feature handles all this for us.
Talking about my own experience: although Excel knows how to open a CSV file, it is kind of stupid and still puts everything into the first column. Therefore, I prefer creating a TXT file instead, using a column separator (so - yes, it basically is a comma-separated-file (or whichever separator you choose)).
For example:
SQL> set pagesize 100
SQL> set linesize 100
SQL> set colsep ";"
SQL>
SQL> spool emp.txt
SQL>
SQL> select * from emp;
EMPNO;ENAME ;JOB ; MGR;HIREDATE; SAL; COMM; DEPTNO
----------;----------;---------;----------;--------;----------;----------;----------
7369;SMITH ;CLERK ; 7902;17.12.80; 800; ; 20
7499;ALLEN ;SALESMAN ; 7698;20.02.81; 1600; 300; 30
7521;WARD ;SALESMAN ; 7698;22.02.81; 1250; 500; 30
7566;JONES ;MANAGER ; 7839;02.04.81; 2975; ; 20
7654;MARTIN ;SALESMAN ; 7698;28.09.81; 1250; 1400; 30
7698;BLAKE ;MANAGER ; 7839;01.05.81; 2850; ; 30
7782;CLARK ;MANAGER ; 7839;09.06.81; 2450; ; 10
7788;SCOTT ;ANALYST ; 7566;09.12.82; 3000; ; 20
7839;KING ;PRESIDENT; ;17.11.81; 5000; ; 10
7844;TURNER ;SALESMAN ; 7698;08.09.81; 1500; 0; 30
7876;ADAMS ;CLERK ; 7788;12.01.83; 1100; ; 20
7900;JAMES ;CLERK ; 7698;03.12.81; 950; ; 30
7902;FORD ;ANALYST ; 7566;03.12.81; 3000; ; 20
7934;MILLER ;CLERK ; 7782;23.01.82; 1300; ; 10
14 rows selected.
SQL> spool off;
Now, start Excel and go to Open; choose "All files" (i.e. not only Excel-type files) so that you'd see emp.txt listed. Excel then - in its "Text Import Wizard" - asks you which kind of a file it is (choose delimited):
set the separator (semi-colon in our example)
and - open the file:
Everything is now in its own column.

SQLPLUS - ORACLE 12 - Trim extra columns white space

I've a problem with a sqlplus spool (oracle 12c/18c).
I want trim extra white space in columns.
This is the expected result
01JHON BROWN 30RED
02MARIO ROSSI 25WHITE
this is my result
01 JHON BROWN 30 RED
02 MARIO ROSSI 25 WHITE
this is the sql code
SET ECHO OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET SERVEROUTPUT ON
SET HEADING OFF
SET PAGESIZE 0
SET LINESIZE 2000
SET SQLBLANKLINES ON
SET FEEDBACK OFF
SET TIME OFF
SET TIMING OFF
SET COLSEP ''
SET TRIMSPOOL OFF
SET TERMOUT OFF
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD';
ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,';
spool pippo.txt
SELECT TRIM(NUM), RPAD(NAME,12), TRIM(AGE), RPAD(COLOR,7)
FROM PLUTO;
spool off
exit
THX
That's just how column formatting works with spool - each row's values are padded to the full width of the columns. See this similar question to get an idea of your options.
If you don't want any spaces between column values, you'll generally have to concatenate them into a single column, e.g.
SELECT TRIM(NUM) || RPAD(NAME,12) || TRIM(AGE) || RPAD(COLOR,7)
FROM PLUTO;

Oracle spool issues

currently i use below code to spool csv file but i have find some line length more than 4000 characters , so it will no propey display
set echo off
set feedback off
set heading on
set linesize 32000
set pagesize 0
set termout off
set trim off
set trimspool on
set array 100
set underline off
set wrap off
set flush off
set verify off
set embedded on
select cloumnA||','||ColumnB ||','||ColumnC ..... from my_tab_name ;
so i modify my code but the result is not my want.
set echo off
set feedback off
set heading on
set linesize 32000
set pagesize 0
set termout off
set trim off
set trimspool on
set array 100
set underline off
set wrap off
set flush off
set verify off
set embedded on
select cloumnA,ColumnB ,ColumnC ..... from my_tab_name ;
the result 1:
A_value,B_value,C_value..... (the result is correct ,but only can show 4000 characters)
the result 2:
A_value
B_value
C_value
....
Anyone can help,thanks a lot!
When you concatenate columns (cloumnA||','||ColumnB ||','||ColumnC), the result is a VARCHAR2, which can't be more than 4000 characters (in typical circumstances).
See this similar question for some different options.
You could use COLSEP:
set colsep ','
spool myfile.csv
select cloumnA,ColumnB,ColumnC ..... from my_tab_name;
Which will get you something like
A_value ,B_value ,C_value
A_value ,B_value ,C_value
A_value ,B_value ,C_value
Which most people don't like, but it doesn't have a 4000-character line limit.
If you're using SQL Developer, you can use the easy /*csv*/ hint
select /*csv*/ cloumnA,ColumnB,ColumnC ..... from my_tab_name;
And if it's possible for you to use the newer SQLcl instead of SQL*Plus, you can use set sqlformat:
set sqlformat csv
spool myfile.csv
select cloumnA,ColumnB,ColumnC ..... from my_tab_name;

Resources