简体   繁体   中英

“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. However, when I try to export data, it throws an error

[Microsoft][ODBC SQL Server Driver][SQL Server]
Error converting data type varchar to numeric

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.

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. - It does the same when you try to compare an INT to a string.

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.

Bottom line: always leave numbers unquoted, and only quote strings and dates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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