1

How to programmatically insert a row/record into a table (generated by third party software) where one of the fields is named when and user which are coincidentally sql keywords.

The sql command is:

INSERT INTO APPT 
(ApptID,Type,Descrip,When,Flag,User,DTS) 
VALUES 
(41,0,'Test Note','20/10/2014 1:42:00 PM',16,'AP','22/10/2014 8:24:01 AM') 

Which gives the errors:

Error 156: Incorrect syntax near the keyword 'When'.
Error 156: Incorrect syntax near the keyword 'User'.
Serge P
  • 1,591
  • 8
  • 22
  • 42

2 Answers2

4

To escape reserved keywords in the name of a column (or table), put the name of the column in square brackets:

INSERT INTO APPT (ApptID,Type,Descrip,[When],Flag,[User],DTS) 
Z .
  • 12,657
  • 1
  • 31
  • 56
2

All the SQL keywords should be kept within the square brackets.

INSERT INTO APPT 
(ApptID,Type,Descrip,[When],Flag,[User],DTS) 
VALUES 
(41,0,'Test Note','20/10/2014 1:42:00 PM',16,'AP','22/10/2014 8:24:01 AM')