I am executing a sql-plus session to dump a table's data into a .csv file. I need header details along with the data in the dumped csv file. When I use the "set heading on" option, I am getting header details along with the data plus one empty line at the start. I tried multiple options like "set newpage NONE" and others, but first empty file is still coming. Can u pls help to solve this issue? My code snippet is mentioned at below
sqlplus -s /nolog << !EOF!
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
connect sys/${DB_PASSWORD}@//${CONN_STR}:${CONN_PORT}/XE as ${DB_USERNAME}
--/*************************/--
set echo off
set feedback off
set linesize 4000
set newpage NONE
set pagesize 0
set serveroutput off
set termout off
set flush off
SET NUMWIDTH 5
SET COLSEP ","
set markup csv on
set sqlprompt ''
set trimspool on
SET VERIFY OFF
SET TERM OFF
--/*******************************/--
--/**** Clear Screen & Buffer ****/--
--/*******************************/--
clear screen
clear buffer
--/*****************************/--
--/**** Start spooling file ****/--
--/*****************************/--
SPOOL $FILE
set heading on
set pagesize 0 embedded on
select * from $table_name;
SPOOL OFF
EXIT
!EOF!
Output
sh-4.2$ cat aprm_mirrordb_persons_2022_11_16.csv
"PERSONID","LASTNAME"
1,"das"
2,"das2"
3,"abc"
4,"def"
5,"testdata only temporary purpose"
6,"testdata only temporary purpose"
7,"testdata only temporary purpose"
I suspect that's a bug. An easy fix would be run the script with SQLcl, eg
test.sql
================
set termout off
set feedback off
set headsep off
set linesize 4000
set pagesize 999
set newpage none
set termout off
set trimspool on
spool test.dat
select /*csv*/ * from scott.emp;
spool off
set termout on
host cat x:\temp\test.dat
gave me
SQL> @x:\temp\test.sql
"EMPNO","ENAME","JOB","MGR","HIREDATE","SAL","COMM","DEPTNO"
7369,"SMITH","CLERK",7902,17/DEC/80,800,,20
7499,"ALLEN","SALESMAN",7698,20/FEB/81,1600,300,30
7521,"WARD","SALESMAN",7698,22/FEB/81,1250,500,30
7566,"JONES","MANAGER",7839,02/APR/81,2975,,20
7654,"MARTIN","SALESMAN",7698,28/SEP/81,1250,1400,30
7698,"BLAKE","MANAGER",7839,01/MAY/81,2850,,30
7782,"CLARK","MANAGER",7839,09/JUN/81,2450,,10
7788,"SCOTT","ANALYST",7566,09/DEC/82,3000,,20
7839,"KING","PRESIDENT",,17/NOV/81,5000,,10
7844,"TURNER","SALESMAN",7698,08/SEP/81,1500,,30
7876,"ADAMS","CLERK",7788,12/JAN/83,1100,,20
7900,"JAMES","CLERK",7698,03/DEC/81,950,,30
7902,"FORD","ANALYST",7566,03/DEC/81,3000,,20
7934,"MILLER","CLERK",7782,23/JAN/82,1300,,10