If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Passing Sql Errors to Unix ksh Script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-10-02, 12:17
manialg manialg is offline
Registered User
 
Join Date: Oct 2002
Posts: 5
Red face Passing Sql Errors to Unix ksh Script

Can someone tell me how to pass the db2 sql errors out to
a ksh script? I'm new to db2 and we have several scripts that
run at night - and where the unix script processes ok we are
getting some sql errors that we'd like to trap for -

Thanks
Reply With Quote
  #2 (permalink)  
Old 12-27-02, 12:08
llsmith llsmith is offline
Registered User
 
Join Date: Dec 2002
Location: Little Rock, AR
Posts: 17
Re: Passing Sql Errors to Unix ksh Script

Quote:
Originally posted by manialg
Can someone tell me how to pass the db2 sql errors out to
a ksh script? I'm new to db2 and we have several scripts that
run at night - and where the unix script processes ok we are
getting some sql errors that we'd like to trap for -

Thanks
Here's a quick example of an easy way to do it:


SQLCODE=0

SQLCODE=`db2 -ec +o "update db cfg for $DBNAME using dbheap 11370"`;

if [ $SQLCODE -ne 0 ]
then
echo " "
echo "Error: Failed to Update Db Cfg for $DBNAME"
echo "Error: SQLCODE = $SQLCODE"
exit 1
__________________
Laura Smith, DBA
Reply With Quote
  #3 (permalink)  
Old 11-10-11, 06:19
amcc38 amcc38 is offline
Registered User
 
Join Date: Oct 2011
Posts: 12
Question

Quote:
Originally Posted by llsmith View Post
Here's a quick example of an easy way to do it:


SQLCODE=0

SQLCODE=`db2 -ec +o "update db cfg for $DBNAME using dbheap 11370"`;

if [ $SQLCODE -ne 0 ]
then
echo " "
echo "Error: Failed to Update Db Cfg for $DBNAME"
echo "Error: SQLCODE = $SQLCODE"
exit 1
Dear Sir,

I followed above script to prepare my ksh script with minor change, it always said the line "if [ $SQLCODE -ne 0 ]" is not vallid, and the return status = 0.

My ksh script as below.
db2 "set serveroutput on"
SQLCODE=0
SQLCODE=`db2 -td/ +o -f abc.sql`;
if [ $SQLCODE -eq 0 ]
then
echo "sucess"
exit 0
else
echo "failed"
exit 1
fi
-------------------------------
In abc.sql,
BEGIN
declare v_count integer;

open cursor
loop
fetch xx into xx;
.....
set v_count = v_count + 1
update abc set no_of_count = v_count where ....
call dbms_output.put_line(v_count);
end loop
close cursor
END
/

What wrong of the script ? Please kindly help.
Reply With Quote
  #4 (permalink)  
Old 11-10-11, 11:28
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
I am not sure that
Code:
SQLCODE=`db2..........`
is necessarily correct. SQLCODE is equal to the standard output of the script, not the exit code.
The variable $? contains the exit code, 0 if successful.
Better to do.
Code:
db2..................
if [ $? != 0 ]
then
echo error message
fi
Reply With Quote
  #5 (permalink)  
Old 11-10-11, 20:40
amcc38 amcc38 is offline
Registered User
 
Join Date: Oct 2011
Posts: 12
Quote:
Originally Posted by kitaman View Post
I am not sure that
Code:
SQLCODE=`db2..........`
is necessarily correct. SQLCODE is equal to the standard output of the script, not the exit code.
The variable $? contains the exit code, 0 if successful.
Better to do.
Code:
db2..................
if [ $? != 0 ]
then
echo error message
fi
Dear Kitaman,

It work, thanks ! But if I need to do some checking in sql and return false/-1 to the ksh script, how can I do it ? I tried to use command raise_error or signal, but it said it is invalid. Please kindly help.

Thanks !
Reply With Quote
  #6 (permalink)  
Old 11-11-11, 02:43
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Wrap the db2 command inside a script.
The $? variable is equal to the exit code from the script.
So.

Code:
db2  ..................
if [ error condition a ]
then
exit 2
fi
if  [ error condition b ]
then
exit 3
fi
$? will have a value of 2 if condition a is met, 3 if condition b is met, 0 if the script terminates normally, and 1 if it fails for any other reason.
Reply With Quote
  #7 (permalink)  
Old 11-11-11, 03:08
amcc38 amcc38 is offline
Registered User
 
Join Date: Oct 2011
Posts: 12
Quote:
Originally Posted by kitaman View Post
Wrap the db2 command inside a script.
The $? variable is equal to the exit code from the script.
So.

Code:
db2  ..................
if [ error condition a ]
then
exit 2
fi
if  [ error condition b ]
then
exit 3
fi
$? will have a value of 2 if condition a is met, 3 if condition b is met, 0 if the script terminates normally, and 1 if it fails for any other reason.
Dear Kitaman,

Thanks for your help.
I added "exit 2" in my sql and add the return code checking as below. After testing, whatever I set the exit value to 3,4,5 for testing, why it still return 4 ? Also, how can I show message as a log file during the job is running. I tried to usd dbms_output.put_line('start...'), but it seems not work.

in ksh.
db2 "set serveroutput on"
$status=%?
if [ $status -eq 0]
then
exit 0
fi
if [ $status -eq 2]
then
echo "warnning"
exit 0
fi
if [ $status -gt 2]
then
echo "fail checking"
exit 1
else
exit 1
fi

---
in sql

if checking failed ...
exit 2
end if

Thanks !
Reply With Quote
  #8 (permalink)  
Old 11-11-11, 06:49
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
This line is incorrect.
Code:
$status=%?
it should be
Code:
status=$?
Reply With Quote
  #9 (permalink)  
Old 11-14-11, 01:28
amcc38 amcc38 is offline
Registered User
 
Join Date: Oct 2011
Posts: 12
Quote:
Originally Posted by kitaman View Post
This line is incorrect.
Code:
$status=%?
it should be
Code:
status=$?
Sorry for typing mistake. Actually, in my ksh, it state $status=$?. Whatever I set the exit code is 2 or 3, it still show the exit code is 4 in ksh. Do you know why ? Am I wrongly use the exit code ?

In sql.
exit 2;

In ksh.
status=$?
echo status

Many Thanks !
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On