Query a oracle DB when fail put in in the error log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Query a oracle DB when fail put in in the error log
# 8  
Old 02-04-2011
Ok I've finished the script.
Any remarks? Please let me know.
Code:
#!/bin/bash

# values
USER=username
PASS=password
SIDORA=sid
LOG=/var/log/$SIDORA.log
DATUM=date
#Login to the DB
sqlplus -s /nolog <<EOF > tmpfile.log
connect $USER/$PASS@$SIDORA

select 1 from dual;

EOF

errorCode=$(head -2 tmpfile.log | awk '{ print $1 }' | grep 1)
# checks if the last operation (sqlplus) was completed successfully or not
if [ $errorCode != 1 ];

then
$DATUM >> $LOG
fi

# 9  
Old 02-04-2011
The only potential problem I see is if you get an error and the "ORA-" message contains a 1 (for example: ORA-01017 Invalid username or password), you would get a false positive.

You could just return your results to a variable, test the exit code and the results and react accordingly:

I don't have my server up to test, but this should work for you.

I use ksh so please excuse any syntax differences...

Code:
#
# Using <<-EOF allows you to TAB (can not be spaces)
# the EOF over for code alignment, if necessary.
#
DB_STATUS=$(sqlplus -s /nolog <<-EOF
   connect $USER/$PWD@$DB

   set pages 0 trimout on trimspool on
   set echo off termout off sqlprompt ''

   select 1 from dual;

   exit

EOF)

EXIT_CODE=$?

if [[ "${EXIT_CODE}" -ne 0 ]] && [[ "${DB_STATUS}" -ne 1 ]]; then
   echo "Error mesage" >> ${LOG}
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Connection to DB from client server through jdbc:Oracle:Oci8 fail

We tried to use to connect to DB using jdbc:Oracle:Oci8:@<SERVICE-A>. Connection fail / refuse with one DB .But its working with other databases. But through toad, jdbc thin client were able to connect. But this has happen suddenly and were able to connect previously. How to navigate this... (0 Replies)
Discussion started by: udara
0 Replies

2. Programming

Oracle query with field filter

Dear community, I have to make a query from a database and apply the following filter: select filed1,field2,field3 from database where (field1 contains only alphanumeric chars and NOT only numbers) Let me explain better what I Need: 21test ==> OK test ==> OK test21 ==> OK te21st ==>... (6 Replies)
Discussion started by: Lord Spectre
6 Replies

3. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

4. Shell Programming and Scripting

perl- oracle sql query

Hi, I am new to perl.How to query oracle database with perl??? Thanks (1 Reply)
Discussion started by: tdev457
1 Replies

5. Shell Programming and Scripting

PROBLEM WITH ORACLE QUERY IN UNIX SCRIPT

hi Guys, i have a problem with oracle query in my unix script.. I'm getting the following error while executing.. ./logtab.sh: sqlplus -s "pmutv/pmutv1" << EOFSQL^Jset head off^Jinsert into... (2 Replies)
Discussion started by: apple2685
2 Replies

6. Shell Programming and Scripting

Read value from user and use it in Oracle SQL query

Guys can anyone just tell me whether i can pass a value(from UNIX SCRIPT) as an ARGUMENT in Oracle Query? e.g. echo "enter value" read value insert into tablename where col=$value /*something like this*/ (1 Reply)
Discussion started by: subodh.thakar
1 Replies

7. Programming

Oracle Database Query

How can i modify the below to search for the things i'm looking for during a certain time frame? select Node, NodeAlias, Summary, Tally, AlertKey, AlertGroup, Manager, Agent from mrtg_alerts where LastOccurrence > '5-Dec-2010' order by Manager desc; In this particular case, this query is... (3 Replies)
Discussion started by: SkySmart
3 Replies

8. Shell Programming and Scripting

How to put db2 query result into an array in shell script?

Hello, Can someone please advise me how to put the db2 query reult into an array? For example, the query reults are: string A string B string C Then how do I put them into array=string A array=string B ... (2 Replies)
Discussion started by: hanul
2 Replies

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

10. UNIX for Advanced & Expert Users

oracle process query !

This query is For HP-UX 11i server. I have certain oracle process that are running on my system as below :- rotmgr 3986 1 0 07:49:33 ? 0:00 oracleedjlive (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) rotmgr 26356 1 0 08:14:32 ? 0:00 oracleedjlive... (4 Replies)
Discussion started by: kpatel786
4 Replies
Login or Register to Ask a Question