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;
Related
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;
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.
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;
I have scheduled a shell script which is outputting the following email. I want to remove the unwanted/additional duplicated text which is being printed using the dual table below to give a heading to my SQL select output.
How can I do it?
Following is the spool SQL script. After the script execution is done (/home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.out) file is added to the email body and sent to recipients.
Can you guide which set parameter command is missing OR which one is extra or unnecessary below?
This is shell script file code.
sqlplus user/pass#pgwdb #/home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.sql
sleep 5
/bin/mail -s "MFS PGW Fraud Alerts (Day)" -r “test-email-1#email-domain.com” "test-email-2#email-domain.com" < /home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.out
PGW_ALERTS.sql Code
set echo off;
set feedback off ;
set verify off ;
set serveroutput off;
set heading on;
set trimspool on;
set headsep off;
set PAGESIZE 60;
set LINESIZE 400;
SET WRAP OFF;
SET COLSEP ' ';
set numw 20;
SPOOL /home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.out append;
select '====== Transactions with count > 2 from same MSISDN ======' from dual;
SELECT '"'
|| MOBILENUMBER
|| '"' MSISDN,
AMOUNT/100 AMOUNT,
DECODE(TXNTYPEID,'6','MW', '0') TXN_TYPE,
MERCHANTCODE,
COUNT (*) COUNT
FROM TBL_TXN_LOG
WHERE TXNTYPEID ='6'
AND AMOUNT <=10000
AND TXNDATETIME>= SYSDATE -1
AND STATUS ='xxxx'
GROUP BY '"'
|| MOBILENUMBER
|| '"', AMOUNT/100, DECODE(TXNTYPEID,'6','MW', '0'), MERCHANTCODE
HAVING COUNT (*) > 2
UNION ALL
SELECT '"'
|| MOBILENUMBER
|| '"' MSISDN ,
AMOUNT/100 AMOUNT,
DECODE(TXNTYPEID,'164', 'Card', '0') TXN_TYPE,
MERCHANTCODE,
COUNT (*) COUNT
FROM TBL_TXN_LOG
WHERE TXNTYPEID ='164'
AND AMOUNT >=2500000
AND TXNDATETIME>= SYSDATE - 1
AND STATUS ='xxxx'
GROUP BY MOBILENUMBER,
AMOUNT,
DECODE(TXNTYPEID,'164','Card', '0') ,
MERCHANTCODE
HAVING COUNT (*) > 1
;
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
quit
The email which recipient is getting.,
-----Original Message----- From: destination.email#emaildomain.com Sent: Friday, December 8, 2017 7:36 PM To:
destination.email#emaildomain.com Subject: MFS PGW Fraud Alerts (4
Hourly)
***> '======TRANSACTIONSWITHAMOUNT<100FORMW&>25000======'
--------------------------------------------------------------***
====== Transactions with Amount < 100 for MW & > 25000 ======
MSISDN AMOUNT TXN_ MERCHANTCODE
COUNT
---------------------- -------------------- ---- ------------------------------ -------------------- "924008482888" 70 MW 00342386 1
"924008345433" 20 MW 002218387
1
I want to remove these extra lines
'======TRANSACTIONSWITHAMOUNT<100FORMW&>25000======'
Change your query a bit, use an alias for your heading, and turn off/on heading as shown:
set heading off
select '====== Transactions with count > 2 from same MSISDN ======' as info from dual;
select '<pre>' from dual;
set heading on
-- rest of your query
set heading off
select '</pre>' from dual;
That should do the trick if the message is being sent in HTML format.