Solved

Getting ORA-00911:invalid character when using "ORDER BY" in dynamic SQL statement using Oracle.DataAccess

Posted on 2010-11-22
7
682 Views
Last Modified: 2012-05-10
Hi:

I'm running into some issues using the Oracle.DataAcess.Client.OracleAdapter.
If my dynamic sql contains an "ORDER BY" clause, then I get the following exception:
ORA-00911:invalid character



Here is a coding snippet outlining the issue:
-------------------------------------------------------
using (OracleConnection oraconn = new OracleConnection(dataConn))
{
    oraconn.Open();

    Oracle.DataAccess.Client.OracleCommand cmd = new OracleCommand(sOraSQL, oraconn);
    cmd.CommandType = CommandType.Text;
    Oracle.DataAccess.Client.OracleDataAdapter da = new OracleDataAdapter(cmd);

    DataTable dt = new DataTable();
    da.Fill(dt);

    oraconn.Close();
    return dt;
}

If I use the following SQL string, it works fine.
----------------------------------------------------------
String sOraSQL = "SELECT a, b from table_foo;"



If I add an "ORDER BY" clause, I get the exception
----------------------------------------------------------
String sOraSQL = "SELECT a, b from table_foo ORDER BY a ASC;"




I'm using the following reference in my solution:
------------------------------------------------------------
Oracle.DataAccess
Runtime Version V1.0.3705
Version 10.2.0.100

0
Comment
Question by:jxbma
7 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Remove the semi-colons.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Without the trailing ';' in the statement - does that cure it?
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
Can you try this :

String sOraSQL = "SELECT a, b from table_foo ORDER BY 1 ;"  

--> no need of ASC keyword because default is ASC only

If it still does not work, can you try it without the semicolon at the end.

String sOraSQL = "SELECT a, b from table_foo ORDER BY 1"

Thanks

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
I also must presume that the ";" in the sql should be removed ...
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
But surprising thing is the below from question author.

If I use the following SQL string, it works fine.
----------------------------------------------------------
String sOraSQL = "SELECT a, b from table_foo;"

--> ; is still present in the above as well.  I can think of a typo while typing here or something like that.

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
One other thought which might not be relevant.
I assume a is a placeholder but what is the actual name?  Maybe it requires square braces because it contains an invalid character (though why it is OK in the SELECT part is beyond me - unless it doesn't actually appear in the select part)

ie.
String sOraSQL = "SELECT a, b from table_foo ORDER BY [Column One] ASC;"


because
String sOraSQL = "SELECT a, b from table_foo ORDER BY Column One ASC;"
is giving it problems
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
Got something of this sort in mind and hence suggested to use "order by 1" as posted in my previous updates.

thanks,
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now