1

my code below, whoch I've copied from a Yahoo Developers articles and changed accordingly, for Query, I want to copy and paste 2999 rows of insert statements from excel to Teradata. My current way doesn't copy and paste the entire range. If I swap this for: Cells(1, 1) & " " & Cells(2, 1) & " " & Cells(3, 1)....etc. until Cells(2999), it would work. A clever, simpler way of doing this please?

As an aside, would you recommend an alternative method of inserting 2999 rows. The tables are already populated, so FLOAD won't work. MLOAD or BTEQ? I'm using normal insert statements because 2999 is small enough to get away with. But, I'd always be very grateful for a q quicker solution! Thank you all!

Sub Insert_to_TD()



Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim cmdsqldata As ADODB.Command
Set cmdsqldata = New ADODB.Command

cn.Open "DSN=NNNNNN; Username=XXXXX; Password=YYYYYYY;"


Set cmdsqldata.ActiveConnection = cn 'This line says to which database it has to send the query
Query = Range(Cells(1, 1), Cells(2999, 1))
cmdsqldata.CommandText = Query 'We asign the query as command text
cmdsqldata.CommandType = adCmdText 'We just say what kind of command VBA has to execute
cmdsqldata.CommandTimeout = 0 'With this instruction we don't set any timeout, so the     query can take all the necessary time to be executed
Set rs = cmdsqldata.Execute() 'VBA just run the query and send back the result


End Sub
4

1 回答 1

2

这不会导致错误,使用 VBA Join():

Function GetColumn1(varArray)
  Dim i, i0, i1, varArrayRet

  i0 = LBound(varArray, 1)
  i1 = UBound(varArray, 1)

  ReDim varArrayRet(i0 To i1)

  For i = i0 To i1
    varArrayRet(i) = varArray(i, 1)
  Next
  GetColumn1 = varArrayRet
End Function

Sub Insert_to_TD()

  Dim cn As ADODB.Connection
  Set cn = New ADODB.Connection
  Dim rs As ADODB.Recordset
  Set rs = New ADODB.Recordset
  Dim cmdsqldata As ADODB.Command
  Set cmdsqldata = New ADODB.Command

  Dim varArray, Query

  cn.Open "DSN=NNNNNN; Username=XXXXX; Password=YYYYYYY;"


  Set cmdsqldata.ActiveConnection = cn 'This line says to which database it has to send the query

  '
  'Query = Range(Cells(1, 1), Cells(2999, 1))
  '
  varArray = Range("A1:A2999").Value
  varArray = GetColumn1(varArray)
  Query = Join(varArray, " ")

  cmdsqldata.CommandText = Query 'We asign the query as command text
  cmdsqldata.CommandType = adCmdText 'We just say what kind of command VBA has to execute
  cmdsqldata.CommandTimeout = 0 'With this instruction we don't set any timeout, so the     query can take all the necessary time to be executed
  Set rs = cmdsqldata.Execute() 'VBA just run the query and send back the result


End Sub

预订:

尽管您最好使用 for 循环,插入数据行/行,但它会比捆绑插入更快更好地完成。

现在我们尝试逐行运行 SQL?

Sub Insert_to_TD()

  Dim cn As ADODB.Connection
  Set cn = New ADODB.Connection
  Dim rs As ADODB.Recordset
  Set rs = New ADODB.Recordset
  Dim cmdsqldata As ADODB.Command
  Set cmdsqldata = New ADODB.Command

  Dim i, strSQL

  cn.Open "DSN=NNNNNN; Username=XXXXX; Password=YYYYYYY;"


  Set cmdsqldata.ActiveConnection = cn

  cmdsqldata.CommandType = adCmdText
  cmdsqldata.CommandTimeout = 0

  '
  For i = 1 To 2999
    strSQL = ActiveSheet.Cells(i, 1).Value
    cmdsqldata.CommandText = strSQL
    Set rs = cmdsqldata.Execute()
  Next

End Sub
于 2013-11-12T10:05:46.277 回答