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 > Database Server Software > DB2 > Exit out of an IF statement in a stored procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-10, 17:52
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Exit out of an IF statement in a stored procedure

Hi,

If I want to iterate through a bunch of records of a cursor and exit as soon a condition is met, what is the correct syntax for this? For eg, in this case as soon as the condition is met, I would like it to start executing the statements after CLOSE c1 ( not exit from the procedure). Thanks!


CREATE PROCEDURE TEST()
RESULT SETS 0
MODIFIES SQL DATA
NOT DETERMINISTIC
LANGUAGE SQL
BEGIN
DECLARE c1 CURSOR FOR.....
OPEN c1;
FETCH records...
-- check the value of the record.. if condition is met, close cursor, exit and go to OTHER STATEMENTS1 ---
CLOSE c1;

-- OTHER STATEMENTS1 ---
-- OTHER STATEMENTS2 ---

END;
Reply With Quote
  #2 (permalink)  
Old 08-12-10, 23:00
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You need two things. First you need a condition handler (NOT FOUND) to set some variable when the cursor is exhausted. Then you put you FETCH in a loop based on the variable. You then can set the variable in the IF condition to exit the loop.

Andy
Reply With Quote
  #3 (permalink)  
Old 08-12-10, 23:42
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
CREATE PROCEDURE TEST()
RESULT SETS 0
MODIFIES SQL DATA
NOT DETERMINISTIC
LANGUAGE SQL
BEGIN

DECLARE at_end INT DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';

DECLARE c1 CURSOR FOR.....

DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;

OPEN c1;

fetch_loop:
LOOP

FETCH C1 INTO xxxxxxxx, xxxxxxxxxx, xxxxxxxxx;

IF (at_end > 0 )
THEN LEAVE fetch_loop;
END IF;

-- OTHER STATEMENTS WITHIN LOOP ---
-- if possible, do a commit every n number of rows if you have updated any data
COMMIT;

END LOOP fetch_loop;

CLOSE c1;

-- OTHER STATEMENTS1 ---
-- OTHER STATEMENTS2 ---
-- Do a final commit if you update any data
Commit;
END;
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 08-13-10, 05:19
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
or be a bit more direct and add that condition to the where clause and only select that record you need.
Dave
Reply With Quote
  #5 (permalink)  
Old 08-13-10, 10:39
wilsonfv wilsonfv is offline
Registered User
 
Join Date: Apr 2009
Posts: 42
I believe you just need, when meet certain condition, simply execute close cursor statement, then execute following statement, that would do...
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