Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ROWNUM returns as "invalid identifier"

Tags:

sql

oracle

odac

I am running a very basic select against an oracle database (not sure of the version).

SELECT * FROM ACCOUNTING WHERE ID = 123456 ORDER BY DATE

I want to return only the most recent record. So I have tried ...

SELECT ROWNUM, * FROM ACCOUNTING WHERE ID = 123456 ORDER BY DATE
SELECT * FROM ACCOUNTING WHERE ID = 123456 AND ROWNUM < 2 ORDER BY DATE

I get the same result every time ...

Error Source: System.Data.OracleClient    
Error Message: ORA-00904: "ROWNUM" : invalid identifier

Everything i see and read suggests that this should work. Can anyone see what I am missing? Could this be an issue with the driver? I am using the following package ... (Oracle ODAC 11.20.30 x64)

UPDATE

Thank you for all your replies ... I apologize for any confusion I created in my efforts to simplify the scenario. The ODAC driver is actually breaking the query out and formatting it for me, so what I originally posted is not exactly the query being run ... here's, specifically what the driver is spitting out that is generating the error ...

SELECT "ROWNUM", ID, SITE_ID, READING_DATE, SUBMITTED_DATE, DEPOSIT_DATE
FROM    ACCOUNTING
WHERE   (SITE_ID = 33730)
ORDER BY READING_DATE

And for my second attempt ...

SELECT ID, SITE_ID, READING_DATE, SUBMITTED_DATE, DEPOSIT_DATE
FROM    ACCOUNTING
WHERE   (SITE_ID = 33730) AND ("ROWNUM" < 2)
ORDER BY READING_DATE
like image 490
Gary O. Stenstrom Avatar asked Jan 08 '23 04:01

Gary O. Stenstrom


1 Answers

Your actual query might be using ROWNUM within double quotes. Otherwise, this error is not possible.

Though your first query would be ORA-00936: missing expression

select * from dual WHERE "ROWNUM" < =3;

Error report -
SQL Error: ORA-00904: "ROWNUM": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:

ROWNUM is a pseudo-column and it is like function without parameters.. and by the way "ROWNUM" makes oracle to search for such a column in your table..

Quoted identifiers when is a Oracle reserved keyword, would surpass its original purpose, and behaves as user defined column.

Unsure, of how to stop the Query builder to interpret this way. I would consider this a BUG.

like image 98
Maheswaran Ravisankar Avatar answered Jan 10 '23 19:01

Maheswaran Ravisankar