In Oracle, why can't I select rownum in an external query when my internal query contains SDO_ANYINTERACT?

I wrote a query in Oracle that looks like this:

select ID, NAME, GEOMETRY from 
(
    select a.*, rownum as rnm from
    (
        select ID, NAME, GEOMETRY from MY_TABLE
        where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
        order by NAME asc
    ) a
)
where rnm <= 50 and rnm >= 40

An internal query selects rows from MY_TABLE using a bounding box. External queries are included to enable paging for results.

For some odd reason, this query does not return any results. If I try to run a subquery:

select ID, NAME, GEOMETRY from MY_TABLE
where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
order by NAME asc

It gives a list of results as expected. If I run the subquery:

select a.*, rownum as rnm from
(
    select ID, NAME, GEOMETRY from MY_TABLE
    where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
    order by NAME asc
) a

the result set is empty. Somehow rownum interferes with the query to give any results. If I remove rownum, the results will be returned as in the innermost query:

select a.* from
(
    select ID, NAME, GEOMETRY from MY_TABLE
    where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
    order by NAME asc
) a

What am I doing wrong here? I am running Oracle 10g ..

+3
1
with
    my_query as
    (
        select ID, NAME, GEOMETRY from MY_TABLE 
        where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE' 
        order by NAME asc 
    )
select *
from
    (
        select /*+ FIRST_ROWS(n) */  my_query.*, rownum rnum
        from my_query
        where rownum <= :last_row_to_fetch
    )
where
    rnum >= :first_row_to_fetch

.

+1

Source: https://habr.com/ru/post/1728619/


All Articles