Run sql query in shell script and output data save as delimited text

I want to run sql query in shell script and output data save as delimited text (delimited text would be comma)

Code:

SPOOL_FILE=/pgedw/dan.txt
SQL=/pgedw/dan.sql
sqlplus -s username/password@myhost:port/servicename <<EOF
set head on
set COLSEP ,
set linesize 32767
SET TRIMSPOOL ON
SET TRIMOUT ON
SPOOL $SPOOL_FILE
@ $SQL
SPOOL OFF
EOF
echo "Note" |mail -s "Test" -a $SPOOL_FILE  org@mail.com
exit 0

For above code it returning result like below

Column 1,Column 2,Column 3,column 4,Column 5,Column 6,Column 7,Column 8  
--------,--------,--------,--------,--------,--------,--------,--------
03,           364,     364,     364,    1703  1925968,  031417, 0155732
03,           364,     364,     364,    1703  1925968,  031417, 0155754
03,           364,     364,     364,    1703  1925968,  031417, 0155708
03,           364,     364,     364,    1703  1925968,  031417, 0155707 
03,           364,     364,     364,    1703  1925968,  031417, 0155708  
Column 1,Column 2,Column 3,column 4,Column 5,Column 6,Column 7,Column 8 
--------,--------,--------,--------,--------,--------,--------,--------
03,           364,     364,     364,    1703  1925970,  031417, 0155732
03,           364,     364,     364,    1703  1925988,  031417, 0155754
03,           364,     364,     364,    1703  1925998,  031417, 0155708
03,           364,     364,     364,    1703  1925968,  031417, 0155707 
03,           364,     364,     364,    1703  1925968,  031417, 0155710

but actually output i want like this

Column 1,Column 2,Column 3,column 4,Column 5,Column 6,Column 7,Column 8  
03,364,364,364,1703,1925968,031417,0155732
03,364,364,364,1703,1925968,031417,0155754
03,364,364,364,1703,1925968,031417,0155708
03,364,364,364,1703,1925968,031417,0155707 
03,364,364,364,1703,1925968,031417,0155708  
03,364,364,364,1703,1925970,031417,0155732
03,364,364,364,17031,925988,031417,0155754
03,364,364,364,1703,1925998,031417,0155708
03,364,364,364,1703,1925968,031417,0155707 
03,364,364,364,1703,1925968,031417,0155710

Help help me

Thanks in advance

Hello!

In case you forgot to read the forum rules, here is quick copy.

Cheers.

The UNIX and Linux Forums

1 Like

Firstly I would recommend moving the credentials into the here document, else they will be publicly visible to anyone running a simple ps whilst your code is executing.

If the columns are of a fixed width, you may simply get away with adding in a few more statements to format the output column like this:-

COLUMN column-name FORMAT format-rule ;

You will need to know your column names and set them all individually.

Have a look at this page for more information. You may also look at setting number format or number width.

Ordinarily, the output will be presented in a nice tabular form with the default width being the length of the column name or the widest column content, whichever is greater.

If none of these work for you, perhaps you can capture the data into a file and use tr to delete all spaces, however this also may not be what you want.

Do you have any real data we can work with?

Robin

The thing is that sqlplus started life as a command-line reporting tool in the early days of Oracle. The reports had fixed-width data because they were for end-users and humans find fixed width format more convenient to read than csv or delimited data. Delimited data is good for exchanging between systems or applications, but not so good for humans.
Over the years, sqlplus wasn't improved to remedy this particular issue.

In the recent years, Oracle has developed another command-line tool that is kind of a bridge between command-line sqlplus and the graphical tool - SQL Developer. This tool is called "SQLcl" and is at this location: Oracle SQLcl

It's free, it's just a zip file that you extract anywhere you like and all it needs is at least JRE version 8. So, if you are willing to try out this tool and explore it, you will find that it provides a lot of functionality out of the box.

Here are a few commands from my session of SQLcl:

SQL> 
SQL> show version
Oracle SQLDeveloper Command-Line (SQLcl) version: 4.2.0.17.073.1038
SQL> 
SQL> -- csv format: technique 1
SQL> 
SQL> select /*csv*/ * from scott.emp;
"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,19-APR-87,3000,,20
7839,"KING","PRESIDENT",,17-NOV-81,5000,,10
7844,"TURNER","SALESMAN",7698,08-SEP-81,1500,0,30
7876,"ADAMS","CLERK",7788,23-MAY-87,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

14 rows selected. 

SQL> 
SQL> -- csv format: technique 2
SQL> 
SQL> set sqlformat delimited ,
SQL> 
SQL> -- now all queries will return data in csv format
SQL> 
SQL> select * from scott.emp;
"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,19-APR-87,3000,,20
7839,"KING","PRESIDENT",,17-NOV-81,5000,,10
7844,"TURNER","SALESMAN",7698,08-SEP-81,1500,0,30
7876,"ADAMS","CLERK",7788,23-MAY-87,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

14 rows selected. 

SQL> 

That was the easier method because it requires minimum effort on your part.

If you must use sqlplus, then you could use rbatte1's suggestions - use column formats for every column that you will be using, or use a two-step process where you spool the data as it is now and then use a scripting language or command to replace "<multiple_spaces>," by ",". The latter technique is a hit-or-miss because your data itself could have "<multiple_spaces>,".

Finally, the method that would require the most effort on your part would be to handcraft all your queries and concatenate all columns by commas. Something like the following in my sqlplus session:

SQL> 
SQL> set feedback off
SQL> set pagesize 0
SQL> 
SQL> select empno ||','|| ename ||','|| job ||','|| mgr ||','|| hiredate ||','|| sal ||','|| comm ||','|| deptno from scott.emp;
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,19-APR-87,3000,,20
7839,KING,PRESIDENT,,17-NOV-81,5000,,10
7844,TURNER,SALESMAN,7698,08-SEP-81,1500,0,30
7876,ADAMS,CLERK,7788,23-MAY-87,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
SQL> 
SQL> 
1 Like

Thanks durden_tyler for your help . It is working :slight_smile:

---------- Post updated at 09:06 AM ---------- Previous update was at 08:56 AM ----------

I have a SQL script that is selecting rows from a table with more than 50,000 records but I only need one header line at the beginning.

Can any one please suggest the code.

SET HEADING ON
SET PAGESIZE 50000

If I set the 'SET PAGESIZE' to 50000. it repeats, the column headers Evert 50000 lines.

Set the pagesize to 0 and hard-code the header.

How to hard-code the header ? Where I want to hard-code the header

By explicitly writing down the column names that comprise the header.

Since the header, by definition, is at the top, you want to hard-code and append the header at the top of your data.

1 Like

Thanks for your help . It is working