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

Posted

技术标签:

【中文标题】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 数据库。它会警告您有关问题对象名称以及其他可能造成麻烦的问题。

以上是关于F# OleDb 语法错误在 INSERT INTO 语句中将数据从访问中提取到链接的 SQL Server的主要内容,如果未能解决你的问题,请参考以下文章

给Access数据库添加数据,但是提示insert into语法错误

INSERT INTO 语句的语法错误。

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

OleDB INSERT 命令错误

数据库写入数据insert into语法错误,以下是代码,请高手差错修改

System.Data.OleDb.OleDbException: 'UPDATE 语句中的语法错误。 '