On 25 Oct 2012 05:47, Franco Lombardo wrote:
> I'm trying to support AS400 database in Activiti
> (
http://www.activiti.org).
> This open source project creates lots of query like this:
>
> SELECT SUB.*
> FROM
> (select RES.* , row_number() over (Order by A.field1) rnk
> FROM (select A.field1, B.field1
> from A inner join B on A.X = B.Y
> ) RES
> ) SUB
> WHERE SUB.rnk >= ? AND SUB.rnk < ?
>
> Unfortunately this query fails with msg SQL5001 - Column qualifier
> or table A undefined.
>
> Note that if I change my statement this way:
>
> SELECT SUB.*
> FROM
> (select RES.* , row_number() over (Order by NEW_NAME) rnk
> FROM
> (select A.field1 NEW_NAME, B.field1
> from A inner join B on A.X = B.Y
> ) RES
> ) SUB
> WHERE SUB.rnk >= ? AND SUB.rnk < ?
>
> All works fine (but I can't modify Activiti this way :-).
> Is it a bug of SQL parser or I don't understand something?
The scope of the correlation-name as qualifier prevents an outer
[higher level] query from "seeing" fields using those [qualified] names
of the inner [lower level; further nested] queries. The scope of the
correlation name\identifier "A" is available only in the deepest nested
query, from the given example. So although apparently the use of the H2
SQL by that software enables [though seemingly improperly] that SQL
SELECT query to run without error, the DB2 for i SQL seems in my opinion
to be *correctly diagnosing* the "undefined qualifier" condition.
The failing query is additionally problematic for any reference to an
unqualified name FIELD1 by the /ranking/ query [the query of the NTE
using table designator RES], because the use of "FIELD1" would be
ambiguous. In that case, the SQL0203 diagnostic condition is expected,
instead of the SQL5001. Using the expected qualified reference for
"OVER (ORDER BY RES.FIELD1)" is ambiguous because there are two FIELD1
column references in the derived table.
The second\modified query resolves those issues by giving a unique
name to the column\expression of the nested query (NTE).
FWiW: An alternate syntax for [re]naming the columns of the "RES"
named\designated NTE would be to specify a column-list [AKA a "list of
column names" or a "column name list"] after the "AS RES" correlation
identifier:
SELECT SUB.*
FROM
(select RES.* , row_number() over (Order by NEW_NAME) rnk
FROM (select A.field1, B.field1
from A inner join B on A.X = B.Y
) RES ( NEW_NAME, FIELD1_TOO )
) SUB
WHERE SUB.rnk >= ? AND SUB.rnk < ?
In the above revised query, the "exposed names" for the RES
table-reference are NEW_NAME and FIELD1_TOO whereas the _qualified_
exposed names are RES.NEW_NAME and RES.FIELD1_TOO which were simply
qualified with the correlation-name specified for that table-reference
[i.e. for the Nested Table Expression or Derived table with
correlation-name "RES"].
> Any suggestion?
Assuming my conclusion about the correctness of the IBM DB2 for i SQL
is functioning properly, then...
If indeed "can't modify" describes the situation, then ask that the
providers of Activiti correct the SQL in the version of the software
that will be downloaded and used. They have community\forum links at
the given URL, where presumably one could start such a conversation.?
Instead, assuming some evidence can be found to support the presumed
effectiveness of the failing syntax in any flavor of DB2 or the ANSI SQL
specifications, then open an issue with your service provider inquiring
why the DB2 for i SQL does not allow that syntax.
--
Regards, Chuck