简体   繁体   English

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

[英]“Error converting data type varchar to numeric” when I export data from Excel to SQL Server using VBA

I have created a macro in Excel to import and export data from SQL Server. 我在Excel中创建了一个宏来从SQL Server导入和导出数据。 However, when I try to export data, it throws an error 但是,当我尝试导出数据时,它会抛出错误

[Microsoft][ODBC SQL Server Driver][SQL Server] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]
Error converting data type varchar to numeric 将数据类型varchar转换为数字时出错

The code worked fine when I was exporting integer numbers. 我导出整数时代码工作正常。 The error came up when I tried exporting real numbers. 当我尝试导出实数时出现错误。

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 & "')"

I have ensured that the variables that have Long data type in VBA are numeric(7, 11) type in SQL Server. 我确保在VBA中具有Long数据类型的变量在SQL Server中是numeric(7, 11)类型。

Feel free to ask for more information about the problem. 请随时询问有关该问题的更多信息。 Thanks. 谢谢。

MySQL must assume the values are supposed to be the same type as the "mycol" column and silently converts them. MySQL必须假设这些值应该与“mycol”列的类型相同,并以静默方式转换它们。 - It does the same when you try to compare an INT to a string. - 当您尝试将INT与字符串进行比较时,它也会这样做。

However, you should try not to quote values that are supposed to be numbers. 但是,您应该尽量不引用应该是数字的值。 Even if MySQL does automatically convert some of them, it costs a few cycles and the behavior may not be consistent. 即使MySQL确实自动转换其中的一些,它也会花费几个周期,并且行为可能不一致。

Bottom line: always leave numbers unquoted, and only quote strings and dates. 底线:始终不加引号,只引用字符串和日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM