通过VBA ODBC连接将多行上载到Teradata

时间:2018-03-27 20:03:24

标签: sql vba excel-vba teradata excel

我有以下VBA代码用于Teradata的let comps = calendar.dateComponents([.hour, .minute, .second], from: currentTime) let st = Double(comps.hour!) + Double(comps.minute!)/60 + Double(comps.second!)/3600 语句。

select

但是,我尝试将 strsql Sub Item_Review() Dim strsql As String strsql = Worksheets("SQL").Range("b3") With ActiveWorkbook.Connections("Item_Review").ODBCConnection .BackgroundQuery = True Debug.Print strsql CommandText = SplitMeUp(strsql) End With ActiveWorkbook.Connections("Item_Review").Refresh End Sub 语句更改为select语句。当我运行VBA时没有任何反应。我点击按钮,我没有得到任何更新通知,也没有收到错误通知。当我在Teradata中查看表时,我可以看到它尚未更新。有什么想法吗?

insert into

2 个答案:

答案 0 :(得分:0)

只是您没有执行任何操作查询。考虑使用ADO连接对象,您可以从OLEDB或ODBC工作簿连接构建(选择适当的连接对象)。

Sub RunQuery()
On Error GoTo ErrHandle    
    Dim connstr As String, strsql As String
    Dim conn As Object
    Const adModeReadWrite = 3

    ' OLEDB CONNECTION STRING (PREFIXES REMOVED)
    connstr = Replace(ThisWorkbook.Connections("Item_Add").OLEDBConnection.Connection, -
                      "OLEDB;", "")
    ' ODBC CONNECTION STRING (PREFIXES REMOVED)
    connstr = Replace(ThisWorkbook.Connections("Item_Add").ODBCConnection.Connection, _
                      "ODBC;", "")

    strsql = "insert into Table_1  (Item_id, NOTE, date, Work_STATUS)" _
              & " values ('Item_1','Testing ','2014-01-01','Worked');"

    Set conn = CreateObject("ADODB.Connection")

    With conn
       .ConnectionString = connstr
       .Mode = adModeReadWrite
       .Open                   
       .Execute strsql       
    End With

    MsgBox "Successfully ran query!", vbInformation     

ExitHandle:
    Set conn = Nothing
    Exit Sub

ErrHandle:
    MsgBox Err.Number & " - " & Err.Description, vbCritical
    Exit Sub
End Sub

答案 1 :(得分:0)

只要我有一个连接到连接的表,我就可以运行它。

1)所以我去了Excel中的数据选项卡 2)单击现有连接 3)选择查询 4)将数据导入表

一旦我这样做,SQL就会在刷新时执行。