How to Print SQLCODE and SQLSTATE info in the Procedure output in DB2 LUW 10.5

Hi All

Please let me know "How to Print SQLCODE and SQLSTATE info in the Procedure output when procedure fails
to process some input records.

So that i can understand the problem of issue.

Thanks in advance!!!
Prardhan NAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
sample ..

create procedure testProc()
 
LANGUAGE SQL
 
BEGIN
--Declare generic variables
 
 DECLARE        v_counter               INTEGER DEFAULT 0;
 DECLARE        v_message               varchar(70);
 DECLARE        v_sqlstate              char(5);
 DECLARE        v_sqlcode               INteger;
 DECLARE        SQLSTATE                char(5);
 DECLARE 	SQLCODE 		INTEGER DEFAULT 0;
 DECLARE        at_notfound             SMALLINT DEFAULT 0;
 DECLARE	is_loopint		SMALLINT;
 
 DECLARE start_ts timestamp; 
 DECLARE end_ts timestamp;
 DECLARE command1 VARCHAR (32000);
 DECLARE v_unique_id    integer;
 DECLARE v_sttype   varchar(100);
 DECLARE v_st        decimal(22,3);
 DECLARE v_st_u    varchar(30);
 
 DECLARE not_found CONDITION for SQLSTATE '02000';
 
 
 DECLARE CONTINUE HANDLER FOR SQLEXEPTION
 BEGIN
  IF is_looping = 1 then
      set v_sqlstate = SQLSTATE;
      set v_sqlcode = SQLCODE;
  else
      return;
  end if;
 END;
 
 DECLARE CONTINUE HANDLER FOR not_found
 BEGIN
	 SET at_notfound = 1;
	 set v_sqlstate = SQLSTATE;
	 set v_sqlcode = SQLCODE;
 END;
 
 
   BEGIN
    SET is_looping = 0;
 
    DECLARE cursor1 CURSOR FOR
    SELECT QUERY
    from TEST_QUERY
    FOR UPDATE OF;  	-- added by momi to be able to set start end end timestamp
 
    OPEN cursor1;
 
    WHILE at_notfound = 0 DO
 
	    set is_looping = 1;
	    set v_sqlstate = '00000';
            set v_sqlcode  = 0;
 
	    FETCH cursor1 INTO command1;
 
	    -- check that we got a row from the cursor
	    if (at_notfound = 0)  then
	      SET start_ts = CURRENT TIMESTAMP;		-- added by momi
 
	      EXECUTE IMMEDIATE command1;
	     
              SET end_ts = CURRENT TIMESTAMP;		-- added by momi
	      
              IF v_sqlcode <> 0 then
                 insert into error_table values (v_sqlcode, v_sqlstate, command1);
              else
                  update test_query
                  set  start_timestamp = start_ts, end_timestamp = end_ts
                  where current of cursor1;
              end if;
	      
	      at_notfound = 0;              -- momi
 
	    end if;
 
    END while;
 
    CLOSE  cursor1;
  COMMIT;
 
 
END;
 
END

Open in new window

From EE database - https://www.experts-exchange.com/questions/23176025/handling-db2-exceptions.html#a20935662
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Prardhan NAuthor Commented:
Thanks for response..

I got the clue that i need to create a error table.
I have declared the SQLCODE and SQLSTATE variables.

i have added following to my code.
DECLARE EXIT HANDLER FOR SQLEXCEPTION
    SET SQLSTATEOUT = SQLSTATE;

DO i need to add the insert into error_code table in with in handler block?
How to move the control to "insert into error_code table" statement.

Pls let me know
0
Pawan KumarDatabase ExpertCommented:
>>DO i need to add the insert into error_code table in with in handler block?
Yes

>>How to move the control to "insert into error_code table" statement.

Execute your sql code
IF v_sqlcode <> 0 then
insert into error_table values (v_sqlcode, v_sqlstate, command1);...
1
Pawan KumarDatabase ExpertCommented:
Question abandoned
Provided solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.