2

I am unable to use %notfound as I get the following error. How can I get past this?? I'm unable to figure out a way to use cursors apart from this method. Please help with other methods to use a cursor loop without using %notfound.

The character "%" following "EXIT WHEN c_rqstid" is not valid.. SQLCODE=-7, SQLSTATE=42601, DRIVER=3.63.123 SQL Code: -7, SQL State: 42601

I have set @ as the delimeter and the code is as follows

create PROCEDURE TEST111()
AS: 
begin 
DECLARE c_id integer; 
DECLARE c_isactive integer; 
DECLARE c_status integer; 
CURSOR c_rqstid is SELECT REQUESTID,REQUESTSTATUS,ISACTIVE FROM SAMPLE.REQUEST;
 OPEN c_rqstid;
  FOR LOOP FETCH c_rqstid into c_id,c_status,c_isactive ;
  ----will code this later
  EXIT WHEN c_rqstid%NOTFOUND;
  END LOOP;
 CLOSE c_rqstid;
end
@

Thanks for any help in advance...

3
  • Welcome to SO. Good question, well formatted. Good luck!
    – Jess
    Commented Feb 8, 2014 at 21:58
  • What type of OS is your DB2 server running on: IBM i (fka OS/400), LUW (Linux, Unix, Windows), or z/OS? And what version?
    – WarrenT
    Commented Feb 9, 2014 at 13:11
  • @WarrenT Its on Windows and 10.5
    – Sherri
    Commented Feb 9, 2014 at 17:43

1 Answer 1

2

You'll need to check the SQLCODE or SQLSTATE variables:

http://pic.dhe.ibm.com/infocenter/db2luw/v10r1/topic/com.ibm.db2.luw.apdv.sqlpl.doc/doc/c0009028.html

 DECLARE SQLCODE INTEGER DEFAULT 0;
 DECLARE SQLSTATE CHAR(5) DEFAULT '00000';

 WHILE(SQLSTATE = '00000') DO
    SET p_sum = p_sum + p_sal;
    FETCH FROM c INTO p_sal; 
 END WHILE;
7
  • It appears Sherri is trying to use PL/SQL compatibility, eh?
    – WarrenT
    Commented Feb 9, 2014 at 13:14
  • 1
    pic.dhe.ibm.com/infocenter/db2luw/v10r5/… shows an example of using %NOTFOUND.
    – WarrenT
    Commented Feb 9, 2014 at 13:34
  • @Charles I tried that but the SQLSTATE doesn't change and I end up in an infinite loop. Am I missing something???
    – Sherri
    Commented Feb 9, 2014 at 17:26
  • @WarrenT Thanks but the examples mentioned are for in-string usage of '%'. I need to use a function named %NOTFOUND.
    – Sherri
    Commented Feb 9, 2014 at 17:31
  • @Charles Hey that worked. The SQLSTATE resets on every statement so you have to check it immediately after the fetch. I had placed it wrong initially but read up the link you posted and got my mistake. Thanks a lot. :)
    – Sherri
    Commented Feb 9, 2014 at 19:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.