简体   繁体   中英

Uploading Volatile Table using VBA

I have a local table in an MS Access 2010 database. I would like to use ADODB to load the local table into a Teradata environment within my spool space using CREATE VOLATILE TABLE command

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

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]

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

I am getting the error: TEMP_TABLE already exists. 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. The population of the table is session specific using the TEMPORARY space assigned to the user or the profile of the user.

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

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