Skip to Main Content
IBM Power Ideas Portal
Hide about this portal


This portal is to open public enhancement requests against IBM Power Systems products, including IBM i. To view all of your ideas submitted to IBM, create and manage groups of Ideas, or create an idea explicitly set to be either visible by all (public) or visible only to you and IBM (private), use the IBM Unified Ideas Portal (https://ideas.ibm.com).


Shape the future of IBM!

We invite you to shape the future of IBM, including product roadmaps, by submitting ideas that matter to you the most. Here's how it works:

Search existing ideas

Start by searching and reviewing ideas and requests to enhance a product or service. Take a look at ideas others have posted, and add a comment, vote, or subscribe to updates on them if they matter to you. If you can't find what you are looking for,

Post your ideas
  1. Post an idea.

  2. Get feedback from the IBM team and other customers to refine your idea.

  3. Follow the idea through the IBM Ideas process.


Specific links you will want to bookmark for future use

Welcome to the IBM Ideas Portal (https://www.ibm.com/ideas) - Use this site to find out additional information and details about the IBM Ideas process and statuses.

IBM Unified Ideas Portal (https://ideas.ibm.com) - Use this site to view all of your ideas, create new ideas for any IBM product, or search for ideas across all of IBM.

ideasibm@us.ibm.com - Use this email to suggest enhancements to the Ideas process or request help from IBM for submitting your Ideas.

Better documentation of Sqlstate behavior in IF statement

See this idea on ideas.ibm.com

I ran into some peculiar piece of code that didn't do what I expected and decided to run a small test.
The goal is to see if SqlState is being reset in an if-statement. My human brain says it shouldn't be reset in the example code below

begin
   declare sqlState char(5) default '';
   declare myLocalVar char(1) default 'X';
   select IBMREQD into myLocalVar from sysibm.sysdummy1 where 0 = 1;
   if SqlState = '02000'  then
     if SqlState = '00000' then
       call systools.lprintf ('2:' || SqlState || myLocalVar);
     end if;
   end if;
 end;

SqlState and myLocalVar are declared
I select IBMREQD from sysdummy1 with a where condition that assures it's not found (the value in sysdummy1 is 'Y'). My SqlState is therefore 02000,
I checked that. So, my subsequent if-statement is then true.
In my mind the inner if-statement can't be true if the outer is, so it should never produce an entry in the joblog.
However, it does give "2:00000X" in the joblog.
I'm  aware that the call to lprintf will alter the SqlState value, but I only expect a new value for SqlState after the call.
It seems that the if statement itself modifies SqlState or am I overlooking something? 

The SQL 7.4 reference states:

Considerations for SQLSTATE and SQLCODE SQL variables: When the first SQL-procedure-statement in
the IF statement is executed, the SQLSTATE and SQLCODE SQL variables reflect the result of evaluating
the search-conditions of that IF statement. If an IF statement does not include an ELSE clause and
none of the search-conditions evaluate to true, then when the statement that follows the IF statement is
executed, the SQLSTATE and SQLCODE SQL variables reflect the result of evaluating the search conditions
of that IF statement.

 

It doesn't say that IF itself is changing SqlState, but it seems to do so, which results in quite strange behavior of the code. 

I wonder if it's a bug or a deficit in the documentation

 

Idea priority Medium
  • Guest
    Reply
    |
    Apr 2, 2025
    IBM believes this is working as expected and as it is documented so it is being closed.

    Whenever a statement is processed by SQL, the result returns a SQLCODE/SQLSTATE. This includes the evaluation of the predicates in an IF statement.

    As you noted, the documentation states:
    When the first SQL-procedure-statement in the IF statement is executed, the SQLSTATE and SQLCODE SQL variables reflect the *result of evaluating the search-conditions of that IF statement*.

    This means that, yes, the evaluation of the IF condition modifies the SQLCODE/SQLSTATE values. If you need to preserve them, you must immediately save them in local variables since running any subsequent statement will produce a new SQLCODE/SQLSTATE result.

    In your example, the first IF
    if SqlState = '02000' then
    is examining the result of the prior SELECT statement.

    The second IF
    if SqlState = '00000' then
    is examining the result of the prior search condition in the first IF statement

    The excerpt you posted from the SQL Reference is saying that before the next logical statement is run, which would be either one of the legs of the IF statement or right after the IF, the SQLCODE/SQLSTATE values reflect the result of evaluating the IF search-condition.

    You will observe similar rules for evaluation of the search-condition for a CASE, WHILE, and UNTIL.

    This is working as intended and as the SQL standard requires.

    Db2 for i development team
    IBM Power Systems Developement