Querying SQL Tables Named With SQL Keywords

When you have any of your SQL DB table named with any of the SQL keyword, SQL will error as shown below.  In the example, I am using a table named User which is also a SQL keyword.

Query: select * from dbo.User (nolock) where Name like ‘%test%’

Error:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword ‘User’.

The trick to use the SQL reserved/keyword in your SQL names you need to enclose the name within either braces [] or double quotes “” as shown below.

Query: 

select * from dbo.[User] (nolock) where Name like ‘%test%’

select * from dbo.”User” (nolock) where Name like ‘%test%’

Leave a Reply

Your email address will not be published. Required fields are marked *