MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WHENEVER clause in DB2 programs

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
pai_sunil_bs
Beginner


Joined: 19 Sep 2005
Posts: 21
Topics: 11

PostPosted: Mon Jun 19, 2006 4:10 am    Post subject: WHENEVER clause in DB2 programs Reply with quote

In my program there is a statement:-
EXEC SQL WHENEVER SQLERROR
GO TO 9800-ABEND
END-EXEC.
EXEC SQL WHENEVER SQLWARNING
CONTINUE
END-EXEC.

However, there is a SQL Query in the same program:-
EXEC SQL
SELECT MAX(PHN_NBR)
INTO :WS-SEQ:WS-NULL-1
FROM PHONE_V
WHERE <Clause>
END-EXEC

IF SQLCODE = +0
ADD +1 TO WS-SEQ
MOVE WS-SEQ TO WS-PHN-SEQ-NBR
WS-PRV-SEQ-NBR
ELSE
MOVE +1 TO WS-PHN-SEQ-NBR
WS-PRV-SEQ-NBR.

My query is if a SQL error occurs, whether the control goes to the ELSE part OR the code within WHENEVER SQLERROR will be executed.

I couldnt find my help from the IBM manual about overriding the WHENEVER cause.
Back to top
View user's profile Send private message
CZerfas
Intermediate


Joined: 31 Jan 2003
Posts: 211
Topics: 8

PostPosted: Mon Jun 19, 2006 4:28 am    Post subject: Reply with quote

The WHENEVER statement is valid for EVERY SQL statement you code. On the other hand, you are right, with these WHENEVER statements in place, the IF-statement doesn't make sense.

Since the WHENEVER statement is used by the DB2 precompiler to generate GOTO-statements into your program, you have no choice to override this setting during runtime. This is why I could not recommend to use the WHENEVER statement at all!!!

regards
Christian
Back to top
View user's profile Send private message
pai_sunil_bs
Beginner


Joined: 19 Sep 2005
Posts: 21
Topics: 11

PostPosted: Mon Jun 19, 2006 7:21 am    Post subject: Reply with quote

I checked in XPED..The control doesnt come to the SQLCODE check. Instead it goes to the the PARA specified within the WHENEVER Clause. So I have commented OUT the WHENEVER clause.

Anyway Thanks for the response.

If anyone has more information on this please share.
Back to top
View user's profile Send private message
shekar123
Advanced


Joined: 22 Jul 2005
Posts: 528
Topics: 90
Location: Bangalore India

PostPosted: Mon Jun 19, 2006 8:00 am    Post subject: Reply with quote

Sunil,

Whenever you want to show code please insert them between the code tags and send so that it will be easier to understand.Why do you not try using EVALUATE statement.Try this code below:
Code:

EXEC SQL
   SELECT MAX(PHN_NBR)
   INTO :WS-SEQ:WS-NULL-1
   FROM PHONE_V
   WHERE <Clause>
END-EXEC.


EVALUATE SQLCODE
  WHEN +0
   ADD +1 TO WS-SEQ
   MOVE WS-SEQ TO WS-PHN-SEQ-NBR WS-PRV-SEQ-NBR
  WHEN 100
       CONTINUE
  WHEN OTHER
   MOVE +1 TO WS-PHN-SEQ-NBR WS-PRV-SEQ-NBR.
END-EVALUATE.

More information on the WHENEVER STATEMENT:
[code:1:1a379be14e]
The WHENEVER statement :

The WHENEVER statement causes DB2 to check the SQLCA and continue processing your program, or branch to another area in your program if an error, exception, or warning occurs. The condition handling area of your program can then examine SQLCODE or SQLSTATE to react specifically to the error or exception. The WHENEVER statement allows you to specify what to do if a general condition is true. You can specify more than one WHENEVER statement in your program. When you do this, the first WHENEVER statement applies to all subsequent SQL statements in the source program until the next WHENEVER statement. The WHENEVER statement looks like this:

EXEC SQL
WHENEVER condition action
END-EXEC

The condition of the WHENEVER statement is one of these three values:

SQLWARNING Indicates what to do when SQLWARN0 = W or SQLCODE contains a positive value other than 100. DB2 can set SQLWARN0 for several reasons
_________________
Shekar
Grow Technically
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group