Sponsored Content
Top Forums Shell Programming and Scripting Executing sql statement from .sh file Post 302462102 by felipe.vinturin on Wednesday 13th of October 2010 08:53:09 AM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
MSSQL_FIELD_SEEK(3)													       MSSQL_FIELD_SEEK(3)

mssql_field_seek - Seeks to the specified field offset

SYNOPSIS
bool mssql_field_seek (resource $result, int $field_offset) DESCRIPTION
Seeks to the specified field offset. If the next call to mssql_fetch_field(3) won't include a field offset, this field would be returned. PARAMETERS
o $result - The result resource that is being evaluated. This result comes from a call to mssql_query(3). o $field_offset - The field offset, starts at 0. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 Using mssql_field_seek(3) on the example for mssql_fetch_field(3) <?php // Connect to MSSQL and select the database mssql_connect('MANGOSQLEXPRESS', 'sa', 'phpfi'); mssql_select_db('php'); // Send a select query to MSSQL $query = mssql_query('SELECT * FROM [php].[dbo].[persons]'); // Construct table echo '<h3>Table structure for 'persons'</h3>'; echo '<table border="1">'; // Table header echo '<thead>'; echo '<tr>'; echo '<td>Field name</td>'; echo '<td>Data type</td>'; echo '<td>Max length</td>'; echo '</tr>'; echo '</thead>'; // Dump all fields echo '<tbody>'; for ($i = 0; $i < mssql_num_fields($query); ++$i) { // Fetch the field information, notice the // field_offset parameter is not set. See // the mssql_field_seek call below $field = mssql_fetch_field($query); // Print the row echo '<tr>'; echo '<td>' . $field->name . '</td>'; echo '<td>' . strtoupper($field->type) . '</td>'; echo '<td>' . $field->max_length . '</td>'; echo '</tr>'; // Move the internal seek pointer to the next // row in the result set mssql_field_seek($query, $i + 1); } echo '</tbody>'; echo '</table>'; // Free the query result mssql_free_result($query); ?> SEE ALSO
mssql_fetch_field(3). PHP Documentation Group MSSQL_FIELD_SEEK(3)
All times are GMT -4. The time now is 05:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy