Using OracleCommand with a column alias

1.7k Views Asked by At

I am attempting to write an oracle query that the client has requested a specific column name for the output. Simple enough, I write the query using an oracle column alias using double quotes. (Note that I am using the quotes because it just so happens that the requested column name is a reserved word.)
The issue is that I am receiving the from the error ORA-00911: invalid character when I attempt to execute the query using the following code:

OracleCommand command = new OracleCommand(sqlquery, connection.Conn);
dt.Load(command.ExecuteReader());

I have tried the oracle quote operator and the various iterations of the c# string literal (i.e. @"this is ""testing"" the literal" and "this is \"testing\" the literal").

The query is simple in that I would like to do this:

select column1 as "alias1" from dual;

This works when I run it directly in Oracle but I am unable to get it to run via the .Net application. Having escaped the double quotes and viewing the query in the debugger shows that it has been formatted as:

select column1 as \"alias1\" from dual;

It seems ¿obvious? that the backslashes are causing the Oracle error but how do I get around this?

1

There are 1 best solutions below

0
On

Wow. After nearly an hour I discovered something stupid. Let this be a warning to the rest of you. Don't leave the semicolon in the string literal for your query.

In other words this is fine ...

string query = @"select column1 as ""alias1"" from dual";

... but this is not ...

string query = @"select column1 as ""alias1"" from dual;";

Note the small addition of the ; in the second example.

I hope this helps someone else save some time.