Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2010-11-22
7
686 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 75

Accepted Solution

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

Expert Comment

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

Expert Comment

by:Naveen Kumar
ID: 34194297
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 143

Expert Comment

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

Expert Comment

by:Naveen Kumar
ID: 34194594
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
ID: 34194915
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
ID: 34195132
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jquery + C#.net radiobuttonlist 3 30
Generate Unique ID in VB.NET 21 65
oracle forms question 22 42
Input parameteres to DragOver 2 20
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

829 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