Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select table with column named "index"

I have a table with a column name called "index"

select id, index
from item;

gives an error:

Msg 1018, Level 15, State 1, Line 1 Incorrect syntax near 'index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.

How can I do a select on a column named index? I'm using sqlserver 2008-R2

like image 253
NomenNescio Avatar asked Dec 05 '11 15:12

NomenNescio


People also ask

How do I select columns for indexing?

Columns with one or more of the following characteristics are good candidates for indexing: Values are unique in the column, or there are few duplicates. There is a wide range of values (good for regular indexes). There is a small range of values (good for bitmap indexes).

How do I find an index on a specific table?

To see the index for a specific table use SHOW INDEX: SHOW INDEX FROM yourtable; To see indexes for all tables within a specific schema you can use the STATISTICS table from INFORMATION_SCHEMA: SELECT DISTINCT TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.

Does SQL in clause use index?

The IN clause becomes an equality condition for each of the list and will use an index if appropriate.


2 Answers

Use square brackets to quote reserved words:

select id, [index]
from item

See also the documentation on Delimited Identifiers.

like image 154
Mark Byers Avatar answered Oct 19 '22 02:10

Mark Byers


Put reserved words in brackets:

select id, [index]
from item
like image 7
Garrett Vlieger Avatar answered Oct 19 '22 00:10

Garrett Vlieger