INSERT INTO 语句的语法错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了INSERT INTO 语句的语法错误相关的知识,希望对你有一定的参考价值。

Stemp = "INSERT INTO 客户资料"
Stemp = Stemp & "(客户ID,公司名称,联系人姓名,联系人职务,公司地址,"
Stemp = Stemp & "城市,地区,邮编,国家,电话,传真,备注)"
Stemp = Stemp & "VALUES ('" & Me![客户ID] & "','" & Me![公司名称] & "',"
Stemp = Stemp & "'" & Me![联系人姓名] & "','" & Me![联系人职务] & "',"
Stemp = Stemp & "'" & Me![公司地址] & "','" & Me![城市] & "',"
Stemp = Stemp & "'" & Me![地区] & "','" & Me![邮编] & "',"
Stemp = Stemp & "'" & Me![国家] & "','" & Me![电话] & "',"
Stemp = Stemp & "'" & Me![传真] & "','" & Me![备注] & "')"

建议你用最容易的方法,就是用数据库自带的跟踪器去跟踪(SQL2000叫时间探测器;SQL 2005是SQL Server Profiler)先启动探测器,然后运行你的插入动作程序,你就可以在探测器上找到这条对数据库操作的插入语句,你就可以发现插入失败在哪里,或者复制到查询分析器里去试试就知道哪里错了。
如果跟踪的数量很大,SQL2000需要打上SP3补丁;
事件探测器其实是很有用的,对于调试程序,希望对你有用。
参考技术A 看着语法没问题,不过看一下,你的数据库与insert里的输入的数值是不是有类型不一致的冲突,还有就是客户ID在数据库里是不是自增型字段,如果还是不行,能否考虑一下把数据库格式贴上来

Access 2010 中的 INSERT INTO 语句中出现 SQL 语法错误

【中文标题】Access 2010 中的 INSERT INTO 语句中出现 SQL 语法错误【英文标题】:Getting SQL Syntax Error in INSERT INTO statement in Access 2010 【发布时间】:2014-06-12 09:10:55 【问题描述】:

我在 Access 2010 VBA 中编写了以下 Insert Into 语句:

Private Sub AddBPSSButton_Click()

'    CurrentDb.Execute "INSERT INTO TabClearDetail(C_Site) VALUES(" & Me.C_Site & ")"

Dim strSQL As String

'MsgBox Me.[Clearance Applying For]
'MsgBox Me.[Contract Applying for]
'MsgBox Me.[C_Site]
'MsgBox Me.[C_SponsorSurname]
'MsgBox Me.[C_SponsorForename]
'MsgBox Me.[C_SponsorContactDetails]
'MsgBox Me.[C_EmploymentDetail]
'MsgBox Me.[C_SGNumber]
'MsgBox Me.[C_REF1DateRecd]
'MsgBox Me.[C_REF2DateRecd]
'MsgBox Me.[C_IDDateRecd]
'MsgBox Me.[C_IDNum]
'MsgBox Me.[C_CriminalDeclarationDate]
'MsgBox Me.[Credit Check Consent]
'MsgBox Me.[C_CreditCheckDate]
'MsgBox Me.[Referred for Management Decision]
'MsgBox Me.[Management Decision Date]
'MsgBox Me.[C_Comment]
'MsgBox Me.[C_DateCleared]
'MsgBox Me.[C_ClearanceLevel]
'MsgBox Me.[C_ContractAssigned]
'MsgBox Me.[C_ExpiryDate]
'MsgBox Me.[C_LinKRef]
'MsgBox Me.[C_OfficialSecretsDate]

strSQL = "INSERT INTO TabClearDetail(Clearance Applying For, Contract Applying for, " & _
"C_Site, C_SponsorSurname, C_SponsorForename, C_SponsorContactDetails, C_EmploymentDetail, " & _
"C_SGNumber, C_REF1DateRecd, C_RED2DateRecd, C_IDDateRecd, C_IDNum, " & _
"C_CriminalDeclarationDate, Credit Check Consent, C_CreditCheckDate, Referred for Management Decision, " & _
"Management Decision Date, C_Comment, C_DateCleared, C_ClearanceLevel, C_ContractAssigned, " & _
"C_ExpiryDate, C_LinkRef, C_OfficialSecretsDate) VALUES('" & Me.[Clearance Applying For] & "', " & _
"'" & Me.[Contract Applying for] & "', '" & Me.[C_Site] & "', '" & Me.[C_SponsorSurname] & "', " & _
"'" & Me.[C_SponsorForename] & "', '" & Me.[C_SponsorContactDetails] & "', " & _
"'" & Me.[C_EmploymentDetail] & "', '" & Me.[C_SGNumber] & "', '" & Me.[C_REF1DateRecd] & "', " & _
"'" & Me.[C_REF2DateRecd] & "', '" & Me.[C_IDDateRecd] & "', '" & Me.[C_IDNum] & "', " & _
"'" & Me.[C_CriminalDeclarationDate] & "', '" & Me.[Credit Check Consent] & "', '" & Me.[C_CreditCheckDate] & "', " & _
"'" & Me.[Referred for Management Decision] & "', '" & Me.[Management Decision Date] & "', " & _
"'" & Me.[C_Comment] & "', '" & Me.[C_DateCleared] & "', '" & Me.[C_ClearanceLevel] & "', " & _
"'" & Me.[C_ContractAssigned] & "', '" & Me.[C_ExpiryDate] & "', '" & Me.[C_LinKRef] & "', " & _
"'" & Me.[C_OfficialSecretsDate] & "');"

DoCmd.RunSQL (strSQL)

'MsgBox strSQL

End Sub

所有 MsgBox 调用都有效,所以我相信我已经正确输入了所有列名和文本框名。当我到达 DoCmd.RunSQL 行时出现语法错误。已经盯着这个看了很长一段时间,想看看我是否漏掉了一个逗号或语音标记或其他东西,但我希望另一双眼睛会看到我的错误。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

看到MsgBox strSQL的输出我认为会有所帮助 请注意,在您更改 SQL 语句以将方括号括在包含空格的字段名称周围并让语句运行之后,它将mangle您的日期。 30/06/2014 将被解释为“2014 年 6 月 30 日”,但 03/06/2014 将被解释为“2014 年 3 月 6 日”,而不是“2014 年 6 月 3 日”。 【参考方案1】:

当您的对象名称中有空格时,您需要将全名括在括号中。那么你在哪里:

INSERT INTO TabClearDetail(Clearance Applying For, Contract Applying for,...

应该是

INSERT INTO TabClearDetail([Clearance Applying For], [Contract Applying for],...

出于这个原因,我个人不能忍受对象名称中的特殊字符,如果还不算太晚,您应该考虑重命名列。对于复合名称,Pascal Case 通常足够清晰,例如清关申请。或在分数下使用 - Clearance_Applying_For。

我还认为 Access 中的约定是使用 # 而不是单引号来限定日期,即#01/01/2014#,而不是 `01/01/2014'

【讨论】:

我已经这样做了,注意到它应该是 C_REF2DateRecd 而不是 C_RED2DateRecd【参考方案2】:

您可以尝试使用“#”而不是“'”将日期字段括起来,或者更改数据库的“SQL Server 兼容语法”属性。

【讨论】:

以上是关于INSERT INTO 语句的语法错误的主要内容,如果未能解决你的问题,请参考以下文章

INSERT INTO 语句的语法错误。

INSERT INTO 语句的语法错误

INSERT INTO 语句的语法错误。

insert into语句的语法错误

insert into 语法错误

执行 INSERT INTO 语句时出现语法错误