最近添加的表列上的SQL“关键字附近的语法不正确”(编辑:列名是保留字)

时间:2018-10-15 13:31:43

标签: sql-server tsql

我的表包含以下列:

  • id(int)
  • errorDate(日期时间)
  • 数据nvarchar(100)
  • 函数(新添加的列,nvarchar(100))
  • fileName(与新添加的功能相同,尽管与此没有问题)

我正在使用Microsoft SQL Server 2008 R2(SP1)-10.50.2876.0(X64)2013年5月30日10:18:43版权所有(c)Windows NT 6.1(Build 7601)上的Microsoft Corporation Enterprise Edition(64位) :Service Pack 1)(管理程序)

和Microsoft SQL Server Management Studio 14.0.17277.0

我的查询是这样的:

contentEncoding

查询返回错误:

  

第15层状态1的第9行156消息   关键字“函数”附近的语法不正确。

2 个答案:

答案 0 :(得分:3)

function是T-SQL中的关键字。

尝试用方括号将其包围:

select * from dbo.InstallationErrorLog  
where data = 'Input string was not in a correct format.' 
   and ErrorDate > GETDATE()-90 
   and [Function] ='ProcessTblFromCSV'

答案 1 :(得分:1)

您需要在[]列周围加上方括号function

select * 
from dbo.InstallationErrorLog  
where data = 'Input string was not in a correct format.' and 
      ErrorDate > GETDATE()-90  and 
      [Function] ='ProcessTblFromCSV';

但是,不建议使用名称function,它是保留关键字。第二件事,我将在此处使用dateadd()而不是减法(-90)上的blog

因此,您可以改为:

select * 
from dbo.InstallationErrorLog  
where data = 'Input string was not in a correct format.' and 
      ErrorDate > DATEADD(DAY, -90,  GETDATE()) and 
      [Function] ='ProcessTblFromCSV';