从Excel导入SQL Server时的日期数据类型转换错误

时间:2016-11-16 04:48:03

标签: sql sql-server vba types

我需要这个案子的帮助。我需要将一个表从Excel导入到SQL Server,我有两个类型为DATE的列。

问题是当运行宏时,显示以下消息

  

从字符串

转换日期和/或时间时转换失败

Excel中的表包含此格式为dd/mm/YYYY的日期,SQL Server列为DATE数据类型。我需要帮助来解决这个问题。感谢您的支持。

在VBA中附加CODE

  'Create Conection for MySQL Database in AR_TELCEL
Dim conn As New ADODB.Connection
conn.CommandTimeout = 0
'Define Data Types to Cells for Upload a SQL Database
Dim iRowNo As Long
Dim CUST_ID As Long, DOC_TYPE As String, BILL_DOC As Long, REFERENCE As String, STATUS_PAYMENT As String, INV_CON As String, OSALES_FOUR As String, INV_REF As String, DOC_NUM As Long, GL As Long, DOC_DATE As Date, DUE_DATE As Date, DOC_CURRENCY As String, DOC_AMNT As Currency, LOC_AMNT As Currency, PAY_TERM As String, POST_K As String, LINE_N As Long



    With Sheets("I. One Report Sap")

        '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
        'Connect with MSSQL PCC AR_TELCEL Database
        '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

        conn.Open "Provider=SQLOLEDB;Data Source=146.250.36.68,1433;Initial Catalog=AR_TELCEL;User Id=user;Password=Password;"

        'Omitimos la linea de encabezados
        iRowNo = 10

        'Loop until empty cell in CustomerId
        Do Until .Cells(iRowNo, 1) = ""
            ACCOUNT = .Cells(iRowNo, 1)
            DOC_TYPE = .Cells(iRowNo, 2)
            BILL_DOC = .Cells(iRowNo, 3)
            REFERENCE = .Cells(iRowNo, 4)
            STATUS_PAYMENT = .Cells(iRowNo, 5)
            INV_CON = .Cells(iRowNo, 6)
            ORDER_SALES = .Cells(iRowNo, 7)
            INV_REF = .Cells(iRowNo, 8)
            DOC_NUM = .Cells(iRowNo, 9)
            GL = .Cells(iRowNo, 10)
            DOC_DATE = .Cells(iRowNo, 11)
            DUE_DATE = .Cells(iRowNo, 12)
            DOC_CURRENCY = .Cells(iRowNo, 13)
            DOC_AMNT = .Cells(iRowNo, 14)
            LOC_AMNT = .Cells(iRowNo, 15)
            PAY_TERM = .Cells(iRowNo, 16)
            POST_K = .Cells(iRowNo, 17)
            LINE_N = .Cells(iRowNo, 18)

1 个答案:

答案 0 :(得分:0)

我可以解决这个问题。

原始代码:

conn.Execute "insert into one_rep_sap_temp  (ACCOUNT, DOC_TYPE, BILL_DOC, REFERENCE, STATUS_PAYMENT, INV_CON, OSALES_FOUR, INV_REF, DOC_NUM, GL, DOC_DATE, DUE_DATE, DOC_CURRENCY, DOC_AMNT, LOC_AMNT, PAY_TERM, POST_K, LINE_N) values ('" & ACCOUNT & "', '" & DOC_TYPE & "', '" & BILL_DOC & "', '" & REFERENCE & "', '" & STATUS_PAYMENT & "', '" & INV_CON & "', '" & OSALES_FOUR & "', '" & INV_REF & "', '" & DOC_NUM & "', '" & GL & "', '" & DOC_DATE & "', '" & DUE_DATE & "', '" & DOC_CURRENCY & "', '" & DOC_AMNT & "', '" & LOC_AMNT & "', '" & PAY_TERM & "', '" & POST_K & "', '" & LINE_N & "')"

新守则:

conn.Execute "insert into one_rep_sap_temp  (ACCOUNT, DOC_TYPE, BILL_DOC, REFERENCE, STATUS_PAYMENT, INV_CON, OSALES_FOUR, INV_REF, DOC_NUM, GL, DOC_DATE, DUE_DATE, DOC_CURRENCY, DOC_AMNT, LOC_AMNT, PAY_TERM, POST_K, LINE_N) values ('" & ACCOUNT & "', '" & DOC_TYPE & "', '" & BILL_DOC & "', '" & REFERENCE & "', '" & STATUS_PAYMENT & "', '" & INV_CON & "', '" & OSALES_FOUR & "', '" & INV_REF & "', '" & DOC_NUM & "', '" & GL & "', '" & Format(DOC_DATE, "YYYY-MM-DD") & "', '" & Format(DUE_DATE, "YYYY-MM-DD") & "', '" & DOC_CURRENCY & "', '" & DOC_AMNT & "', '" & LOC_AMNT & "', '" & PAY_TERM & "', '" & POST_K & "', '" & LINE_N & "')"

修复就是这个

格式(DOC_DATE," YYYY-MM-DD")

感谢您对此问题的帮助。 :d