当我使用VBA将数据从Excel导出到SQL Server时,“将数据类型varchar转换为数值时出错”

时间:2019-03-30 05:47:42

标签: sql sql-server excel vba

我已经在Excel中创建了一个宏,可以从SQL Server导入和导出数据。但是,当我尝试导出数据时,它会引发错误

  

[Microsoft] [ODBC SQL Server驱动程序] [SQL Server]
  将数据类型varchar转换为数字时出错

当我导出整数时,代码工作正常。当我尝试导出实数时出现错误。

Dim gil, gibnr, gul, fil, fibnr, ful, qil, qibnr, qul, xil, xibnr, xul, oil, oibnr, oul, cil, cibnr, cul, nil, nibnr, nul, bul, hul, lul, indul As Long


conn.Execute "insert into dbo.Intl_LL (UWU,OBU,Profile_ID, Insured_name,   Claim_number,Claim_desc,  Event_Name,   UY, AY, AQ, Date_of_loss,   Region, CCY,    Policy_number,  Branch, LE, MPL,Claim_alert_email,     Comments,   [Large Profile Flag], [Earmark Flag],   [Tracked for Qtrly dev],  [Gross Incurred], [Gross IBNR], [Gross Ultimate], [FAC Incurred],   [FAC IBNR],   [FAC Ultimate],   [QS Incurred],[QS IBNR],[QS Ultimate],[XOL Incurred],[XOL IBNR],[XOL Ultimate],[Ceded OTH Incurred],[Ceded OTH IBNR],[Ceded OTH Ultimate],[Ceded Total Incurred],[Ceded Total IBNR],[Ceded Total Ultimate],[Net Incurred],[Net IBNR],[Net Ultimate],[Booked Ultimate],version)" & _
     "values ('" & sUWU & "', '" & sOBU & "','" & sProfile & "', '" & sInsured & "','" & sClaim & "','" & sClmdesc & "','" & sEvent & "','" & sUY & "','" & sAY & "','" & sAQ & "','" & sDOL & "','" & sRegion & "','" & sCCY & "','" & sPolnum & "','" & sBranch & "','" & sLE & "','" & sMPL & "','" & sClaimalert & "','" & sComm & "','" & sLargeF & "','" & sEarF & "','" & sTrackF & "','" & gil & "', '" & gibnr & "','" & gul & "','" & fil & "','" & fibnr & "','" & ful & "','" & qil & "','" & qibnr & "','" & qul & "','" & xil & "','" & xibnr & "','" & xul & "','" & oil & "','" & oibnr & "','" & oul & "','" & cil & "','" & cibnr & "','" & cul & "','" & nil & "','" & nibnr & "','" & nul & "','" & bul & "', '" & ver & "')"

我确保在VBA中具有Long数据类型的变量在SQL Server中为numeric(7, 11)类型。

随时询问有关该问题的更多信息。谢谢。

1 个答案:

答案 0 :(得分:0)

MySQL必须假定这些值与“ mycol”列的类型相同,并进行静默转换。 -当您尝试将INT与字符串进行比较时,效果相同。

但是,您应该尽量不要引用应该是数字的值。即使MySQL确实自动转换了其中的一些,它也要花费几个周期,并且行为可能不一致。

底线:始终不要用数字引号,而只用引号和日期引起来。