1
SELECT Id, to, Subject, Body, DateCreated, DateSent 
FROM Emails

Gives the following error:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'to'.

Any idea why?

TO is highlighted, so I guess, it is because it thinks that is a keyword, anyway to prevent this?

Cheers.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bobo2000
  • 1,779
  • 7
  • 31
  • 54
  • Also see: http://stackoverflow.com/questions/2901453/sql-standard-to-escape-column-names – Oded Apr 10 '12 at 12:36

2 Answers2

5

Yes, put to in brackets like this [to]

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
  • 2
    Or alternatively if you want to be compatible to standard ANSI SQL use double quotes: "to" –  Apr 10 '12 at 12:35
  • You can also not pick such inane words to be field names. – JNK Apr 16 '12 at 17:25
0

You escape it by surrounding it with [] (SQL Server specific):

SELECT Id,[to],Subject,Body,DateCreated,DateSent 
FROM Emails

You can also use the standard SQL escape, " as detailed here:

SELECT Id,"to",Subject,Body,DateCreated,DateSent 
FROM Emails
Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009