views:

292

answers:

1

I have written 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

The inner query is selecting rows from MY_TABLE using a bounding box. The outer queries are included to enable paging for the results.

For some odd reason this query does not yield any results. If I try and run the 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 yields 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 is preventing the query from yielding any results. If I remove rownum the results are 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..

+1  A: 
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

See this article.

Vadim K.
It seems to be working! But why?? My query is running fine when I am not using SDO_ANYINTERACT. It seems really strange.
Jesper Kihlberg
Have a look at the article I mention in the solution. It offers an excellent explanation of what's happening here.
Vadim K.
Vadim, I can't see anything in the article that explains this either - can you elaborate?
Tony Andrews
From all appearances, Jesper's original code should work. After some investigation, it appears that (at least on my system) the subquery which introduces `rownum` is causing Oracle to throw internal exception `ORA-00600`, to which Oracle's recommended remedy is to report it to them as a bug.
Vadim K.
At work, my development server (10gR2 with patches) executes Jesper's query without a problem. It may be that one of the Oracle patch sets resolves this problem.
Vadim K.