When executing this code in SQL Plus:
set heading off
set colsep ';'
set feedback off
column descripcion format a50
set linesize 250
SPOOL lineas.txt
SELECT codigo, n_pedido, precio, calid1, calid2, fecha, cantidad, descripcion
FROM TABLA_PED
WHERE
SERIE = 'WEB'
AND venta = 25;
SPOOL OFF;
QUIT;
Works perfect but if there are asterisks in the "descripcion" it returns unexpected results.
Is there a way to avoid this?
Completely new in this, any help appreciated.
Have you tried cast it as nVarchar? Like:
set heading off
set colsep ';'
set feedback off
column descripcion format a50
set linesize 250
SPOOL lineas.txt
SELECT codigo, n_pedido, precio, calid1, calid2, fecha, cantidad, CAST (descripcion as nvarchar2(240)) as "descripcion"
FROM TABLA_PED
WHERE
SERIE = 'WEB'
AND venta = 25;
SPOOL OFF;
QUIT;
Thank you for your answers. It seems that the length "a50" was not enough for the descripcion field so the output did this empty fields thing.
Just changed to "a250" and fixed.
Related
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;
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.
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;
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;
In Oracle SQL Developer, I can get simple query results returned in the 'Query Results' grid, but if I need to use variable in script, I need to use the 'Run Script' option and my results show up in 'Script Output' window, and I can't export it to csv format. Here is my sample code:
var CatCode char(5) ;
exec :CatCode := 'ZK';
SELECT * FROM Products WHERE CategoryCode = :CatCode;
Any help would be appreciated.
Thanks.
Just add a /*csv*/ to your query, the tool will bring back the output in CSV automatically when executed as a script (F5).
Or use a substitution variable instead. &Var vs :Var, run with F9, SQLDev will prompt you for the value.
VAR stcode CHAR(2);
EXEC :stcode := 'NC';
SELECT /*csv*/
*
FROM
untappd
WHERE
venue_state =:stcode;
Or to go straight to the grid so you can use can use the Grid Export feature.
SELECT
*
FROM
untappd
WHERE
venue_state =:stcode2;
Execute with Ctrl+Enter or F9
Supply the input parameter in the pop up dialog, click OK.
Shazaam.
Here you go you can run this one to be ensure. it's running.
set colsep , -- separate columns with a comma
set pagesize 0 -- No header rows
set trimspool on -- remove trailing blanks
set headsep off -- this may or may not be useful...depends on your headings.
set linesize X -- X should be the sum of the column widths
set numw X -- X should be the length you want for numbers (avoid scientific notation on IDs)
spool C:\Users\**direcotory**\sql\Test1.csv; --this is file path to save data
var CatCode char(5) ;
exec :CatCode := 'ZK';
SELECT * FROM Products WHERE CategoryCode = :CatCode;
spool off;
Thanks #thatjeffsmith and Paras, spool option gave me new direction and it worked. I slightly changed your code and it works great.
var CatCode char(5) ;
exec :CatCode := 'ZK';
set feedback off;
SET SQLFORMAT csv;
spool "c:\temp\spoolTest.csv"
SELECT * FROM Products WHERE CategoryCode = :CatCode;
spool off;
SET SQLFORMAT;
set feedback on;