473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DB SQLSTATE check returns wrong value

1
CREATE PROCEDURE NYCIKCP.LIB_SEQUNCE_GEN(IN "@ADB_DIVISION" VARCHAR(32))
SPECIFIC LIB_SEQUNCE_GEN
MODIFIES SQL DATA
NOT DETERMINISTIC
NULL CALL
LANGUAGE SQL
BEGIN
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE SQLCODE INT DEFAULT 0;
DECLARE INT_COUNTER INTEGER DEFAULT 1;
DECLARE VC_P_ID VARCHAR(32) DEFAULT NULL;

DECLARE CURSOR_MAST_TABLE CURSOR FOR
--SELECT P_ID FROM LIBRA_PROCESS_MAST MAST WHERE MAST.DIVISION_ID='adv_GCS' AND MAST.ADDENDUM IS NULL ORDER BY MAST.P_NAME;
SELECT P_ID FROM LIBRA_PROCESS_MAST MAST WHERE MAST.DIVISION_ID=@ADB_DIVISION AND MAST.ADDEND_PARENT IS NULL ORDER BY MAST.P_NAME;
OPEN CURSOR_MAST_TABLE;

FETCH FROM CURSOR_MAST_TABLE INTO VC_P_ID;

WHILE(SQLSTATE = '00000') DO
UPDATE LIBRA_PROCESS_MAST MAST SET MAST.DIV_SHORT_CODE_INDEX=INT_COUNTER WHERE MAST.P_ID=VC_P_ID;
FETCH FROM CURSOR_MAST_TABLE INTO VC_P_ID;
SET INT_COUNTER=INT_COUNTER+1;
END WHILE;

CLOSE CURSOR_MAST_TABLE;

END
;

this proc when run, run for ages and does not terminate. When i debug this proc from Embarcadero DBArtisan i could detect that the while loop is causing to run the proc, as its not getting terminated even after the sqlstate changes from '00000' to '02000' when end of cursor record is reached.

I have no clue why the while loop returns true for the check SQLSTATE = '00000') when the sqlstate vale is '02000'.

i have tried replacing the while loop with if loop, i am still facing the issue. i belive the issue is not with the while or for loop but the way the check is performed in the proc.

could someone suggest the correct way to perform the check and the exit procedure for end of cursor.


refrence:
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.apdv.sql.doc/doc/c0024361.htm
Apr 3 '08 #1
2 3180
sakumar9
127 Expert 100+
As of now, you can try following code. It will solve your purpose.

I have modified the code to work for my sample database. You can either try it against sample database or change it according to your requirement.

Expand|Select|Wrap|Line Numbers
  1. DROP PROCEDURE jairam@
  2.  
  3. CREATE PROCEDURE jairam()
  4. MODIFIES SQL DATA
  5. NOT DETERMINISTIC
  6. NULL CALL
  7. LANGUAGE SQL
  8. BEGIN
  9.   DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
  10.   DECLARE SQLCODE INTEGER DEFAULT 0;
  11.   DECLARE errorLabel CHAR(32) DEFAULT '';
  12.  
  13. DECLARE INT_COUNTER INTEGER DEFAULT 1;
  14. DECLARE at_end SMALLINT DEFAULT 0;
  15. DECLARE VC_P_ID INTEGER;
  16. DECLARE CURSOR_MAST_TABLE CURSOR FOR SELECT ID FROM STAFF;
  17.  
  18. DECLARE CONTINUE HANDLER FOR NOT FOUND
  19.   SET at_end = 1;
  20.  
  21. DECLARE EXIT HANDLER FOR SQLEXCEPTION
  22.   SIGNAL SQLSTATE value SQLSTATE SET MESSAGE_TEXT = errorLabel;
  23.  
  24. OPEN CURSOR_MAST_TABLE;
  25.  
  26. FETCH FROM CURSOR_MAST_TABLE INTO VC_P_ID;
  27.  
  28. WHILE(at_end = 0) DO
  29. UPDATE STAFF SET SALARY=SALARY + 1 WHERE ID=VC_P_ID;
  30. FETCH FROM CURSOR_MAST_TABLE INTO VC_P_ID;
  31. SET INT_COUNTER=INT_COUNTER+1;
  32. END WHILE;
  33.  
  34. CLOSE CURSOR_MAST_TABLE;
  35.  
  36. END@
Regards
-- Sanjay
Apr 3 '08 #2
sakumar9
127 Expert 100+
The problem that you are facing is because of SET command that you have after FETCH. SET is also an SQL and will always pass. So it overwrites the SQLSTATE of FETCH. So the condition in WHILE loop is always true.

Just change the order of FETCH and SET, make FETCH as last statement in WHILE loop. This will solve your purpose.

Hope it clears your doubt.

Regards
-- Sanjay
Apr 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
0
by: Srinivas B.S.S | last post by:
Hi, I am using MyODBC 3.51.06 on solaris. When a ODBC call fails to execute due to communication problems with database server, according to ODBC reference, sqlstate should be set to 08S01...
0
by: minjie | last post by:
Hello, I have a C++ CLI app that uses stored procedures to get record sets from tables. It works fine except for accessing one of the tables, which means the general logic of the code is OK. I got...
3
by: Jason Gyetko | last post by:
I'm running DB2 v8.1 FP5 and am trying to link tables via ODBC from Access 2002 to my DB2 database. Access lets me connect to the database and returns a list of all tables I can select from, but...
1
by: xf wang via DBMonster.com | last post by:
when i call db2 store producedure,db2 throws exception whose sqlstate is identical t0 38503.I find information as follows: DB2 does check for certain types of limited actions that erroneously...
3
by: nan | last post by:
Hi All, I am trying to connect the Database which is installed in AS400 using DB2 Client Version 8 in Windows box. First i created the Catalog, then when i selected the connection type...
4
by: jason.awlt | last post by:
Greetings, I recently being bugged by the following error on my DB2. SQL0902C SQLSTATE = 58005 Reason Code = 14 This error comes out everytime i tried to insert some record to a table...
8
by: Earl | last post by:
What's the best way to check for null on an ExecuteScalar? The following would fire the command twice: if (cmd.ExecuteScalar() != null) { intContactID = Convert.ToInt32(cmd.ExecuteScalar()); }
11
by: aisling.cronin | last post by:
Hi I am using ctime to convert the following string 1144412677847 .... Please could some one to double check if they get the same result as me (The Time is Sun Nov 02 09:11:51 2031). It seems...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.