How to set headers in SQLPLUS? - oracle

I have set following SQLPLUS commands:
SET ECHO OFF
SET FEEDBACK OFF
SET HEADING ON
SET LINESIZE 100
SET PAGESIZE 1000
SET SPACE 0
SET TERMOUT OFF
SET TRIMOUT OFF
SET TRIMSPOOL ON
SET VERIFY OFF
But i am getting the result as:
ERROR_CODE
----------
ERROR_DESC
----------------------------------------------------------------------------------------------------
ERROR_COUNT
-----------
EXCP098
EXCEPTION: Processing not allowed
2
EXCP014
EXCEPTION: UNKNOWN STATUS
11
i.e. all the column headings and column values are in a new line instead of all column header should be in the same row and then record 1 should be in the first row and then record 2 should be in the second row.
I want it should be displayed as:
ERROR_CODE ERROR_DESC ERROR_COUNT
-----------------------------------------------------------------------
EXCP098 EXCEPTION: Processing not allowed 2
EXCP014 EXCEPTION: UNKNOWN STATUS 11

If "ERROR_DESC" is too long to fit on a line (together with "ERROR_CODE" and "ERROR_COUNT"), you have a few options to try:
return just a substring,
TRIM the value, or
change the data type for "ERROR_DESC".
What's working and appropriate, depends on your overall context. After all, the display in SQLPlus is usually not the most important aspect.

Related

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.

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;

rows and columns truncating when spooling in sqlplus

*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",,,,,,,,,,,,,,,,,,,

Why do C-style comments make insert statement run twice?

To make a long story short, I started getting ORA-00001 primary key violations and I tracked down the issue to the fact that some of my INSERT INTO statements were running twice. I then discovered that the offending commands had a C-style comment afterwards:
WHENEVER SQLERROR EXIT FAILURE
SET ECHO OFF
SET HEADING OFF
SET PAGESIZE 0
SET FEEDBACK OFF
SET TIMING OFF
SET TIME OFF
SET TRIMSPOOL ON
SET TRIMOUT ON
SET LINESIZE 120
SET SQLBLANKLINES ON
SET SERVEROUTPUT ON
[...]
INSERT INTO INF_FIELD (FIELD_ID, CATEGORY_ID, COLUMN_EXPRESSION, DISPLAY_NAME, SORT_ORDER) VALUES (17, 1, 'FOO.NAME', 'Name of the foo', 17);
/*This is a comment*/
It was then easily fixed by switching to this syntax:
--This is a comment
What's the exact reason why /*...*/ comments were making SQL*Plus run the statement twice?
/* This is a comment */
Just make sure you have a space after /* ,
So it is treated as a single/multi line comment. And not mean to execute the last stored PL/SQL or SQL
To put it in detail.
What ever SQL*Plus interprets after / is ignored and it blindly pushes it's cached block into the server. Except for /* followed by a new line or space.
SQL> SELECT * FROM DUAL;
D
-
X
SQL> /*t*/
D
-
X
SQL> /*
SQL> */
SQL> /
D
-
X
SQL> /*s
D
-
X
From Document:
You must enter a space after the slash-asterisk(/*) beginning a
comment.

Resources