2013-04-04 27 views
2

我正在運行一個F#應用程序以將數據從Access表中提取到鏈接的SQL Server表。查詢結果如下:INSERT INTO語句中的F#OleDb語法錯誤將數據從訪問中提取到鏈接的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 
+0

您是否試圖使用查詢從訪問中提取數據並直接將其插入SQl服務器? – 2013-04-04 15:58:29

回答

3

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 
+0

就是這樣!非常感謝。下次我看到另一個像這樣的無意義錯誤時,我會檢查保留字列表。 – 2013-04-05 14:53:01

+0

不客氣,傑夫。在我鏈接的頁面頂部附近,有一個* Database Issue Checker Utility *的鏈接。您可以下載該實用程序並使用它來檢查您的Access數據庫。它會警告你關於問題對象名稱,以及其他可能產生麻煩的問題。 – HansUp 2013-04-05 15:01:55