OpenQuery to a linked server that uses reserved words

231 Views Asked by At

Here is an issue that annoys me but I also am hoping someone out there can provide a resolution. We have a third party vendor/app that handles one aspect of our transactions. I am using OpenQuery to try and query this table via the linked server. Here's the catch (and a source of frustration)- the table I am trying to query has over 110M records and uses reserved words as column names (Date, Time). Specifically I am trying to query a subset based on date so that I don't have to try and pull the entire table each time and then subset it after pulling the entire thing but trying to use a where clause that references the date column, for example, is posing issues because of the reserved word.

Anyone have a solution to this? Thanks in advance.

1

There are 1 best solutions below

0
Dan Sorensen On

T-SQL uses brackets [] around reserved words and names with illegal characters such as a dash or space.

You can alias the column names to make it easier if needed. You can safely use brackets around many additional words if you are unsure.

SELECT t.[Date] AS ItemDate, 
       t.[Time],
       t.[Event] AS Evt 
FROM dbo.ImportTable t
WHERE ItemDate > '01-01-2020'
AND [t].[Time] IS NOT NULL

Example query provided to show flexibility in the syntax.