Sponsored Content
Top Forums Shell Programming and Scripting Shell Script to execute Oracle query taking input from a file to form query Post 302572482 by felipe.vinturin on Thursday 10th of November 2011 05:56:16 AM
Old 11-10-2011
Depending on how much users you have, then Shell_Life's solution will be better, but here is one:
Code:
#!/bin/ksh
############################################################################
buildDBSearchStr ()
{
	inFile="${1}"
	
	if [ ! -f "${inFile}" -o ! -r "${inFile}" ]
	then
		echo "Input file: [${inFile}] does not exist or is not readable."
		exit 1
	fi
	
	numUsers=0
	unset dbSearchStr
	
	while read dbUserName
	do
		dbUserName="${dbUserName:-NULL}"
		
		[ "${dbUserName}" == "NULL" ] && continue
		
		if [ ${numUsers} -eq 0 ]
		then
			dbSearchStr="'${dbUserName}'"
		else
			dbSearchStr="${dbSearchStr},'${dbUserName}'"
		fi
		numUsers=`expr ${numUsers} + 1`
	done < "${inFile}"

	if [ ${numUsers} -eq 0 ]
	then
		echo "No users added."
		exit 1
	fi
	
	echo "DB Search string: [${dbSearchStr}]"
}
############################################################################

############################################################################
# Main
############################################################################
if [ $# -ne 1 ]
then
	echo "Arguments required: [1] - [Users Input File]."
	exit 1
fi

dbOutputFile="./dbUsersData.txt"
dbLogFile="./dbUsersData.log"

buildDBSearchStr "${1}"

searchDBQuery="SELECT CITY FROM TableA WHERE USER IN (${dbSearchStr});"
dbSets="SET LINESIZE 600 PAGESIZE 0"
echo "${dbSets}\n${searchDBQuery}\nEXIT;" | sqlplus -S -L ${DBUSER}/${DBPASS}@${ORACLE_SID} 1> "${dbOutputFile}" 2> "${dbLogFile}"
retCode=${?}

if [ ${retCode} -ne 0 ]
then
	echo "Failed to execute query: [${retCode}]. Check log file: [${dbLogFile}]."
else
	echo "Query executed successfully!"
fi

############################################################################

The input file must be of form, otherwise you can change the code to suit your needs:
Code:
# cat searchUsers.txt
A
B
C
D
E
F

And must execute it as:
Code:
./searchUsersDB.sh searchUsers.txt

It is quite simple, but I hope it helps! Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute oracle query determined at runtime from a unix script

Hi I am trying to run a SQL statement from a unix script determined at runtime. It is throwing me an error. Please advise some solution to this. echo "Enter username for the database" read username echo "Enter password for the database" read password echo "Enter SQL stmt" read... (4 Replies)
Discussion started by: infyanurag
4 Replies

2. Shell Programming and Scripting

TO execute .sql 2005 query file in shell script

Hi, I know in oracle a .sql file is called by @ <path> /<filename>. But how to call in sql 2005, I am opening the sql sessionwith sqsh, is there any command to execute there a .sql file (query in sql 2005) in K shell script. (0 Replies)
Discussion started by: n2ekhil
0 Replies

3. 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

4. Shell Programming and Scripting

run oracle query remotly with shell script

hello how to run shell script to excute oracle queries on remote db ? i have tried as following sqlplus -s user/password@remote_server "select query;" but not working also this one sqlplus -s user/password@remote_server `select query;` not working :( i add this line to run another... (4 Replies)
Discussion started by: mogabr
4 Replies

5. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

6. Shell Programming and Scripting

Shell script to query Oracle table

Hi, unix gurnis I need help for following requirement for writing a shell scritp. log in to oracle database, query one table total records (select count(*) from table1), pass the return value to a file. Thanks in advance (2 Replies)
Discussion started by: ken002
2 Replies

7. Shell Programming and Scripting

Problems with storing oracle sqlplus query output shell script

Hello everyone, I have a RHEL 5 system and have been trying to get a batch of 3-4 scripts each in a separate variables and they are not working as expected. I tried using following syntax which I saw a lot of people on this site use and should really work, though for some reason it doesn't... (3 Replies)
Discussion started by: rockf1bull
3 Replies

8. 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

9. Shell Programming and Scripting

Taking information from a postgres sql query and putting it into a shell script array

I have a postgres sql statement that is the following: select age from students; which gives me the entries: Age --- 10 15 13 12 9 14 10 which is about 7 rows of data. Now what I would like to do with this is use a shell script to create an array age. As a results... (3 Replies)
Discussion started by: JSNY
3 Replies

10. Shell Programming and Scripting

Shell script to execute sql query.

Hi Experts, Need your support. Not able to use sql query alias in shell script. Could you please help me in finding right way to use alias with sql query in shell script. Below is the code i am using. #!/bin/bash sqlplus -s abc/abc@abc << EOF> bcd.csv set trimspool on select zone_id... (4 Replies)
Discussion started by: as7951
4 Replies
line(1) 						      General Commands Manual							   line(1)

NAME
line - Reads one line from standard input SYNOPSIS
line STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: line: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
None DESCRIPTION
The line command copies one line, up to and including a newline, from standard input and writes it to standard output. Use this command within a shell command file to read from your terminal. The line command always writes at least a newline character. NOTES
The line utility has no internationalization features and is marked LEGACY in XCU Issue 5. Use the read utility instead. EXIT STATUS
Success. End-of-File. EXAMPLES
To read a line from the keyboard and append it to a file, enter: echo 'Enter comments for the log:' echo ': c' line >>log This shell procedure displays the message: Enter comments for the log: It then reads a line of text from the keyboard and adds it to the end of the file log. The echo ': c' command displays a : (colon) prompt. See the echo command for information about the c escape sequence. SEE ALSO
Commands: echo(1), ksh(1), read(1), Bourne shell sh(1b), POSIX shell sh(1p) Functions: read(2) Standards: standards(5) line(1)
All times are GMT -4. The time now is 04:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy