Oracle SQL Spool Output Issues - Headers and Dashes - oracle

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.

Related

SQL*Plus Custom Summary Report

I have a requirement to generate the following summary report that look like the following:
My problem is that,
I have no idea on how do I fill the count data in this custom report.
I do not know how to put it in a table view like the above in a text document. It is not HTML.
So far, I only know how to do the first row, and column without the table view.
Here are my codes.
SET HEADING OFF;
SET LINESIZE 200;
SET SPACE 0;
SET ECHO OFF;
SET FEEDBACK OFF;
SET VERIFY OFF;
SET MARKUP HTML OFF SPOOL OFF;
SET TERMOUT OFF; --Do not show output to the screen.
SET NEWPAGE NONE; --Remove the first blank line at the top of the page and between queries.
TTITLE LEFT "REPORT NAME" RIGHT "PAGE : " SQL.PNO SKIP 1 -
LEFT "--------------------------------" RIGHT "DATE : " _DATE SKIP 1 -
LEFT "A) TOTAL RECORDS " RIGHT total_records; -- Cannot output variable in the title.
LEFT "B) MATCHED RECORDS " RIGHT matched_records; -- Cannot output variable in the title.
LEFT "C) UNMATCHED RECORDS " RIGHT matched_records; -- Cannot output variable in the title.
BTITLE LEFT "E N D";
total_records is an insert into statement.
SELECT COUNT(*) INTO total_records FROM TABLE;
I have not done matched records and unmatched records. But the only way I can think of
Select a statement into a cursor.
Loop into the cursor.
Increase matched count when there is a match.
Once loop finish. unmatched count = total count - matched count.
I don't think this is the most efficient way. But, if you have a better way, let me know.
Does something like this ring a bell? Example is based on Scott's sample schema:
SQL> select 'Total records' name, count(*) cnt
2 from emp
3 union all
4 select 'Matched count', sum(case when deptno = 10 then 1 else 0 end)
5 from emp
6 union all
7 select 'Unmatched count', sum(case when deptno = 10 then 0 else 1 end)
8 from emp;
NAME CNT
--------------- ----------
Total records 14
Matched count 3
Unmatched count 11
SQL>

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;

Header formatting while spooling a csv file in sqlplus

I am required to spool a csv from a table in Oracle, using sqlplus. Following is the format required:
"HOST_SITE_TX_ID","SITE_ID","SITETX_TX_ID","SITETX_HELP_ID"
"664436565","16","2195301","0"
"664700792","52","1099970","0"
Following is the relevant piece of the shell script I wrote:
sqlplus -s $sql_user/$sql_password#$sid << eof >> /dev/null
set feedback off
set term off
set linesize 1500
set pagesize 11000
--set colsep ,
--set colsep '","'
set trimspool on
set underline off
set heading on
--set headsep $
set newpage none
spool "$folder$filename$ext"
select '"'||PCL_CARRIER_NAME||'","'||SITETX_EQUIP_ID||'","'||SITETX_SITE_STAT||'","'||SITETX_CREATE_DATE||'","'||ADVTX_VEH_WT||'"'
from cvo_admin.MISSING_HOST_SITE_TX_IDS;
spool off
(I have used some commented statements in, to signify the things that I tried but couldn't get to work)
The output I receive is:
'"'||PCL_CARRIER_NAME||'","'||SITETX_EQUIP_ID||'","'||SITETX_SITE_STAT||'","'||SITETX_CREATE_DATE||'","'||ADVTX_VEH_WT||'"'
"TRANSPORT INC","113","00000000","25-JAN-13 10.17.51 AM",""
"TRANSPORT INC","1905","00000000","25-JAN-13 05.06.44 PM","0"
Which shows that the header is messed up - it is literally printing the whole string that should have been interpreted as an sql statement, as is the case with the data displayed.
Options I am considering:
1) Using colsep
set colsep '","'
spool
select * from TABLE
spool off
This introduces other problems as the data having leading and trailing spaces, first and the last values in the files are not enclosed by quotes
HOST_SITE_TX_ID"," SITE_ID"
" 12345"," 16"
" 12345"," 21
I concluded that this method gives me more heartburn than the one I described earlier.
2) Getting the file and use a regex to modify the header.
3) Leaving the header altogether and manually adding a header string at the beginning of the file, using a script
Option 2 is more doable, but I was still interested in asking, if there might be a better way to format the header somehow, so it comes in a regular csv, (comma delimited, double quote bounded) format.
I am looking to do as less hard coding as possible - the table I am exporting has around 40 columns and I am currently running the script for around 4 million records - breaking them in a batch of around 10K each. I would really appreciate any suggestions, even totally different from my approach - I am a programmer in learning.
One easy way to have a csv with just one header is to do
set embedded on
set pagesize 0
set colsep '|'
set echo off
set feedback off
set linesize 1000
set trimspool on
set headsep off
the embedded is a hidden option but it is important to have JUST one header
This is how I created a header:
set heading off
/* header */
SELECT '"'||PCL_CARRIER_NAME||'","'||SITETX_EQUIP_ID||'","'||SITETX_SITE_STAT||'","'||SITETX_CREATE_DATE||'","'||ADVTX_VEH_WT||'"'
FROM
(
SELECT 'PCL_CARRIER_NAME' AS PCL_CARRIER_NAME
, 'SITETX_EQUIP_ID' AS SITETX_EQUIP_ID
, 'SITETX_SITE_STAT' AS SITETX_SITE_STAT
, 'SITETX_CREATE_DATE' AS SITETX_CREATE_DATE
, 'ADVTX_VEH_WT' AS ADVTX_VEH_WT
FROM DUAL
)
UNION ALL
SELECT '"'||PCL_CARRIER_NAME||'","'||SITETX_EQUIP_ID||'","'||SITETX_SITE_STAT||'","'||SITETX_CREATE_DATE||'","'||ADVTX_VEH_WT||'"'
FROM
(
/* first row */
SELECT to_char(123) AS PCL_CARRIER_NAME
, to_char(sysdate, 'yyyy-mm-dd') AS SITETX_EQUIP_ID
, 'value3' AS SITETX_SITE_STAT
, 'value4' AS SITETX_CREATE_DATE
, 'value5' AS ADVTX_VEH_WT
FROM DUAL
UNION ALL
/* second row */
SELECT to_char(456) AS PCL_CARRIER_NAME
, to_char(sysdate-1, 'yyyy-mm-dd') AS SITETX_EQUIP_ID
, 'value3' AS SITETX_SITE_STAT
, 'value4' AS SITETX_CREATE_DATE
, 'value5' AS ADVTX_VEH_WT
FROM DUAL
) MISSING_HOST_SITE_TX_IDS;
This is how you add a pipe delimited header to SQL statements. Once you spool it out that "something" wont be there
-- this creates the header
select 'header_column1|header_column2|header_column3' as something
From dual
Union all
-- this is where you run the actual sql statement with pipes in it
select
rev.value1 ||'|'||
rev.value2 ||'|'||
'related_Rel' as something
from
...
In Oracle 19 you can use set markup csv on to ensure that csv outputs are created.
You can also set the delimiter and optional quote or even spool html if you prefer
You can read more here
set markup csv on
spool "$folder$filename$ext"
select q'|wow, I can't beleive he said "hello, how are you?", can you beleive it!|' as text
from dual;
spool off
quit;

Oracle - dynamic column name in select statement

Question:
Is it possible to have a column name in a select statement changed based on a value in it's result set?
For example, if a year value in a result set is less than 1950, name the column OldYear, otherwise name the column NewYear. The year value in the result set is guaranteed to be the same for all records.
I'm thinking this is impossible, but here was my failed attempt to test the idea:
select 1 as
(case
when 2 = 1 then "name1";
when 1 = 1 then "name2")
from dual;
You can't vary a column name per row of a result set. This is basic to relational databases. The names of columns are part of the table "header" and a name applies to the column under it for all rows.
Re comment: OK, maybe the OP Americus means that the result is known to be exactly one row. But regardless, SQL has no syntax to support a dynamic column alias. Column aliases must be constant in a query.
Even dynamic SQL doesn't help, because you'd have to run the query twice. Once to get the value, and a second time to re-run the query with a different column alias.
The "correct" way to do this in SQL is to have both columns, and have the column that is inappropriate be NULL, such as:
SELECT
CASE WHEN year < 1950 THEN year ELSE NULL END AS OldYear,
CASE WHEN year >= 1950 THEN year ELSE NULL END AS NewYear
FROM some_table_with_years;
There is no good reason to change the column name dynamically - it's analogous to the name of a variable in procedural code - it's just a label that you might refer to later in your code, so you don't want it to change at runtime.
I'm guessing what you're really after is a way to format the output (e.g. for printing in a report) differently depending on the data. In that case I would generate the heading text as a separate column in the query, e.g.:
SELECT 1 AS mydata
,case
when 2 = 1 then 'name1'
when 1 = 1 then 'name2'
end AS myheader
FROM dual;
Then the calling procedure would take the values returned for mydata and myheader and format them for output as required.
You will need something similar to this:
select 'select ' || CASE WHEN YEAR<1950 THEN 'OLDYEAR' ELSE 'NEWYEAR' END || ' FROM TABLE 1' from TABLE_WITH_DATA
This solution requires that you launch SQLPLUS and a .sql file from a .bat file or using some other method with the appropriate Oracle credentials. The .bat file can be kicked off manually, from a server scheduled task, Control-M job, etc...
Output is a .csv file. This also requires that you replace all commas in the output with some other character or risk column/data mismatch in the output.
The trick is that your column headers and data are selected in two different SELECT statements.
It isn't perfect, but it does work, and it's the closest to standard Oracle SQL that I've found for a dynamic column header outside of a development environment. We use this extensively to generate recurring daily/weekly/monthly reports to users without resorting to a GUI. Output is saved to a shared network drive directory/Sharepoint.
REM BEGIN runExtract1.bat file -----------------------------------------
sqlplus username/password#database #C:\DailyExtracts\Extract1.sql > C:\DailyExtracts\Extract1.log
exit
REM END runExtract1.bat file -------------------------------------------
REM BEGIN Extract1.sql file --------------------------------------------
set colsep ,
set pagesize 0
set trimspool on
set linesize 4000
column dt new_val X
select to_char(sysdate,'MON-YYYY') dt from dual;
spool c:\DailyExtracts\&X._Extract1.csv
select '&X-Project_id', 'datacolumn2-Project_Name', 'datacolumn3-Plant_id' from dual;
select
PROJ_ID
||','||
replace(PROJ_NAME,',',';')-- "Project Name"
||','||
PLANT_ID-- "Plant ID"
from PROJECTS
where ADDED_DATE >= TO_DATE('01-'||(select to_char(sysdate,'MON-YYYY') from dual));
spool off
exit
/
REM ------------------------------------------------------------------
CSV OUTPUT (opened in Excel and copy/pasted):
old 1: select '&X-Project_id' 'datacolumn2-Project_Name' 'datacolumn3-Plant_id' from dual
new 1: select 'MAR-2018-Project_id' 'datacolumn2-Project_Name' 'datacolumn3-Plant_id' from dual
MAR-2018-Project_id datacolumn2-Project_Name datacolumn3-Plant_id
31415 name1 1007
31415 name1 2032
32123 name2 3302
32123 name2 3384
32963 name3 2530
33629 name4 1161
34180 name5 1173
34180 name5 1205
...
...
etc...
135 rows selected.

Resources