insert into语句的语法错误

Posted

tags:

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

cpu=Request("select")
grade=Request("select3")
department=Request("select4")
timepd=Request("select2")
money=Request("select5")
needs=Request("select6")
houhui=Request("select7")
thing=Request("select8")
timegj=Request("select9")
gfs=Request("select10")

sql="insert into stu(cpu,grade,department,timepd,money,needs,houhui,thing,timegj,gfs) values('" & cpu & "','" & grade & "','" & department & "','" & timepd & "','" & money & "','" & needs & "','" & houhui & "','" & thing & "','" & timegj & "','" & gfs & "')"
conn.execute sql

我也试过,插入语句格式是正确的(因为是从另外一个能运行的程序拷过来的),在放在我的文件中就说“INSERT INTO 语句的语法错误”
但后来解决了,方法是将表和字段名都加上中括号,就是改成这样:
sql="insert into [stu]([cpu],[grade,department],[timepd],[money],[needs],[houhui],[thing],[timegj],[gfs]) values('" & cpu & "','" & grade & "','" & department & "','" & timepd & "','" & money & "','" & needs & "','" & houhui & "','" & thing & "','" & timegj & "','" & gfs & "')"
我用的是c#
参考技术A now

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 语句时出现语法错误