Need Help: Shell script to call sql session with variables stored in .txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help: Shell script to call sql session with variables stored in .txt file
# 1  
Old 10-29-2014
Need Help: Shell script to call sql session with variables stored in .txt file

Hi,
I need help in writing a shell script which can read data from a text file (Cancel_ID.txt) and then calls sqlplus session (Cancel.sql) with the first line parameter of the text file ("0322600453") till all rows are not completed.

The data in text file will be like following:

0322600453
CAN
5076

2867000260
CAN
3496

Code:
sqlplus -S <<EOF > /dev/null
user/password@universe
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;



SET ECHO ON
SET SERVEROUTPUT ON
SPOOL $LOGFILE

-------------------------------------------------------------------------------------
---- Here I need to make a loop to read text file and pass on the first line as 
---- parameter to following .sql script till all the rows are not finished in text 
---- file
-------------------------------------------------------------------------------------
@cancel.sql 

SPOOL OFF
EOF



sql_return_code=$?

if [ $sql_return_code != 0 ]
then
echo "The Cancel.sql script failed. Please refer to the log results.txt for more information"
echo "Error code $sql_return_code"
exit 0;
fi

# 2  
Old 10-29-2014
Quote:
Originally Posted by Khan28
Hi,
I need help in writing a shell script which can read data from a text file (Cancel_ID.txt) and then calls sqlplus session (Cancel.sql) with the first line parameter of the text file ("0322600453") till all rows are not completed.

The data in text file will be like following:

0322600453
CAN
5076

2867000260
CAN
3496

Code:
sqlplus -S <<EOF > /dev/null
user/password@universe
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
 
 
 
SET ECHO ON
SET SERVEROUTPUT ON
SPOOL $LOGFILE
 
-------------------------------------------------------------------------------------
---- Here I need to make a loop to read text file and pass on the first line as 
---- parameter to following .sql script till all the rows are not finished in text 
---- file
-------------------------------------------------------------------------------------
@cancel.sql 
 
SPOOL OFF
EOF
 
 
 
sql_return_code=$?
 
if [ $sql_return_code != 0 ]
then
echo "The Cancel.sql script failed. Please refer to the log results.txt for more information"
echo "Error code $sql_return_code"
exit 0;
fi

Hello Khan28,

Welcome to forum, you can use following command for same which will take input from a file(with sql queries) and put it's output to a output file.

Code:
sqlplus user/password@universe < QUERIES.sql > Output_file

As I am not that much clear on your requirement so just an idea. Then after saving the sql output to a file you can check it's data if any error or according to your need you can perform the operations.


Thanks,
R. Singh
# 3  
Old 10-29-2014
Hi R Singh,
Thanks for your prompt reply. The requirement is to read values from txt file and pass those to sqlplus session for each record coming from the txt file. The pseudo code will be like this

While line in $ cancel.txt
do
sqlplus cancel.sql "How to read first parameter of the text file here"
done
# 4  
Old 10-29-2014
Hello Khan28,

You can try with following as a startup. Let's say we have cancel.txt as follows.
Code:
cat cancel.txt
USER_NAME= Singh
Password= xyz
universe= abscedd

Then script can be as follows.
Code:
cat test_script.ksh
user=`awk -F"=" '/USER_NAME/ {print $2}' cancel.txt`
password=`awk -F"=" '/Password/ {print $2}' cancel.txt`
universe=`awk -F"=" '/universe/ {print $2}' cancel.txt`
sqlplus "$user"/"$password"@"$universe" < QUERIES.sql > Output_file
 
you can do testings here with Output file as per your reqiurements.

Thanks,
R. Singh
# 5  
Old 10-29-2014
SQL is a descriptive language, not a procedural and it does not support constructs like loops. You'll have to construct the loop in the shell script and call the sql-client within this loop.
Does the sql-commandfile cancel.sql already accept the parameters? The first paramerer (in cancel.sql) is referenced as &1 (unless you changed the default via set define).
I would put all SQL*Plus commands (WHENEVER, SET... and SPOOL) into cancel.sql and then write your script like this if you are sure the file Cancel_ID.txt is always in the correct format (maybe validate that as start of the script):
Code:
#!/bin/bash
# read first line holding the parameter until there is nothing more to read
while read CANCEL_ID; do   
   sqlplus -s user/password@universe @cancel.sql $CANCEL_ID
   sql_return_code=$?
   if [ $sql_return_code != 0 ]
   then
      echo "The Cancel.sql script failed. Please refer to the log results.txt for more information"
      echo "Error code $sql_return_code"
      exit 0;
   fi
   # read second line and ignore it
   read DUMMY                     
   # read 3rd line and ignore
   read DUMMY
   # read empty line between the paragraphs and ignore
   read DUMMY                  
# feed the file to the loop
done <Cancel_ID.txt


Last edited by cero; 10-29-2014 at 09:27 AM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to call sql file

hi , the below script contains sql query and after executed it sends the output of the query (output.txt) to an email body with conditional subject line based on the output of all_counts_match.txt. i want to make this script generic so that it can accept the sql file as parameter and can... (5 Replies)
Discussion started by: itzkashi
5 Replies

2. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

3. Shell Programming and Scripting

How to call SQL Loader in shell script?

HI Experts, I am pretty new to scripting and i need to create a perl or shell script which should fetch a file from local directory and insert the data into a table using sql loader. This will be later added to chron job to run daily and fetch all files and load them into the table. Also i... (1 Reply)
Discussion started by: sam1234
1 Replies

4. Shell Programming and Scripting

Need to write shell script for my .sql file call

Hi Guys, I need to write a simple shell script which will generate a .csv file/report by calling .sql file inside a shell script. Can somebody help me on this. Thanks in advance! Regards, LK (7 Replies)
Discussion started by: lakshmanraok117
7 Replies

5. UNIX for Advanced & Expert Users

call sql through shell script

Hi i am not able to connect sqlplus my script is as follows $ORACLE_HOME/bin/sqlplus << ! > /tmp/extract/DM.txt and output is SQL*Plus: Release 11.1.0.7.0 - Production on Wed Jan 18 02:53:54 2012 Copyright (c) 1982, 2008, Oracle. All rights reserved. Enter user-name: t175481... (1 Reply)
Discussion started by: tushar_spatil
1 Replies

6. Shell Programming and Scripting

How to call stored procedure with CLOB out parameter from shell script?

I have written a stored procedure in oracle database, which is having a CLOB OUT parameter. How can i call this stored procedure from shell script and get the CLOB object in shell script variable? (0 Replies)
Discussion started by: vel4ever
0 Replies

7. Shell Programming and Scripting

call shell script from pl/sql block

Hi Experts, I want to call script_name.ksh as many time as id in customer table and also pass it as a parameter to script. someting Like below. for i in select id from customer do ./script_name.ksh $i & done I have figured out how to have ID from customer but now how to call... (3 Replies)
Discussion started by: Opamps123
3 Replies

8. Shell Programming and Scripting

how to call shell script from pl/sql loop

Hello, I am doing a shell script which contain a pl/sql loop to search for 3 values, i would like to call another shell script inside this sql loop each time it find the values. so how can i call shell script from pl/sql using its variables, any idea? Here is idea about the code: my... (1 Reply)
Discussion started by: rosalinda
1 Replies

9. Shell Programming and Scripting

how can i call a shell script from pl/sql

I would like to call the shell script from pl/sql and i need to uses the value returned by the shell script in pl/sql procedure. can any one suggest me how can i do that? (3 Replies)
Discussion started by: rajesh.P
3 Replies

10. UNIX for Dummies Questions & Answers

how can a call shell script from pl/sql

I like to call a shell script from pl/sql proceduere and i have to use the shell script return value in that procedure. i am using oracle 9i and cygwin. can any one suggest me how can i do this (0 Replies)
Discussion started by: rajesh.P
0 Replies
Login or Register to Ask a Question