tags:

views:

87

answers:

4

Is there a SQL standard to escape a column name? If not what works for mysql and sqlite? does it also work for sqlserver?

A: 

For MySQL, use ticks `.

For instance:

SELECT `column`, `column2`, FROM `table`
Kerry
+2  A: 

For MS SQL use [ and ]

SELECT [COLUMN], [COLUMN 2] FROM [TABLE]
BoltBait
you can have spaces in column names with ms sql!?!
acidzombie24
you can! I wouldn't, though.
JMP
Yes, you can have spaces or reserved words as entity names in MSSQL. You just need to [ ] them.
BoltBait
+3  A: 

The SQL-99 standard specifies that double quote (") is used to delimit identifiers.

Oracle, PostgreSQL, MySQL, MSSQL and SQlite all support " as the identifier delimiter (though they don't all use " as the 'default' - for example, you have to be running MySQL in ANSI mode and SQL Server only supports it when QUOTED_IDENTIFIER is ON.)

Dean Harding
A: 

According to SQLite,

  • 'foo' is an SQL string
  • "foo" is an SQL identifier (column/table/etc)
  • [foo] is an identifier in MS SQL
  • `foo` is an identifier in MySQL

I'm not sure if MS SQL and MySQL support the standard "foo".

tc.