vb sql insert into 语句语法错误 求解

Posted

tags:

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

Private Sub Command1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
datafile = App.Path & "\数据库.mdb"
cn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & datafile
cn.Open
sql = "insert into 临时用表 (序号,名称) valuess (5, 4)"
cn.Execute sql
cn.Close
End Sub

数据都是数字型
如果是字符串型呢?如果是变量呢,该怎么写啊

sql = "insert into 临时用表 (序号,名称) valuess (5, 4)"

改为
sql = "insert into 临时用表 (序号,名称) values (5, 4)"

如果是字符型:
sql = "insert into 临时用表 (序号,名称) values ('abbbccc', 'fgkhkljl')"

如果是变量:
a=5
b=4
sql = "insert into 临时用表 (序号,名称) values (" & a & ", " & b & ")"
参考技术A valuess写错了,应该是values。

如果是字符串型变量(比如序号=a,名称=b):sql = "insert into 临时用表 (序号,名称) values ('“ & a & "', '"b & "')"
参考技术B ****这句*****
Set
rs
=
DataEnvironment1.Connection1.Execute(sql,
,
adCmdText)
这种写法用于返回记录集,执行操作查询应改为下列写法
****改为*****
DataEnvironment1.Connection1.Execute
sql

F# OleDb 语法错误在 INSERT INTO 语句中将数据从访问中提取到链接的 SQL Server

【中文标题】F# OleDb 语法错误在 INSERT INTO 语句中将数据从访问中提取到链接的 SQL Server【英文标题】:F# OleDb Syntax Error in INSERT INTO Statement Pulling Data from Access to Linked SQL Server 【发布时间】:2013-03-26 19:21:11 【问题描述】:

我正在运行 F# 应用程序以将数据从 Access 表中提取到链接的 SQL Server 表中。以下是查询结果:

INSERT INTO dbo_TempTerm (UnitID, PolicyTermYear, InsuredName, PolicyNumber, RenewalDate, CovATotal, CovBTotal, CovLTotal, DwellExtn, AllOtherPerilDeductible, MedPay, TotalPremium, HurricaneDeductible, Zone, Subzone, PercentCRC, PercentCRD, YearBuilt, RenYrs, PercentCFD, PercentHA, PercentMLD, GRP1, PercentNH, QCLM, RateV, CRI, AgentCode, AgentName, AFOCode, PolicyType, PolicyForm, OC, DateCreated)
SELECT [UNIT ID], [POLICY TERM YEAR], [INSURED NAME], [POLICY #], [RENEWAL DT], [COV A TOTAL], [COV B TOTAL], [COV L TOTAL], [DWELL EXTN], [ALL/OTHER PERIL DEDUCTIBLE], [MED PAY], [TOT PREMIUM], [HURR DED], ZONE, SUBZONE, [%CRC], [%CRD], [YR BLT], RENYRS, [%CFD], [%HA], [%MLD], GRP1, [%NH], QCLM, [RATE V], CRI, [AGENT CODE], [AGENT NAME], [AFO CODE], [POLICY TYPE], [POLICY FORM], OC, DateCreated
FROM tblPrior

查询在 Access 中解析并运行良好。但在我的应用程序中,它引发了一个错误:INSERT INTO 语句中的语法错误。

这是我的 F#,虽然我认为它无关紧要。

module Data

open System.Data
open System.Data.OleDb

type Period = Prior | Current

let Upload (p:Period) db =
    use conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + db + ";Persist Security Info=False;")

    let execNonQuery s =
        let comm = new OleDbCommand(s, conn) in
        comm.ExecuteNonQuery() |> ignore

    let (table,proc) = match p with
                       | Prior   -> ("tblPrior"  ,"InitPriorTable")
                       | Current -> ("tblCurrent","InitCurrentTable")

    do conn.Open()

    let u = sprintf "INSERT INTO dbo_TempTerm (UnitID, PolicyTermYear, InsuredName, PolicyNumber, RenewalDate, CovATotal, CovBTotal, CovLTotal, DwellExtn, AllOtherPerilDeductible, MedPay, TotalPremium, HurricaneDeductible, Zone, Subzone, PercentCRC, PercentCRD, YearBuilt, RenYrs, PercentCFD, PercentHA, PercentMLD, GRP1, PercentNH, QCLM, RateV, CRI, AgentCode, AgentName, AFOCode, PolicyType, PolicyForm, OC, DateCreated) SELECT [UNIT ID], [POLICY TERM YEAR], [INSURED NAME], [POLICY #], [RENEWAL DT], [COV A TOTAL], [COV B TOTAL], [COV L TOTAL], [DWELL EXTN], [ALL/OTHER PERIL DEDUCTIBLE], [MED PAY], [TOT PREMIUM], [HURR DED], ZONE, SUBZONE, [%%CRC], [%%CRD], [YR BLT], RENYRS, [%%CFD], [%%HA], [%%MLD], GRP1, [%%NH], QCLM, [RATE V], CRI, [AGENT CODE], [AGENT NAME], [AFO CODE], [POLICY TYPE], [POLICY FORM], OC, DateCreated FROM %s" table
    // DEBUG.
    printfn "%s" u
    // DEBUG.
    execNonQuery u
    execNonQuery proc

【问题讨论】:

您是否尝试使用查询从访问中提取数据并直接插入到 SQl 服务器中? 【参考方案1】:

ZoneDateCreated 都是 reserved words。很难预测保留字何时会给 SQL 语句带来麻烦,但在我看来,OleDb 可能不太宽容,即保留字更有可能失败。看看是否将所有出现的这些名称括起来将使INSERT 成功。

let u = sprintf "INSERT INTO dbo_TempTerm (UnitID, PolicyTermYear, InsuredName, PolicyNumber, RenewalDate, CovATotal, CovBTotal, CovLTotal, DwellExtn, AllOtherPerilDeductible, MedPay, TotalPremium, HurricaneDeductible, [Zone], Subzone, PercentCRC, PercentCRD, YearBuilt, RenYrs, PercentCFD, PercentHA, PercentMLD, GRP1, PercentNH, QCLM, RateV, CRI, AgentCode, AgentName, AFOCode, PolicyType, PolicyForm, OC, [DateCreated]) SELECT [UNIT ID], [POLICY TERM YEAR], [INSURED NAME], [POLICY #], [RENEWAL DT], [COV A TOTAL], [COV B TOTAL], [COV L TOTAL], [DWELL EXTN], [ALL/OTHER PERIL DEDUCTIBLE], [MED PAY], [TOT PREMIUM], [HURR DED], [ZONE], SUBZONE, [%%CRC], [%%CRD], [YR BLT], RENYRS, [%%CFD], [%%HA], [%%MLD], GRP1, [%%NH], QCLM, [RATE V], CRI, [AGENT CODE], [AGENT NAME], [AFO CODE], [POLICY TYPE], [POLICY FORM], OC, [DateCreated] FROM %s" table

【讨论】:

就是这样!太感谢了。下次看到类似这样的无意义错误时,我会检查保留字列表。 不客气,杰夫。在我链接的页面顶部附近,有一个 Database Issue Checker Utility 的链接。您可以下载该实用程序并使用它来检查您的 Access 数据库。它会警告您有关问题对象名称以及其他可能造成麻烦的问题。

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

在 INSERT INTO 语句中插入值时出现语法错误。 -VB 和 ACCESS 2013

sql insert into语句错误

INSERT INTO 语句的语法错误

INSERT INTO 语句的语法错误

INSERT INTO 语句的语法错误

INSERT INTO 语句的语法错误