Arjun Bora
Arjun Bora

Reputation: 437

What is the universal method to escape keywords in SQL query?

If my query is

SELECT from FROM myTable;

How can I escape column name 'from'.

I need a way which is applicable to Oracle, MySQL, Teradata and all other systems

Will

SELECT myTable.from FROM mytable; 

work on all systems?

Upvotes: 2

Views: 5105

Answers (2)

user330315
user330315

Reputation:

The SQL standard is pretty clear: reserved keywords can only be used when they are enclosed in double quotes:

select "FROM" from mytable;

Note that a quoted identifier are also case sensitive. "Foo" and "FOO" are two different names! (whereas Foo and FOO are the same names in standard SQL and most DBMS)


I need a way which is applicable to Oracle, MySQL, Teradata and all other systems

Postgres, Oracle, DB2 (LUW), Firebird, Ingres, HSQLDB, H2, Vertica, HANA and Teradata comply with the standard out of the box.

MySQL needs to convinced to respect the standard by setting sql-mode to ANSI or at least ANSI_QUOTES.

For SQL Server the option QUOTED_IDENTIFIER needs to be set to on.

For Informix you need to set DELIMIDENT to y when connecting through JDBC


But in general you should really avoid the need to use quoted identifiers. Typically you will get in trouble sooner or later by doing that.

Upvotes: 3

Rahul
Rahul

Reputation: 77846

Universal method??? not sure about any such but double quote "" is ANSI standard other than that every RDBMS has some specific way of doing so. Moreover you don't need any such universal way if you refrain yourself from using reserve word or keyword for table or column name .. as already suggested in comment

Upvotes: 0

Related Questions