I currently open and fill a global table with the name ##GlobalTableMain
via a macro at the moment. The table gets created the following way:
Public Sub ExecuteSQLQuery(sQuery As String)
Dim cn As New ADODB.Connection
cn.Open strConnection
cn.Execute "SET NOCOUNT ON;" & sQuery
cn.Close
End Sub
The Query roughly looks like this:
CREATE TABLE ##GlobalTableMain (Columns here);
INSERT INTO ##GlobalTableMain (Columns) VALUES
BUNCH OF ROWS
All of this worked just fine until I tried to add another macro that became necessary due to another factor.
The query in question:
Sub AggregateSQLTempTable(sTempTable As String, sAggClm As String)
Dim cn As New ADODB.Connection, rs As New ADODB.Recordset, sQuery As String
Dim selectClms As String, groupClms As String
cn.Open strConnection
sQuery = "SET NOCOUNT ON; SELECT tempdb.sys.columns.name FROM tempdb.sys.columns WHERE tempdb.sys.columns.object_id = Object_Id('tempdb.." & sTempTable & "')"
rs.Open sQuery, cn
sQuery = vbNullString
selectClms = vbNullString
groupClms = vbNullString
If Not (rs.EOF Or rs.BOF) Then
rs.MoveFirst
Do While Not (rs.EOF Or rs.BOF)
selectClms = selectClms & IIf(Len(selectClms) > 0, ", ", "") & IIf(rs!Name = sAggClm, "SUM(" & rs!Name & ") " & rs!Name, rs!Name)
groupClms = groupClms & IIf(rs!Name = sAggClm, "", IIf(Len(groupClms) > 0, ", ", "") & rs!Name)
rs.MoveNext
Loop
sQuery = vbNullString
sQuery = "SELECT * INTO #aggTempTable FROM (SELECT " & selectClms & " FROM " & sTempTable & " GROUP BY " & groupClms & ") a;"
sQuery = sQuery & Chr(10) & "TRUNCATE TABLE " & sTempTable & ";"
sQuery = sQuery & Chr(10) & "INSERT INTO " & sTempTable & " SELECT * FROM #aggTempTable;"
sQuery = sQuery & Chr(10) & "DROP TABLE #aggTempTable;"
cn.Execute sQuery
End If
rs.Close
cn.Close
End Sub
Supposedly SET NOCOUNT ON
should prevent this but it doesn't work for me unfortunately.