Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Handle Table Column Named With Reserved Sql Keyword?

Tags:

sql

oracle

I have a table that has a column named RANK which is a keyword in Oracle.

Now I need to insert data in this table :

insert into mytbl (RANK)
select RANK from other_table

when executing this query I got the following error :

ORA-00907: missing right parenthesis

How does one escape a keyword?

like image 495
Radi Avatar asked Jul 24 '12 11:07

Radi


People also ask

How do you use SQL reserved words as column names?

Using Keywords in Tables or Columns Generally, SQL developers advise against using reserved words. However, if you want (or need) to use a reserved word as a table or column name, you can place the respective identifier in special quotation marks, so-called backticks (` `), in the SQL statements.

Can we use SQL keywords as column name?

In SQL, certain words are reserved. These are called Keywords or Reserved Words. These words cannot be used as identifiers i.e. as column names in SQL.

How do you use reserved words in SQL query?

Within SQL certain words are reserved. You cannot use an SQL reserved word as an SQL identifier (such as the name for a table, a column, an AS alias, or other entity), unless: The word is delimited with double quotes ("word"), and. Delimited identifiers are supported.

How do you escape a reserved keyword in SQL?

To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').


1 Answers

Oracle uses double quotes " to escape reserved words.

insert into mytbl ("RANK")
select "RANK" 
from other_table

One other note, Oracle requires correct case as well.

like image 95
Taryn Avatar answered Oct 13 '22 09:10

Taryn