On 29-Nov-2011 15:33 , TheBoss wrote:
> CRPence<
CRP...@vnet.ibm.com> wrote:
>
>> On 29-Nov-2011 09:08 , Graham Hobbs wrote:
>>
>> A given date less than current date... Really? I know the example
>> was taken almost directly from a prior offered link, but that is
>> some very specific logic included in an apparent attempt merely to
>> "validate" a[n unspecified bounds] date string.
>>
>
> No, it is just a clever way to validate that a certain variable is a
> valid date (or time or datetime); boundaries are not relevant at
> all.
I understand the concept. The chosen statement however, implies
something different to anyone who might review\read the coded SQL; even
with a visibly coded response to any SQL errors that might follow the
EXEC SQL. That was my point.... per "Really?", as in, why use something
so confusing?
> The SYSIBM.SYSDUMMY1 view isn't a real table/view, but a dummy
> one that resides in memory and makes it possible to perform
> arithmetic using special registers like "CURRENT_DATE" (and
> "CURRENT_TIME" / "CURRENT_TIMESTAMP") in SQL.
The DB2 for i provides and uses the actual TABLE, as does DB2 for z
from what I recall. Though given that description, I infer that perhaps
some DB2 might just rewrite such SELECT INTO statements as a VALUES INTO
instead, which could conceptually be described in the same way.?
Interesting, but I have never seen any documentation to support that
effect. The DB2 for i I know [and AFaIK still] performs the actual
query of the TABLE when\as requested, but no longer uses a dummy table
[or view] to implement the VALUES INTO statement several years and
releases earlier.
> In this case it is not even relevant which specific computation is
> done, as long as it (implicitly) contains a comparison of the
> host-variable to be validated with a variable/value known to be a
> correct date, time or datetime.
Yep. That is why the DATE() [cast] scalar of a 10-byte character
string could do the same. And without any comparison to, nor any reason
to evaluate, the CURRENT DATE special register.
> This comparison fails on a syntax-error (SQL-code "-181" in DB2 for
> zOS) when the host-variable isn't a valid date (or time or datetime).
> This means that the OP should catch the error (as said: -181 for DB2
> zOS, not sure about LUW, but this should be easy to find out).
Just seemed to me that anyone asking such novice questions about the
SQL would not be so clear about both ignoring the result [in the host
variable] and deferring to the SQLstate because SQLSTATE values would be
consistent due to standards; noting the reference above, to SQLcode
instead.?
This is also where using the comparison is ugly IMO, since a valid
date can effect one of two results for SQLcode\SQLstate, either non-zero
or zero. A valid date can effect either an SQLcode=100 [for a valid
date of today or future] or the SQLcode=0 [for any valid past date]
along with the selected integer value updated\into the host variable. I
would expect that someone asking such basic questions about the SQL
might just add a [presumably wrong\undesirable] test like "SQLcode<>0"
after the SELECT INTO. Similarly, the SQLstate.
Even if using SELECT versus VALUES INTO, dropping the WHERE clause
entirely would IMO be much prettier for a general validation. Using a
statement like either of SELECT DATE(:CharDate) INTO or SELECT
DAYS(:CharDate) INTO, each eliminates the confusing predicate and gives
either a valid result with a zero sqlcode\sqlstate or an error; plus,
AFaIK, an indicator variable could be used instead\additionally to test
validity, something which is not a consistent option for the "SELECT 1"
for the same variations on effects for the :HV.
>
>> The first error suggests the dummy table usage is unnecessary;
>> i.e. the VALUES INTO can be used instead. <<SNIP>>
>
> The reason for using SYSIBM.SYSDUMMY1 is that it is compatible
> between platforms (at least for DB2 zOS vs. DB2 LUW, not sure about
> DB2/400). I'm not so sure this is true for VALUES and the DAYS- and
> DATE- functions.
>
The VALUES INTO statement and those scalars exist for every member of
the DB2 family according to any [even several years old] documentation I
have seen.
Regards, Chuck