Executing sql statement from .sh file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing sql statement from .sh file
# 1  
Old 10-13-2010
Executing sql statement from .sh file

Hi,


How to execute sql statements from the .sh file ??

Means, when we run .sh file then the sql statements within it should be get executed one by one from the sqlplus

With Regards
# 2  
Old 10-13-2010
Could you be more explicit?

---------- Post updated at 14:41 ---------- Previous update was at 14:35 ----------

I usually use this in my scripts calling .sql file:
Code:
sqlplus -silent $GO <<END
start $HOME/sqlscript
exit;
END

# 3  
Old 10-13-2010
Hi,

I want to test oracle connection through shell script.

Will Prompt user for
1) schema user name
2) password
3) tns name

I have redirected the output to check_conn.txt and further also want some sql output to be get redirected for verification.

how to do ?

Code:
sqlplus $user_name/$password@$tns;exit; > c:/tmp/check_conn.txt

or can u suggest me some other method to verify the oracle connection using shell script ?

With Regards
# 4  
Old 10-13-2010
Here is a simple example on how to execute sqlplus from a script:
Code:
#!/bin/ksh

sqlplusDefSets="SET HEAD OFF\nSET FEEDBACK OFF\nSET LINESIZE 600\n"
myQuery="SELECT TO_CHAR(SYSDATE, 'YYYYMMDD hh24:MI:SS') FROM DUAL;"
mySQLPlusLogFile="/<PathToSQLPlus>/<LogFile>.log"

execSQLPlus ()
{
	dbConnString="${1:-NULL}"
	dbQuery="${2:-NULL}"
	
	echo "[execSQLPlus] Starting..."
	
	if [ "${dbConnString}" == "NULL" -o "${dbQuery}" == "NULL" ]
	then
		echo "[execSQLPlus] Either database connection string or query are null arguments. Exiting: [${EXIT_ERR:-1}]."
		exit ${EXIT_ERR:-1}
	fi
	
	sqlplusStartTime=${SECONDS}
	
	echo "${sqlplusDefSets}\n${dbOverwriteSets}\n${dbQuery}\nExit\n" | sqlplus -S -L ${dbConnString} 1>"${mySQLPlusLogFile}" 2>"${mySQLPlusLogFile}"
	sqlplusReturnCode=${?}
	
	sqlplusEndTime=${SECONDS}
	sqlplusDiffTime=`expr ${sqlplusEndTime:-0} - ${sqlplusStartTime:-0}`
	
	echo "[execSQLPlus] Ended SQLPlus. Took: [${sqlplusDiffTime}] second(s)."

	dbErrorCount=0
	egrep -i 'ORA-|SP2-|TNS-|Usage' "${mySQLPlusLogFile}" | sort | uniq | \
	while read dbError
	do
		echo "[execSQLPlus] Database error: [${dbError}]."
		dbErrorCount=`expr ${dbErrorCount} + 1`
	done

	if [ ${sqlplusReturnCode} -ne 0 -o ${dbErrorCount} -ne 0 ]
	then
		echo "[execSQLPlus] Database returned errors. Exiting: [${EXIT_ERR:-1}]."
		exit ${EXIT_ERR:-1}
	fi
	
	echo "[execSQLPlus] Ended."
	# return 0
}

execSQLPlus "<ConnectionString: user/pass@${ORACLE_SID}>" "${myQuery}"

echo "########################################"
echo "# Result"
echo "########################################"
cat "${mySQLPlusLogFile}"
echo "########################################"

I hope it helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Executing SQL's in parallel

Hi Folks, I have requirement to pull a bunch of SQL's from a table in DB and execute them in parallel and update the status of each query as and when they complete. Can you please help me with ideas on how this can be achieved? create table list_of_sql ( id number, full_sql... (3 Replies)
Discussion started by: member2014
3 Replies

2. Shell Programming and Scripting

Execute and log each statement/block SQL file

Hi friends, I would like to get some help on the following requirement. I have a SQL file with following things, select 1 from dual; select user from dual; select sysdate from dual; BEGIN PL/SQL Code END; / This file will be saved as sql file. When I run my expected shell script,... (1 Reply)
Discussion started by: ssnair
1 Replies

3. Homework & Coursework Questions

Problem with executing a possible if or case statement script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a phonebook program. It should use functions to perform the required tasks. It should be menu-based,... (1 Reply)
Discussion started by: Rgasin02
1 Replies

4. UNIX for Advanced & Expert Users

Executing SQL file in UNIX

I have few .sql file at some location say /x/y/z. I want to execute those .sql files in UNIX server so that all packages,procedures can be applied on the database of the UNIX server. (1 Reply)
Discussion started by: Dip
1 Replies

5. UNIX for Advanced & Expert Users

Bash executing Orcale Update statement

Hi All, Using Solaris box bash-3.00$ echo $BASH_VERSION 3.00.16(1)-release I have a real bummer of a bug, basically Im running a bash script that executes a bash function "dbase_sql". The bash function accepts a parameter in the form of an Oracle update statement eg ... (1 Reply)
Discussion started by: satnamx
1 Replies

6. Shell Programming and Scripting

Executing a Oracle SQL statement in a UNIX script

Hi All, I need to select one column from a table based upon the passed in parameter. I tried this: sqlplus -silent $MISP_USER << EOF set feedback off; set verify off; set sqlprompt "" SELECT mail_flag FROM dailyjobs WHERE job_name = '$1'; exit 0 EOF exit... (1 Reply)
Discussion started by: ganga.dharan
1 Replies

7. Shell Programming and Scripting

need some help in executing sql

i am stuck with a problem ... i have a shell script that gets the file name as input and performs the following operation... it runs through a for loop inside from which i connect to sqlplus and run a procedure that creates a number of tables .. there is no space in my server so we have made... (0 Replies)
Discussion started by: sais
0 Replies

8. Shell Programming and Scripting

Creating an sql statement from a file. Problem with '

Hi, I am trying to create sql statements from a file, but I have a problem with ': This is what I do: cat filex.txt | awk -F: '{print $1,"A","and personnavn like",$5}' | sed -e "s/^/select bruker.brukernavn, person.personnavn from bruker, person where brukernavn like '/" -e "s/$/' and... (2 Replies)
Discussion started by: hannem
2 Replies

9. Shell Programming and Scripting

Executing Multiple .SQL Files from Single Shell Script file

Hi, Please help me out. I have around 700 sql files to execute in a defined order, how can i do it from shell script (3 Replies)
Discussion started by: anushilrai
3 Replies

10. HP-UX

SQL statement output to Log file-How?

Hi all, I need to bring the message to log file.Teradat/Hp-Ux script: ----- ### Update Log Table bteq <<- EOC .run file ${SRC_DATA}/logon.txt .run file ${SRC_DATA}/dbstagebteq.txt .set format off .set foldline off all .set sidetitles off ... (1 Reply)
Discussion started by: vsubbu1000
1 Replies
Login or Register to Ask a Question