Sponsored Content
Top Forums Shell Programming and Scripting Run sql query in shell script and output data save as delimited text Post 302993984 by Jaganjag on Friday 17th of March 2017 06:40:47 AM
Old 03-17-2017
Blade 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:
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
Code:
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
Code:
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



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-17-2017 at 07:56 AM.. Reason: Added CODE tags.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to assign sql output data to shell script variable

Hi Guys ! I am new to unix and want to find out how we can make sql statement data to shell script variable? Any help/suggestion is greatly appreciated -Chandra (1 Reply)
Discussion started by: kattics
1 Replies

2. Shell Programming and Scripting

how to use data in unix text file as input to an sql query from shell

Hi, I have data in my text file something like this. adams robert ahmed gibbs I want to use this data line by line as input to an sql query which i run by connecting to an oracle database from shell. If you have code for similar scenario , please ehlp. I want the output of the sql query... (7 Replies)
Discussion started by: rdhanek
7 Replies

3. Shell Programming and Scripting

How to run a SQL select query in Oracle database through shell script?

I need to run a SQL select query in Oracle database and have to capture the list of retrieved records in shell script. Also i would like to modify the query for certain condition and need to fetch it again. How can i do this? Is there a way to have a persistent connection to oracle database... (9 Replies)
Discussion started by: vel4ever
9 Replies

4. Shell Programming and Scripting

Problem in formatting output of SQL query in excel sheet in shell script

Hi Guys.. Need your help to format the output of my shell script. I am using spool command to take out put in csv file. below is my code. (for example) col USERNAME for a15 col EMAIL for a30 col FULL_NAME for a20 col LAST_LOGIN for a40 col DATE_CREATED for a40 SPOOL 120.csv... (3 Replies)
Discussion started by: Agupte
3 Replies

5. Shell Programming and Scripting

Run SQL thru shell script: how to get a new line when run sql query?

Hi, this's Pom. I'm quite a new one for shell script but I have to do sql on shell script to query some information from database. I found a concern to get a new line...When I run my script, it retrieves all data as wondering but it's shown in one line :( What should I do? I'm not sure that... (2 Replies)
Discussion started by: Kapom
2 Replies

6. Shell Programming and Scripting

Shell script to run sql query having a long listing of parameters

Hi, I have a query regarding execution of a sql query having long listing of parameters ..I need to execute this query inside a shell script. The scenario is like.... Suppose I have a file abc.txt that has the card numbers..it could be in thousands.. then I need to fire a query like ... (12 Replies)
Discussion started by: vsachan
12 Replies

7. Shell Programming and Scripting

SQL Query in Shell Script output formatting

Hi All, #!/bin/ksh call_sql () { sql=$1 sqlplus -s $sqlparam_sieb <<EOF SET ECHO OFF; SET NEWPAGE NONE; SET SQLBL OFF; SET VERIFY OFF; SET LINESIZE 2000; SET... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

8. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

9. Shell Programming and Scripting

Shell script appending output of sql query

I am writing the following script to create the file v_out.txt. sqlplus -s /nolog << EOF CONNECT scott/tiger@orcl; whenever sqlerror exit sql.sqlcode; set newpage 0; SET PAGESIZE 0; SET ECHO OFF; SET FEEDBACK OFF; SET HEADING OFF; SET VERIFY OFF; SET LINESIZE 100; set tab off; set... (7 Replies)
Discussion started by: itzkashi
7 Replies

10. UNIX for Beginners Questions & Answers

Not able to write SQL query output in to .csv file with shell script.

I am trying to write SQL query output into a .csv file. But in the output columns are displaying in different lines instead of coming in one line. Main Code shell script: this is my code: #!/bin/bash file="db_detail.txt" . $file rm /batch/corpplan/bin/dan.csv... (6 Replies)
Discussion started by: sandeepgoli53
6 Replies
All times are GMT -4. The time now is 10:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy