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
# 1  
Old 01-28-2011
Query a oracle DB when fail put in in the error log

Hi all,

I'm trying to create a script that does the following:
Connect with SQLplus to my oracle db. Do a select 1 from dual; and when I don't get a value back. Than put the $DATE in a logfile.

What I have until now:

Code:
#!/bin/bash

# values
GEN_ERR=1 #error 1 code
USER=username
PASS=mypass
SIDORA=oracle.db
LOG=/var/log/failure.log

#Login to the DB
sqlplus -s /nolog <<EOF 
connect $USER/$PASS@$SIDORA

select 1 from dual;

EOF

errorCode=$?    # checks if the last operation (sqlplus) was completed successfully or not
if [ ${errorCode} -ne 0 ]
then
echo "DATE" >> $LOG
exit ${CRED_ERR}
fi

Any help would be nice !

Last edited by Roadster; 01-28-2011 at 10:05 AM..
# 2  
Old 01-28-2011
Hi,

I would've thought that sqlplus's return value will be set according to system events, not internal SQL parsing?

Is it entirely non-functional presently?
# 3  
Old 01-28-2011
Well the query work.
I get a 1/1 respone, but when I close the DB de response is something like:
Unable to connect, no response from host.

At the first respone, de script don't have to do anything!
But at the second response the script has to put the date and time in logfile.
# 4  
Old 01-28-2011
I believe the status code $? gives the status of the command sqlplus in your above script and not for the 'select ...' statement inside it . IMO re-direct the output to a file and check for its size.Something like..
Code:
OUTFILE=outfile.txt # Declare a output file
#Login to the DB
sqlplus -s /nolog <<EOF > $OUTFILE
connect $USER/$PASS@$SIDORA
select 1 from dual;
exit;
EOF

if [[ ! -s ${OUTFILE} ]]
then
        echo "$DATE" >> $LOG
        exit ${CRED_ERR}
fi

Or you could grep for any 'ORA-..' error in the out file to check if the connection is success or not.
# 5  
Old 02-03-2011
That could work.

But the output of the statement is
1
----------------------
1

so when I do a grep on the 1 it will also write to the logfile when the date in that file is the date and time are 10:11:00
# 6  
Old 02-03-2011
Quote:
Originally Posted by Roadster
That could work.

But the output of the statement is
1
----------------------
1

so when I do a grep on the 1 it will also write to the logfile when the date in that file is the date and time are 10:11:00
Well then don't grep for "1" i.e. do not grep for success; grep for failure.

If there is a "^ORA-" pattern in the outfile, then you are certain that something went wrong.

If not, then everything should've worked fine (note: should've, not must've).

If you do want to test for success as well as failure explicitly, then you may want to provide a column alias like so -

Code:
select 1 my_test_value from dual;

or maybe just fetch a literal string from Oracle -

Code:
select 'my_test_string' x from dual;

and then grep for "my_test_string" otherwise grep for "^ORA-".

tyler_durden
# 7  
Old 02-04-2011
Quote:
Originally Posted by Roadster
That could work.

But the output of the statement is
1
----------------------
1

so when I do a grep on the 1 it will also write to the logfile when the date in that file is the date and time are 10:11:00
Or else you should go for a more refined grep'ing/sed than just a simple grep like..
Code:
grep '1' inputfile

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