INSERT INTO 语句中的语法错误,没有给出其他详细信息

Posted

技术标签:

【中文标题】INSERT INTO 语句中的语法错误,没有给出其他详细信息【英文标题】:Syntax Error in INSERT INTO statement, no other details given 【发布时间】:2014-03-30 15:25:16 【问题描述】:

生成我的代码的 SQL 如下所示 -

INSERT INTO [Item] (
  Vendor_num
, item
, [Desc]
, Pack
, item_size
, unit_of_sale
, allowance
, points
, Promo
, WS_Cost
, WS_Unit_cost
, Retailer_Price
, Retail
, Cust_Use
, Show_Price
, GP
, Min
, Max
, Book_Number
, Del1_date
, Del2_date
, Del3_date
, Del4_date
, Del5_date
, Del6_date
, Del7_date
, Del8_date
, Del9_date
, Del10_date
, Del11_date
, Del12_date
, Del13_date
, Del14_date
, Del15_date
, warehouse
, Purchase_Group
, GPM
, Material_Number
, PM
) VALUES (
  '11'
, '300681'
, 'item description'
, '60'
, 'BOX'
, 'BOX'
, '3'
, '20'
, 'Y'
, '0'
, null
, '0'
, null
, '03652'
, null
, null
, null
, null
, '832'
, '6/2/2014 12:00:00 AM'
, '6/30/2014 12:00:00 AM'
, '7/28/2014 12:00:00 AM'
, '8/18/2014 12:00:00 AM'
, null
, null
, null
, null
, null
, null
, null
, null
, null
, null
, null
, '1'
, null
, null
, null
, null
)

(原谅所有那些null 值,它们在这些插入中通常不为空,因此当它们不为空时,语句需要足够灵活)

我遇到了一般性错误 --

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error in INSERT INTO statement.

直接复制到 Access 中的新查询中时,该语句工作正常,但通过 .NET 和 OLE DB 触发时,它会失败。

有什么想法吗?

【问题讨论】:

我对 Access 几乎一无所知,但我会尝试使用 [Min][Max] 而不是不加引号地使用它们。 日期分隔符是#,而不是',所以#6/2/2014 12:00:00 AM# 虽然我同意@JoachimIsaksson 的观点,即 [] 可以解决问题,但我建议您开始使用表名或更好的表别名作为字段/列名的前缀。这将防止任何保留字错误,更具可读性,并且可以很好地升级到其他数据库。 @Remou 我真的希望 Access 在这方面更加一致。我同意 # 是要使用的东西,但我刚刚测试过,单引号也可以正常工作。 @Brad 这是新的,是的,很烦人。 【参考方案1】:

MinMax 是reserved words in Access,如果使用它们作为字段名,则应加引号,否则可能会遇到错误;

如果保留字已被使用,您可以通过用方括号 ([ ]) 将每个出现的字括起来来避免错误消息。但是,最好的解决方案是将名称更改为非保留字。

也就是说,作为字段名使用时,应该用[Min][Max]引用。

【讨论】:

以上是关于INSERT INTO 语句中的语法错误,没有给出其他详细信息的主要内容,如果未能解决你的问题,请参考以下文章

使用 DateTime 时 INSERT INTO 语句中的语法错误

为啥会出现“INSERT INTO 语句中的语法错误”?

“INSERT INTO 语句中的语法错误”错误 - 错误是啥?

将记录添加到 Access 数据库时出现“INSERT INTO 语句中的语法错误”

insert into语句的语法错误

INSERT INTO 语句 Visual Basic 中的语法错误