0

I am making a few attempts to batch process several query fetches to a SQL Server database, to pull data, in batches. If I do this in one single pass, it's just too much for Excel, and probably for basic SQL. I know for a fact SQL Server can handle much, much more, but this is NOT a SQL Server environment... it's basically an Excel environment. So, anyway, I'm trying to figure out a way to loop through an array, in steps, and process each step sequentially.

I am trying to do something like this... query rows 1-100 and process, query rows 101-200 and process, query rows 201-300 and process, etc, etc, until the end of the array, which is a bunch of unique IDs, all listed in ColumnA.

Here is the code that I am testing.

Sub TryMe()

'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
Dim lRow As Long
Dim lCol As Long
Dim myRange As String
Dim rng As Range, cell As Range
Dim MatDate As String

pswd = "pswd"
user = "user"
dbName = "DB"
server = "serverName.database.Windows.net"

'Setup the connection string for accessing MS SQL database
    ConnectionString = "Provider=MSDASQL;Extended Properties=""Driver={ODBC Driver 17 for SQL Server};pwd=" & pswd & ";uid=" & user & ";Server=" & server & ";database=" & dbName & """"

    'Opens connection to the database
    cnn.Open ConnectionString

    'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
    cnn.CommandTimeout = 900

    'MatDate = "'12/11/2019'"
    'Find the last non-blank cell in column A(1)
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    LastFind = Cells(Rows.Count, 2).End(xlUp).Row + 1

    Set rng = Range("A2:A" & LastRow)
    stepbegin = 2
    stepend = 100

    For Each cell In rng

        myRange = "'" & Join(Application.Transpose(Range("A" & LastFind & ":A" & stepend).Value), "','") & "'"
        'myRange = Left(myRange, Len(myRange) - 1)
        StrQuery = "Select * FROM myTable WHERE my_ID IN (" & myRange & ")"
        'Performs the actual query
        rst.Open StrQuery, cnn
        'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
        Worksheets("Sheet1").Range("A2").CopyFromRecordset rst

        LastFind = Cells(Rows.Count, 2).End(xlUp).Row

        stepend = LastFind + 100

    Next cell

End Sub

The thing is, I don't want to go cell by cell, but rather chunk by chink, so processing 100 cells at a time, or 1000 cells at a time. It has to be a batch job. I can't do the whole thing in one single pass. I tried a few different ways, and each time the job times out. I'm open to any and all ideas. The simpler, the better.

ComputerVersteher
  • 2,638
  • 1
  • 10
  • 20
ASH
  • 20,759
  • 19
  • 87
  • 200
  • You saw my comment on https://stackoverflow.com/questions/59295567/instead-of-looping-through-rows-how-can-i-very-quickly-create-a-comma-separated? Executed on SQL-Server with proper indexing should be way faster than 15 min.Use OleDB driver instead of MSDASQL, as that uses ODBC con what is way slower. – ComputerVersteher Dec 12 '19 at 03:39
  • I suggest you write _every_ id to a database table in one go (this is fast). _Then_ execute a stored procedure that references that table and does everything in one go. – Nick.Mc Dec 12 '19 at 03:57
  • @Nick.McDermaid: wouldn't you prefer a subquery on the sheet (possible on SQL-Server too) as In clause? No need for a SP or a temp table then.Sth like`Select * FROM myTable WHERE BB_ID IN (Select id FROM [Sheet1$]) AND myDate < '12/11/2019'` – ComputerVersteher Dec 12 '19 at 04:08
  • 1
    Depends what the “processing” is in this case. Also depends if this is multi user. A staging table won’t work for multi user. – Nick.Mc Dec 12 '19 at 04:15
  • Of course (anything that is not depending on sth. in programming?), but if OP doesn't hide essential infos the not well known query on sheet should be the simplest solution, even many excel users avoid sql (would make life too easy).. What is the point on the multi-user comment? Can't see any disadvantages to OPs approach (Sheet can be linked to SQL-Server for multi-user)? – ComputerVersteher Dec 12 '19 at 04:57
  • 1
    I just reread the question. So you are just trying to load data into an Excel sheet. The fundamental question is: _why are you loading 70K rows into an excel sheet?_ I've built an excel UI that basically does that and I had no issues with it. – Nick.Mc Dec 12 '19 at 09:05

0 Answers0