Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Quotes in Oracle Column Aliases

Ok, this is bit of an obscure question, but hopefully someone can help me out with it.

The system I'm working on builds a dynamic SQL string for execution inside a stored procedure, and part of that dynamic SQL defining column aliases, which themselves are actually values retrieved from another table of user generated data.

So, for example, the string might look something like;

SELECT table1.Col1 AS "This is an alias" FROM table1

This works fine. However, the value that is used for the alias can potentially contain a double quote character, which breaks the outer quotes. I thought that I could maybe escape double quotes inside the alias somehow, but I've had no luck figuring out how to do so. Backslash doesn't work, and using two double quotes in a row results in this error;

SQL Error: ORA-03001: unimplemented feature
03001. 00000 -  "unimplemented feature"
*Cause:    This feature is not implemented.

Has anyone had any experience with this issue before? Cheers for any insight anyone has.

p.s. the quotes are needed around the aliases because they can contain spaces.

like image 384
Chris McAtackney Avatar asked Sep 11 '08 13:09

Chris McAtackney


People also ask

Can alias be enclosed in double quotes?

Enclose the alias in single or double quotes if it's a reserved keyword or if it contains spaces, punctuation, or special characters. You can omit the quotes if the alias is a single non-reserved word that contains only letters, digits, or underscores.

Can we use double quotes in Oracle?

Double quotes in Oracle Double-quotes are used to enclose identifiers like table name/table alias or column name/column alias. They are rarely used when the name doesn't need to conform to Oracle Database Object Naming Rules. Using double-quotes to represent identifiers is not recommended.

Does Oracle Use single or double quotes?

Answer: Now it is first important to remember that in Oracle, you enclose strings in single quotes. The first quote denotes the beginning of the string and the second quote denotes the termination of the string.

How do you handle double quotes in SQL query?

Use two single quotes to escape them in the sql statement. The double quotes should not be a problem: SELECT 'How is my son''s school helping him learn?


2 Answers

Can you just put another character instead of double quotes and replace that with double quotes in the code?

Something like this:

SELECT table1.Col1 AS "This is |not| an alias" FROM table1

Then just replace | with ".

I know it's a hack, but I can't think of any better solution... And what you are doing there is a hack anyway. The "nice" way would be to select the values and the column names separately and associate them in your code. That would make things much cleaner.

like image 188
ibz Avatar answered Oct 23 '22 15:10

ibz


use the Oracle quote operator:

select q'#someone's quote#' from dual;

the '#' can be replaced by any character

like image 39
swissunix Avatar answered Oct 23 '22 16:10

swissunix