Executing Sql Query Using Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing Sql Query Using Shell Script
# 8  
Old 07-31-2006
thanks for your reply markile..

in here, the user name and password are stored in a particular directory (batchuse/user.ksh). I have to retrieve the user names from there. how will i do that.

if i have to store the results of a query in a particular file, how do i give the path of the file wher to store.

thanks again
# 9  
Old 07-31-2006
Hi,

Can anybody explain me the following code. Can you tell me how i need to modify this code to incorporate my scenario.

START OF CODE


umask 000
#
# Source in the home profile
#
if [ -f $HOME/.profile ]
then
. $HOME/.profile
else
echo "ERROR: Error sourcing $HOME/.profile !" >> $EBPP_DATAFEED_LOGFILE
exit 10
fi

TMP_OUTPUT_FILE="temp_log.tmp"
TMPBATCHPROC="temp_batch.tmp"

export CURRENT_FEED=`date +%Y-%m-%d`
#
# Source in the environment variables (if possible)
#
if [ -f ./eBPPDataFeed_env.rc ]
then
. ./eBPPDataFeed_env.rc
fi
#

# Set the error codes
#
. ./errorcodes.ksh
#
# Obtain the secure username and password (if it exists)
#
if [ -f "$BATCH_USER_DIR/batch_user.ksh" ]
then
TMPPWD=`pwd` # v1.1
cd $BATCH_USER_DIR # v1.1
. ./batch_user.ksh # v1.1
RTNCD=$? # v1.1
cd $TMPPWD # v1.1
if [ $RTNCD = 0 ]
then
export SIEBEL_USERNAME=$USERID
export SIEBEL_PASSWORD=$PASSWORD
else
exit $EXIT_BATCHUSER_FAILED
fi
else
echo "ERROR: batch_user.ksh does not exist, unable to source in variables" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_BATCHUSER_DOESNOTEXIST
fi
#
# Check if the Environment Variables are set, if not, exit -1
#
for i in SIEBEL_USERNAME SIEBEL_PASSWORD \
EBPP_DATAFEED_ROOT EBPP_DATAFEED_DATA EBPP_DATAFEED_SCRIPTS EBPP_DATAFEED_LOGS \
SIEBEL_DB_USERNAME SIEBEL_DB_PASSWORD SIEBEL_DB_ORACLESID SIEBEL_DB_TABLE_OWNER \
SIEBEL_ROOT EXIT_SUCCESS EXIT_ENV_MISSING EXIT_ENV_NOVALUE \
EXIT_SQLPLUSERROR EXIT_BATCHUSER_FAILED EXIT_BATCHUSER_DOESNOTEXIST MINERRORLEVEL
do

IS_SETF1=`env | grep "^$i=" | cut -d= -f1`
if [[ -z $IS_SETF1 ]]
then
echo "FATAL: Environment Variable $i is not set, terminating process" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_ENV_MISSING
fi

IS_SETF2=`env | grep "^$i=" | cut -d= -f2`
if [[ -z $IS_SETF2 ]]
then
echo "FATAL: Environment Variable $i has no value, terminating process" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_ENV_NOVALUE
fi
done
#
echo "main> INFO: Running eBPP Profile Data Feed Process with Table Owner $SIEBEL_DB_TABLE_OWNER @ `date`" >> $EBPP_DATAFEED_LOGFILE
# Let us know what we are doing
# Get the last feed date from system preferences and store in variable#

LAST_FEED=`sqlplus -S $SIEBEL_DB_USERNAME/$SIEBEL_DB_PASSWORD@$SIEBEL_DB_ORACLESID <<!
SET HEAD OFF
SET FEEDBACK OFF
SET PAGESIZE 0
SET LINESIZE 900
SELECT VAL FROM $SIEBEL_DB_TABLE_OWNER.S_SYS_PREF where SYS_PREF_CD = 'ALLTEL - EBPP Profile Feed';
QUIT;

!`

RTNCD=$?
if [[ ! 0 = $RTNCD ]]
then
echo "ERROR: SQLPLUS Failed when searching for system preferences" >> $EBPP_DATAFEED_LOGFILE
cat $EBPP_DATAFEED_LOGFILE
exit $EXIT_SQLPLUSERROR
fi

echo "main> INFO: Last eBPP Data Feed Date" $LAST_FEED >>$EBPP_DATAFEED_LOGFILE
echo "main> INFO: Current eBPP Data Feed Date" $CURRENT_FEED >> $EBPP_DATAFEED_LOGFILE
echo "main> INFO: Start Extracting eBPP Data Feed file `date`" >> $EBPP_DATAFEED_LOGFILE

# Extract data feed and make dat file and save in data directory
# exit -1

sqlplus -S $SIEBEL_DB_USERNAME/$SIEBEL_DB_PASSWORD@$SIEBEL_DB_ORACLESID > $EBPP_DATAFEED_DATAFILE <<!
SET HEAD OFF
SET FEEDBACK OFF
SET PAGESIZE 0
SET LINESIZE 900
-- This part of the query will pick up any B2C profile changes.
SELECT ''||ORG.X_BILL_ACCT_NUM||'|'||CON.EMAIL_ADDR||'|'||PROF.X_PAPER_BILL_FLG||''
FROM $SIEBEL_DB_TABLE_OWNER.S_CONTACT CON,
$SIEBEL_DB_TABLE_OWNER.S_ORG_EXT ORG,
$SIEBEL_DB_TABLE_OWNER.S_USER_PROF PROF
WHERE ORG.ROW_ID = CON.PR_DEPT_OU_ID
AND PROF.PAR_ROW_ID = CON.ROW_ID
AND PROF.LAST_UPD >= TO_DATE('$LAST_FEED','yyyy-mm-dd')
AND ORG.X_BILL_ACCT_NUM IS NOT NULL
AND ORG.X_B2B_ACCOUNT IS NULL
AND PROF.X_PAPER_BILL_FLG IS NOT NULL
UNION
-- This part of the query will pick up any B2B profile changes.
SELECT ''||ORG.X_BILL_ACCT_NUM||'|'||CON.EMAIL_ADDR||'|'||PROF.X_PAPER_BILL_FLG||''
FROM $SIEBEL_DB_TABLE_OWNER.S_CONTACT CON,
$SIEBEL_DB_TABLE_OWNER.S_ORG_EXT ORG,
$SIEBEL_DB_TABLE_OWNER.S_USER_PROF PROF
WHERE ORG.PR_CON_ID = CON.ROW_ID
AND PROF.PAR_ROW_ID = ORG.ROW_ID
AND PROF.LAST_UPD >= TO_DATE('$LAST_FEED','yyyy-mm-dd')
AND ORG.X_BILL_ACCT_NUM IS NOT NULL
AND ((ORG.X_B2B_ACCOUNT = 'Y') or (ORG.X_B2B_ACCOUNT ='N'))
AND PROF.X_PAPER_BILL_FLG IS NOT NULL;
QUIT;
!


RTNCD=$?
if [[ ! 0 = $RTNCD ]] || [[ ! 0 = `grep -c 'ORA-' $EBPP_DATAFEED_DATAFILE` ]]
then
echo "ERROR: There were errors during EBPP data load process. Please check the log file: $EBPP_DATAFEED_LOGFILE" >> $EBPP_DATAFEED_LOGFILE
cat $EBPP_DATAFEED_DATAFILE >> $EBPP_DATAFEED_LOGFILE
echo "Removing data file $EBPP_DATAFEED_DATAFILE"
rm $EBPP_DATAFEED_DATAFILE
RTNCD=1
exit $EXIT_SQLPLUSERROR
fi


echo "main> INFO: Total Records written to eBPP data file : `wc -l $EBPP_DATAFEED_DATAFILE | tr -s ' '| cut -d ' ' -f2`" >> $EBPP_DATAFEED_LOGFILE
echo "main> INFO: `date`" >> $EBPP_DATAFEED_LOGFILE


###added the below 2 lines HD 1597462
echo "main> INFO: Completed sql query ready to send data files to valicert server- Return Code :" $RTNCD >> $EBPP_DATAFEED_LOGFILE
if [[ 0 = $RTNCD ]]
then

# upload data file into valicert server.
# Verify that Valicert files exist in the data directory

echo "main> INFO: Connecting to VALICERT and uploading data file" >> $EBPP_DATAFEED_LOGFILE

# export VALICERT_NOTIFICATION_FILE=$EBPP_DATAFEED_SCRIPTS/valiNotificati.txt
#
# Read the files and process
#
#for FILE in `ls $EBPP_DATAFEED_DATAFILE 2> /dev/null`
for FILE in `ls $EBPP_DATAFEED_DATA/EBPP_Users_Feed*.dat 2> /dev/null`
do
if [ $FILE=$EBPP_DATAFEED_DATAFILE ]
then

echo "main> INFO: Valicert Files: Name $FILE is being uploaded" >> $EBPP_DATAFEED_LOGFILE
$VAL_ST_BIN/alltel-stc -f -g 5 -w 25 -u $FILE https://$VALICERT_CHECKFREE_USERNAME:$VALICERT_CHECKFREE_PASSWORD@$VALICERT_URL >> $EBPP_DATAFEED_LOGFILE

if [[ ! $? = 0 ]]
then
echo "FATAL: Error in uploading the file $FILE: `date` **" >> $EBPP_DATAFEED_LOGFILE
exit -1
else
print "main> INFO: Successfully Completed uploading $FILE `date '+%m-%d-%Y %H:%M:%S'`" >> $EBPP_DATAFEED_LOGFILE
tmpFN=$FILE.ctl
EBPP_DATAFEED_CONTROLFILE=$tmpFN
wc -l $FILE | tr -s " "| cut -d" " -f2 > $EBPP_DATAFEED_CONTROLFILE
$VAL_ST_BIN/alltel-stc -f -g 5 -w 25 -u $EBPP_DATAFEED_CONTROLFILE https://$VALICERT_CHECKFREE_USERNAME:$VALICERT_CHECKFREE_PASSWORD@$VALICERT_URL >> $EBPP_DATAFEED_LOGFILE
fi
fi
done

#Create csv file eBPP_Last_FeedInfo.csv file with current system date

echo "main> INFO: Create eBPP_Last_FeedInfo.csv current date $CURRENT_FEED for Data Writer" >> $EBPP_DATAFEED_LOGFILE
echo "BUSOBJ=System Preferences" > $EBPP_LAST_DATAFEED
echo "BUSCOMP=System Preferences" >> $EBPP_LAST_DATAFEED
echo "FIELD_PROPERTIES=key," >> $EBPP_LAST_DATAFEED
echo "FIELD_NAMES=Name,Value" >> $EBPP_LAST_DATAFEED
echo "BEGIN" >> $EBPP_LAST_DATAFEED
echo "ALLTEL - EBPP Profile Feed,$CURRENT_FEED" >>$EBPP_LAST_DATAFEED

echo "main> INFO: Moving files to processed directory" >> $EBPP_DATAFEED_LOGFILE

mv $EBPP_DATAFEED_DATA/EBPP_Users_Feed*.* $EBPP_DATAFEED_PROCESSED

echo "main> INFO: Update ALLTEL - eBPP Profile Feed System Preferences with current date $CURRENT_FEED, Java Beans" >> $EBPP_DATAFEED_LOGFILE

java -classpath $EBPP_DATAFEED_CLASSPATH \
com.alltel.siebel.datawriter.DataWriter $EBPP_LAST_DATAFEED >> $EBPP_DATAFEED_LOGFILE

RTNCD=$?
if [ $RTNCD != 0 ]
then
echo "ERROR: Running Java Programming failed (returned $RTNCD)! terminating process!" >> $EBPP_DATAFEED_LOGFILE
echo "Please check the log file and classpath" >> $EBPP_DATAFEED_LOGFILE
exit 40
fi

exit $EXIT_SUCCESS
##added next 2 line for HD 1597462
else
echo "Process Exited with Error " >> $EBPP_DATAFEED_LOGFILE
exit $MINERRORLEVEL
fi
# 10  
Old 07-31-2006
Quote:
Originally Posted by ragha81
thanks for your reply markile..

in here, the user name and password are stored in a particular directory (batchuse/user.ksh). I have to retrieve the user names from there. how will i do that.

if i have to store the results of a query in a particular file, how do i give the path of the file wher to store.

thanks again
That depends on how the script works. Since it's a KSH file, I assume that you'll want to execute like this:
Code:
. batchuse/user.ksh

This way, if a variable is being set, it gets included into your session.
# 11  
Old 07-31-2006
Quote:
Originally Posted by ragha81
Hi,

Can anybody explain me the following code. Can you tell me how i need to modify this code to incorporate my scenario.
What part?

Also, please use the vB code tags so that we can read the code.
# 12  
Old 07-31-2006
Hi please explain me the code mentioned below. i have enclosed tags. hope u will be able to read it..


HTML Code:
umask 000
#
# Source in the home profile
#
if [ -f $HOME/.profile ]
then
. $HOME/.profile
else
echo "ERROR: Error sourcing $HOME/.profile !" >> $EBPP_DATAFEED_LOGFILE
exit 10
fi

TMP_OUTPUT_FILE="temp_log.tmp"
TMPBATCHPROC="temp_batch.tmp"

export CURRENT_FEED=`date +%Y-%m-%d`
#
# Source in the environment variables (if possible)
#
if [ -f ./eBPPDataFeed_env.rc ]
then
. ./eBPPDataFeed_env.rc
fi
#

# Set the error codes
#
. ./errorcodes.ksh
#
# Obtain the secure username and password (if it exists)
#
if [ -f "$BATCH_USER_DIR/batch_user.ksh" ]
then
TMPPWD=`pwd` # v1.1
cd $BATCH_USER_DIR # v1.1
. ./batch_user.ksh # v1.1
RTNCD=$? # v1.1
cd $TMPPWD # v1.1
if [ $RTNCD = 0 ]
then
export SIEBEL_USERNAME=$USERID
export SIEBEL_PASSWORD=$PASSWORD
else
exit $EXIT_BATCHUSER_FAILED
fi
else
echo "ERROR: batch_user.ksh does not exist, unable to source in variables" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_BATCHUSER_DOESNOTEXIST
fi
#
# Check if the Environment Variables are set, if not, exit -1
#
for i in SIEBEL_USERNAME SIEBEL_PASSWORD \
EBPP_DATAFEED_ROOT EBPP_DATAFEED_DATA EBPP_DATAFEED_SCRIPTS EBPP_DATAFEED_LOGS \
SIEBEL_DB_USERNAME SIEBEL_DB_PASSWORD SIEBEL_DB_ORACLESID SIEBEL_DB_TABLE_OWNER \
SIEBEL_ROOT EXIT_SUCCESS EXIT_ENV_MISSING EXIT_ENV_NOVALUE \
EXIT_SQLPLUSERROR EXIT_BATCHUSER_FAILED EXIT_BATCHUSER_DOESNOTEXIST MINERRORLEVEL
do

IS_SETF1=`env | grep "^$i=" | cut -d= -f1`
if [[ -z $IS_SETF1 ]]
then
echo "FATAL: Environment Variable $i is not set, terminating process" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_ENV_MISSING
fi

IS_SETF2=`env | grep "^$i=" | cut -d= -f2`
if [[ -z $IS_SETF2 ]]
then
echo "FATAL: Environment Variable $i has no value, terminating process" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_ENV_NOVALUE
fi
done
#
echo "main> INFO: Running eBPP Profile Data Feed Process with Table Owner $SIEBEL_DB_TABLE_OWNER @ `date`" >> $EBPP_DATAFEED_LOGFILE
# Let us know what we are doing
# Get the last feed date from system preferences and store in variable#

LAST_FEED=`sqlplus -S $SIEBEL_DB_USERNAME/$SIEBEL_DB_PASSWORD@$SIEBEL_DB_ORACLESID <<!
SET HEAD OFF
SET FEEDBACK OFF
SET PAGESIZE 0
SET LINESIZE 900
SELECT VAL FROM $SIEBEL_DB_TABLE_OWNER.S_SYS_PREF where SYS_PREF_CD = 'ALLTEL - EBPP Profile Feed';
QUIT;

!`

RTNCD=$?
if [[ ! 0 = $RTNCD ]] 
then
echo "ERROR: SQLPLUS Failed when searching for system preferences" >> $EBPP_DATAFEED_LOGFILE
cat $EBPP_DATAFEED_LOGFILE
exit $EXIT_SQLPLUSERROR
fi
# 13  
Old 07-31-2006
Quote:
Originally Posted by ragha81
Hi please explain me the code mentioned below. i have enclosed tags. hope u will be able to read it..
Actually you added some color so it helps a little. The tags that you need to use use code and /code enclosed within brackets [ and ].

Since I'm not exactly certain which part of the code, I'll comment each line.
Code:
LAST_FEED=`sqlplus -S $SIEBEL_DB_USERNAME/$SIEBEL_DB_PASSWORD@$SIEBEL_DB_ORACLESID <<!  <== Starts a "here" documnet where all lines 
                                                                                            until the "!" is reached are executed in sqlplus
SET HEAD OFF                   <== Turns off Oracle column headings
SET FEEDBACK OFF               <== Causes sqlplus not to return any feedback from queries (e.g. number of rows deleted, inserted, etc)
SET PAGESIZE 0                 <== Cause all page breaks to be turned off and removes column headings
SET LINESIZE 900               <== Returns 900 character rows
SELECT VAL FROM $SIEBEL_DB_TABLE_OWNER.S_SYS_PREF where SYS_PREF_CD = 'ALLTEL - EBPP Profile Feed'; <== SQL statement
QUIT;                          <== Quit sqlplus

!`                             <== End of here document

RTNCD=$?                       <== return code from sqlplus, which isn't useful if your script runs while Oracle is offline (you'll get 0 then too)
if [[ ! 0 = $RTNCD ]]          <== Check to see if return code was not 0
then
echo "ERROR: SQLPLUS Failed when searching for system preferences" >> $EBPP_DATAFEED_LOGFILE  <== Logged message for SQL execution errors
cat $EBPP_DATAFEED_LOGFILE     <== Dump the log file
exit $EXIT_SQLPLUSERROR        <== Exit script with error code
fi

# 14  
Old 07-31-2006
mark, i really appreciate your assiatance .. thank u very much for explaining me in detail.

the user name and passwords are stored in a seperate file to prevent easy access. they retrieve the username and pwd using the following code. i will be grateful if you could explain this a little bit. i m actually working hard on understanding, i m having difficulties as this is the first time i am ever working on unix.

Code:
umask 000
#
# Source in the home profile
#
if [ -f $HOME/.profile ]
then
. $HOME/.profile
else
echo "ERROR: Error sourcing $HOME/.profile !" >> $EBPP_DATAFEED_LOGFILE
exit 10
fi

TMP_OUTPUT_FILE="temp_log.tmp"
TMPBATCHPROC="temp_batch.tmp"

export CURRENT_FEED=`date +%Y-%m-%d`
#
# Source in the environment variables (if possible)
#
if [ -f ./eBPPDataFeed_env.rc ]
then
. ./eBPPDataFeed_env.rc
fi
#

# Set the error codes
#
. ./errorcodes.ksh
#
# Obtain the secure username and password (if it exists)
#
if [ -f "$BATCH_USER_DIR/batch_user.ksh" ]
then
TMPPWD=`pwd` # v1.1
cd $BATCH_USER_DIR # v1.1
. ./batch_user.ksh # v1.1
RTNCD=$? # v1.1
cd $TMPPWD # v1.1
if [ $RTNCD = 0 ]
then
export SIEBEL_USERNAME=$USERID
export SIEBEL_PASSWORD=$PASSWORD
else
exit $EXIT_BATCHUSER_FAILED
fi
else
echo "ERROR: batch_user.ksh does not exist, unable to source in variables" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_BATCHUSER_DOESNOTEXIST
fi
#
# Check if the Environment Variables are set, if not, exit -1
#
for i in SIEBEL_USERNAME SIEBEL_PASSWORD \
EBPP_DATAFEED_ROOT EBPP_DATAFEED_DATA EBPP_DATAFEED_SCRIPTS EBPP_DATAFEED_LOGS \
SIEBEL_DB_USERNAME SIEBEL_DB_PASSWORD SIEBEL_DB_ORACLESID SIEBEL_DB_TABLE_OWNER \
SIEBEL_ROOT EXIT_SUCCESS EXIT_ENV_MISSING EXIT_ENV_NOVALUE \
EXIT_SQLPLUSERROR EXIT_BATCHUSER_FAILED EXIT_BATCHUSER_DOESNOTEXIST MINERRORLEVEL
do

IS_SETF1=`env | grep "^$i=" | cut -d= -f1`
if [[ -z $IS_SETF1 ]]
then
echo "FATAL: Environment Variable $i is not set, terminating process" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_ENV_MISSING
fi

IS_SETF2=`env | grep "^$i=" | cut -d= -f2`
if [[ -z $IS_SETF2 ]]
then
echo "FATAL: Environment Variable $i has no value, terminating process" >> $EBPP_DATAFEED_LOGFILE
exit $EXIT_ENV_NOVALUE
fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with Executing sql in Shell Script

Hello~ I have a requirement to write a shell script which will connect to the oracle database and run a select count(*) query on a table. The script should succeed only when the count returns a number greater than zero. If the count returns zero, the script should fail. Can someone please... (3 Replies)
Discussion started by: Naren.N
3 Replies

2. Red Hat

Sql query through shell script

hey , i am using this code to store value of a sql query and and then use it in other query but after some time , but it is not working. please help #!/bin/bash val_1=$( sqlplus -s rte/rted2@rel76d2 << EOF setting heading off select max(stat_id) from cvt_stats; exit EOF ) nohup... (5 Replies)
Discussion started by: ramsavi
5 Replies

3. UNIX for Dummies Questions & Answers

Regarding executing sql query in shell script

Hi, I have one SQL file prepared in UNIX and one script that is executing that. In SQL i have Update and create queries. I want to introduce conditions in SQL file (in UNIX) that if either of the create or update query failes whole transaction should be rollback. I just have 1 create... (2 Replies)
Discussion started by: abhii
2 Replies

4. Shell Programming and Scripting

Executing SQL Query and sending a mail

Hi all, My reqirenet goes like this. Need to execute one select statement within the script and send a mail to the users with the number of records fecthed from the query. Please help.. Thanks. (3 Replies)
Discussion started by: Achiever
3 Replies

5. Shell Programming and Scripting

Executing a shell script from a PL / SQL Block

Hi, I need to call a shell script present on solaris server from within a PL / SQL block. Kindly suggest.. Thanks Sudhir (1 Reply)
Discussion started by: sudhird
1 Replies

6. UNIX for Dummies Questions & Answers

executing SQL query using unix shell script

I want to perform few post-session success tasks like update a status to 'true' in one of the sql database table, update date values to current system date in one of the configuration table in sql. How do i achieve this in a post session command?syntax with example will be helpful. (3 Replies)
Discussion started by: nathanvaithi
3 Replies

7. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

8. Shell Programming and Scripting

Script executing sql query

Hello, I have a sh script excuting a sql query through sqlplus. I am having trouble making my date equal to the date of the server time in the sql script. How can i call the server date from my query? Thanks (2 Replies)
Discussion started by: kingluke
2 Replies

9. Shell Programming and Scripting

executing a SQL query in shell script

Hi ALL, I need an help in connecting to oracle database, executing a select query and printing it on the screen. Can any one please write a simple code or psuedo code and let me know. select query returns multiple values( say select name from emp) Thanks in advance LM (1 Reply)
Discussion started by: lijju.mathew
1 Replies

10. UNIX for Dummies Questions & Answers

Executing a SQL query from a shell script

I cannot figure out how to run a SQL script, or just a sqlplus query, from a shell script (bash or ksh). Basically, I need to su - oracle from root and run a query, then test the exit status. (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question