|
Hi Javier
I think the implementation makes sense - it might have been a matter of
developer's or standards author's choice whether to set SQLSTATE (and
SQLCODE) to defaults (reset) before an SQL statement is processed or to
leave them at the value they had after the previous statement, then set
to defaults if there is no error. I've not read the SQL standards, so I
don't know what is said there. And I prefer what is happening now, as
one of these posts remarked is happening in the C++ code.
Nonetheless, we are told to save these return codes immediately after
any SQL statement has completed, so that we can use them to condition
further actions.
I used the term *SQL statement* above - I looked online and see that
many places refer to *IF statements*. So I'd need to see what the
standards call it.
--
*Regards*
*Vern Hamberg*
IBM Champion 2025 <cid:part1.TW2QR0wS.ZQv1LuHs@centurylink.net> CAAC
(COMMON Americas Advisory Council) IBM Influencer 2023
On 2/27/2025 1:39 PM, Javier Sanchez wrote:
From my point of view, the code should not reset the value of SQLSTATEand
before you evaluate another SQL statement, and what the C source code
version does is that: it first sets the value of SQLSTATE to '00000' just
before you execute your second IF statement, making it a true condition
not respecting the previous value of the last SQL statement. That's whatescribió:
I'm saying, to me that is bad logic. But, that's how it is and we
already know how to get over it.
JS
El jue, 27 feb 2025 a las 13:29, Daniel Gross (<daniel@xxxxxxxx>)
an
Hi Javier,
I don't think we have a bug here.
The evaluation of an expression in SQL is something, that can result in
theerror.
Think of an SQL SELECT statement, where you do a "division by zero" in
resultSELECT clause. You will receive a SQLSTATE of '01564' - this is the
SELECT.of the failed evaluation of the expression - not the result of the
reasonThe SELECT only passes this SQLSTATE back to the caller.
The evaluation of an expression sets the SQLCODE - this is only
consequential from SQL's point-of-view.
IMHO - this all works as intended - only because the intention does not
align with our reception, doesn't make it a bug.
HTH
Daniel
Am 27.02.2025 um 19:20 schrieb Javier Sanchez <javiersanchezbarquero@xxxxxxxxx>:
What still makes it "weird", this behavior in SQL P/L. For some
logicIBM
decided to let the C preprocessor code it like that, but using our
asdiscussed here, it should be "a big bug". Good to know to rather usesave
variables.escribió:
JS
El jue, 27 feb 2025 a las 12:06, Daniel Gross (<daniel@xxxxxxxx>)
Absolutely right!
If that is coded as embedded SQL in RPG, you would simply code the IF
variableRPG - and the RPG compiler generates code to compare the (RPG)
theSQLSTATE without evaluating it in SQL.
So indeed the "same" code would work as RPG with embedded SQL.
Regards,
Daniel
Am 27.02.2025 um 18:34 schrieb Javier Sanchez <javiersanchezbarquero@xxxxxxxxx>:
Maybe you didn't debug the C source listing, what I found is that
afterevaluatesfirst
IF statement generates code to call DB2 with the query and is setsSQLSTATE
to what in internal variable used in a data structure for that call,which
at that moment is a NOT FOUND condition ('02000'), and just aftercomparing
the value against '02000' it unconditionally sets SQLSTATE to '00000'just
before the second IF statement. SQLSTATE is then '00000' which
to
TRUE in the second IF.. And it then yet again sets it to '00000'
beevaluating it.
What you could expect with an SQLRPGLE embedded SQL program, may not
withdoesthe
same though. Because the SQLSTATE and SQLCODE variables not only are
automatically created by the precompiler, but the generated RPG code
SQLnot change the value of SQLSTATE "before" you use it again in another
statement.
So your code maybe does work as you expect in an SQLRPGLE program
pagethe
same SQL statements.escribió:
Test it, you may find this is true.
JS
El jue, 27 feb 2025 a las 10:04, Daniel Gross (<daniel@xxxxxxxx>)
Hi,
After reading the docs completely, it seems to be very clear. The
thehttps://www.ibm.com/docs/en/i/7.5?topic=pl-if-statementin
InfoCenter is:
And Martijn you already quoted the right part:
... when the statement that follows the IF statement is executed,
conditions"theSQLSTATE and SQLCODE SQL variables reflect the result of evaluating
search conditions of that IF statement.
The SQLSTATE reflect the result of "evaluating the search
could-
seemsso
evaluating
SqlState = '02000'
as an logical expression, results in an SQLSTATE = '00000' - which
C-IF -absolutely plausible.
Birgitta you are absolutely right - the (SQL-)IF is translated to a
could gobut the expression is still evaluated by SQL - and that evaluation
wrong (like with an unknown variable or something) and therefore
thealso
SQLSTATEresult in a SQLSTATE <> '00000'.
So I was wrong - it's not the IF statement itself, that sets the
Hauser@xxxxxxxxxxxxxxx>:- but the evaluation of the (logical) expression does.
HTH
Daniel
Am 27.02.2025 um 16:33 schrieb Birgitta Hauser <
SQL code is converted into C-Code with embedded SQL .. and only
stars."convertedembedded SQL Statements produce SQLCODE and SQLSTATE.
There is a native C command for an IF, so the IF-Statement is
doesin
the C-IF ... which is NO longer an SQL statement and consequently
not
produce/change the SQLCODE and/or SQLSTATE.
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
Modernization – Education – Consulting on IBM i
Database and Software Architect
IBM Champion since 2020
"Shoot for the moon, even if you miss, you'll land among the
training(Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not
firstsothem
and keeping them!"
"Train people well enough so they can leave, treat them well enough
(Albertthey
don't want to. " (Richard Branson)
"Learning is experience … everything else is only information!"
OfEinstein)
-----Original Message-----
From: MIDRANGE-L<midrange-l-bounces@xxxxxxxxxxxxxxxxxx> On Behalf
midrange-l@xxxxxxxxxxxxxxxxxxMartijn van Breden
Sent: Thursday, 27 February 2025 16:18
To: Midrange Systems Technical Discussion <
reference.Subject: Re: When is SqlState reset?
Thanks for the reactions, I looked up documentation in the SQL
It states:
Considerations for SQLSTATE and SQLCODE SQL variables: When the
SQLSTATESQL-procedure-statement in the IF statement is executed, the
valuestatementand
clauseSQLCODE SQL variables reflect the result of evaluating thesearch-conditions
of that IF statement. If an IF statement does not include an ELSE
and
none of the search-conditions evaluate to true, then when the
that
follows the IF statement is executed, the SQLSTATE and SQLCODE SQLvariables
reflect the result of evaluating the search conditions of that IFstatement.
As I read it, it doesn't say that the IF itself is changing the
codeof
SQLSTATE, but that's what Javier found. I changed my production
liketo
tosave
the SqlState value in a seperate variable, but it still seems weird
SQLafterme.
I'll get in touch with IBM about the documentation
Kind regards
Martijn van Breden
lead software architect
My understanding is that SQLCODE and SQLSTATE are set to the result
each SQL statement, which probably means reset at the start of each
statement. Since this code is SQl procedural language, it looks
seemsIF
is
an SQL statement.
I just googled this and ran across and SAP question about it -
interestingthere
might have been a change in behavior there, and a change to what isreally
the SQL standard.conclusively to
Please remember that many things in SQL are the same in different
implementations, but I am not saying that SAP should be used
determine what is going on with IBM i - this was just an
seesimilarity.
--
*Regards*
*Vern Hamberg*
<cid:part1.VByWODzW.Wwn8nQpF@centurylink.net>
On 2/24/2025 9:09 AM, Javier Sanchez wrote:
Interesting. And yes! Weird.
I replicated your code and it's like you say.
I debugged the procedure using the "C" source code, and I could
inspectedyourthat, YES it changes the value of SQLSTATE to '00000' just after
didfirst "if", it does it using this:
memset(SQLSTATE, '0', 5);
Then it immediately also resets SQLCODE to zero.
Additionally, when you declared the SQLSTATE variable, it actually
not set it to blanks as your default clause specified. I
assuresit
expectedand it rather initialized it with the value '00000'.
This explains the "weird" behavior.
HTH
JS
El lun, 24 feb 2025 a las 8:07, Martijn van Breden (<
m.vanbreden@xxxxxxxxxxxxxxxxxxxxxxxxxx>) escribió:
Hi all
I ran into some peculiar piece of code that didn't do what I
Myand decided to run a small test.
The goal is to see if SqlState is being reset in an if-statement.
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
isit's not found (the value in sysdummy1 is 'Y'). My SqlState is
therefore 02000, I checked that. So, my subsequent if-statement
sothen
true.
In my mind the inner if-statement can't be true if the outer is,
value,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
messageemail:MIDRANGE-L@xxxxxxxxxxxxxxxxxxbut 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? It just seems weird...
Kind regards,
Martijn van Breden
lead software architect
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
mailing list To post a
subscribe,mailing--To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
oremail:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/midrange-l.
Pleasecontactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
related questions.
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
list
To post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxx To
subscribe,mailingrelatedunsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives at
https://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
questions.
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
list
To post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxx To
mailingmailingrelatedunsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives at
https://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
questions.
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
list
To post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxxrelated questions.
To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
mailinglist
relatedTo post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
questions.--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
listlistlist
To post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxxrelated questions.
To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
relatedTo post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
listquestions.--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
To post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxxrelated questions.
To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
--To post a message email:MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/midrange-l
or email:MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/midrange-l.
Please contactsupport@xxxxxxxxxxxxxxxxxxxx for any subscription related
questions.
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription related
questions.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.