Apologies if this is too much info, but I wanted to be thorough when posting.
I've been struggling with an issue getting my temporary table to populate with data from an Insert Query.
This data is being pulled from Oracle SQL tables
This query creates the temp table, inserts the data, and queries the temp table without issue in the Oracle SQL Developer software
My code will successfully create the temp table (I've verified its existence in the Oracle software)
I am able to use Select Queries with my setup (So I do not believe the connection is the issue)
Running my code doesn't produce any errors (At least nothing that stops the execution)
What I did notice is that if I use "Record Set" in the execution of my insert query, it does show that there are the expected amount of affected rows, but when I Select All (From the Oracle Software) from the temp table it shows nothing populated.
I've been looking all over but I haven't quite figured out what I am missing. From what I was reading it could be that I don't Set NOCOUNT ON (Or Set FEEDBACK OFF), but I can't find how to actually do that within my VBA code. I originally tried inserting the data into an existing table, so I attempted to create a temporary table to move the data into.
Option Explicit
Sub oracleQuery()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ActiveWorkbook.Worksheets("Query Output")
Set ws2 = ActiveWorkbook.Worksheets("Main Page")
Clears query output worksheet
ws1.Cells.Clear
Assigns variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.RecordSet
Dim ConnectionString As String
Dim StrQuery As String
Dim StrQuery2 As String
Dim StrQuery3 As String
Dim StrQuery4 As String
This query creates a blank Global Temp table (With column headers)
StrQuery = "XXXXXX"
This is my insert Query
StrQuery2 = "INSERT INTO temp_1 (Field1,Field2,Field3,..etc) Select From
Where UNION ALL Select From Where UNION ALL..Etc
This query selects the data from the temp table, and joins in some addition fields with detail information
StrQuery3 = "Select temp_1. Etc....
This query is used to drop the temp table when I am done with it
StrQuery4 = "DROP TABLE temp_1"
This is my connection string
ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=(XXX)(CONNECT_DATA=(SERVICE_NAME=XXX)));User ID=XXX;Password=XXX"
Opens connection to the database, also sets a timeout timer
cnn.Open ConnectionString
cnn.CommandTimeout = 60
Executes Temp table query
cnn.Execute StrQuery
Executes Insert Query
cnn.BeginTrans
cnn.Execute StrQuery2
cnn.CommitTrans
Executes select query on temp table, uses a record set
rst.Open StrQuery3, cnn
Drops temp table
cnn.Execute StrQuery4
Dumps all the results from the record set into cell A2 of the first sheet in the active workbook
ws1.Range("A2").CopyFromRecordset rst
Empties connection string
ConnectionString = ""
Closes and clears recordset
rst.Close
Set rst = Nothing
End Sub
It would be great to determine what is actually causing my Insert Query to not insert data. I would greatly appreciate any insight on this issue as I am running out of ideas to check.
UPDATE I simplified my code to hone in on my problem. My issue is the INSERT INTO Query is not actually inserting any data into the table. The Select Query I am trying to Insert results in 3 columns & rows of data.
Temp table query
StrQuery = "CREATE GLOBAL TEMPORARY TABLE temp_1( GALOTIDX Number(9, 0), LOTID VARCHAR2(12 BYTE), LOTDESCR VARCHAR2(40 BYTE))"
INSERT INTO Query String
StrQuery2 = "Insert INTO temp_1 (GALOTIDX, LOTID, LOTDESCR)"
StrQuery2 = StrQuery2 + " Select LotIdx, ID1, Descr1 From DataTable1"
Query For Final Select
StrQuery3 = "Select * From temp_1"
Query for Dropping Temp Table
StrQuery4 = "DROP TABLE temp_1"
Builds connection string
ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=XXX)(Port=XXX))(CONNECT_DATA=(SERVICE_NAME=XXX)));User ID=XXX;Password=XXX"
Opens connection to the database, also sets a timeout timer
cnn.Open ConnectionString
cnn.CommandTimeout = 60
Creates Temp Table
cnn.Execute StrQuery
Executes Insert Into Query
Dim recordsAffected As Integer
cnn.BeginTrans
cnn.Execute StrQuery2, recordsAffected
cnn.CommitTrans
Selects Data from temp table and dumps into a worksheet
rst.Open StrQuery3, cnn
ws1.Range("A2").CopyFromRecordset rst
Drops Temp Table
cnn.Execute StrQuery4
Empties connection string
ConnectionString = ""
cnn.Close