简体   繁体   English

使用VBA上传挥发表

[英]Uploading Volatile Table using VBA

I have a local table in an MS Access 2010 database. 我在MS Access 2010数据库中有一个本地表。 I would like to use ADODB to load the local table into a Teradata environment within my spool space using CREATE VOLATILE TABLE command 我想使用ADODB通过CREATE VOLATILE TABLE命令将本地表加载到后台打印空间内的Teradata环境中

This should be achievable i think using arrays but i was wondering is there a better way (speedy too if possible) to batch load the records into the teradata environment for example 我认为使用数组应该可以实现,但是我想知道是否有更好的方法(如果可能的话也可以快速)将记录批量加载到Teradata环境中。

CREATE VOLATILE TABLE EXAMPLE AS (SELECT * FROM LOCAL TABLE)
WITH DATA PRIMARY INDEX(Set Index Keys)
ON COMMIT PRESERVE ROWS;

Thanks for your help 谢谢你的帮助

Just a quick update on this [24/01/2013] 请对此快速更新[24/01/2013]

I have managed to get the data from the access database, Load it into an Array, Create a Volatile table in my teradata warehouse using spool space and then attempt to load the array into the table 我已经设法从访问数据库中获取数据,将其加载到阵列中,使用后台处理空间在我的Teradata仓库中创建一个Volatile表,然后尝试将阵列加载到表中

I am getting the error: TEMP_TABLE already exists. 我收到错误消息:TEMP_TABLE已经存在。 when trying to append the records to the table 尝试将记录追加到表时

I am almost there so any help would be greatly appreciated. 我快到了,因此任何帮助将不胜感激。 Thank you again for your help 再次感谢你的帮助

Public Function Open_Connection()

On Error GoTo ErrorHandler

    ' http://voices.yahoo.com/teradata-ms-excel-vba-2687156.html
    ' The connection is used to connect with the DBMS,
    ' The Recordset to surf the result of some SELECT queries
    ' The Command to send the sql requests to Teradata.

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 myArray() As Variant

' Open Connection With Teradata
cn.Open "Data Source=Database; " & _
        "Database=Database; " & _
        "Persist Security Info=True; " & _
        "User ID=Me; " & _
        "Password=FakePassword; " & _
        "Session Mode=ANSI;"

' Which database it has to send the query
Set cmdSQLData.ActiveConnection = cn

   ' Query to create the violotile table in Teradata'
       Query = "CREATE VOLATILE TABLE TEMP_TABLE ( " & _
            "field1 VARCHAR (39)," & _
            "field2 VARCHAR (44)," & _
            "field3 DECIMAL (18, 3)," & _
            "field4 CHAR (3))" & _
            "ON COMMIT PRESERVE ROWS;"

' Query is assessed as Text
cmdSQLData.CommandText = Query
' Specifies which kind of command VBA has to execute
cmdSQLData.CommandType = adCmdText
' Remove Query Timeouts
cmdSQLData.CommandTimeout = 60
'VBA just run the query and send back the result
Set rs = cmdSQLData.Execute()

' Retrieve the sharepoint records into an Array
myArray = Retrieve_Sharepoint_Records

intNumberRows = UBound(myArray, 2) + 1 ' number of records/rows in the array
rowcounter = 0

' Append the Rows to the temp table
For rowcounter = 0 To intNumberRows - 1
        ' Debug.Print myArray(0, rowcounter) & " | " & myArray(1, rowcounter) & " | " & myArray(2, rowcounter) & " | " & myArray(3, rowcounter)
    AppendQuery = "INSERT INTO TEMP_TABLE VALUES ('" & myArray(0, rowcounter) & "','" & myArray(1, rowcounter) & "'," & myArray(2, rowcounter) & ",'" & myArray(3, rowcounter) & "');"

    cmdSQLData.CommandText = Query
    cmdSQLData.CommandType = adCmdText
    cmdSQLData.CommandTimeout = 60
    Set rs = cmdSQLData.Execute()

Next

' Clean up
cn.Close
Set cn = Nothing
Set rs = Nothing
Set cmdSQLData = Nothing

ErrorHandler:
If (Len(Err.Description) > 0) Then
    MsgBox (Err.Description)
End If

End Function

You may have better success using a Global Temporary Table as the object definition is stored within the DBC Data Dictionary on Teradata. 使用Global Temporary Table可能会获得更好的成功,因为对象定义存储在Teradata上的DBC数据字典中。 The population of the table is session specific using the TEMPORARY space assigned to the user or the profile of the user. 该表的填充是特定于会话的,使用分配给用户的TEMPORARY空间或用户的个人资料。

Just to answer my own question, I should change the second code line of below 只是为了回答我自己的问题,我应该更改下面的第二行代码

cmdSQLData.CommandText = Query

To

cmdSQLData.CommandText = AppendQuery

It then works perfectly albeit slowly 即使缓慢,它也可以正常工作

Again thank you all for your help 再次感谢大家的帮助

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

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