循环遍历 SQL 时溢出 - 为 ADODB.connection 插入
Overflow when looping through SQL-inserts for an ADODB.connection
我正在尝试将来自金融服务提供商的实时数据插入到 Excel 中,只能使用 Excel 插件获取大约 100,000 个值的块。
代码似乎按预期工作:
Sub insert()
'Declare Variables
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim ColumnsSchema As ADODB.Recordset
Dim rsT As Variant
Dim i As Integer
For i = 0 To 2
rsT = Join(Application.Transpose(ThisWorkbook.Sheets("Prices").Range(Cells(3 + i * 10000, 9), Cells(10002 + i * 10000, 9)).Value), " ")
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=DB1;" 'rest of the string blackened
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute(rsT)
Next i
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
如果我将计数器 i 增加到 3,从而使用 for 循环插入超过 30000,则会导致溢出错误。
我试图将它分成更小的块,并且计数器变为 49:类似错误。
sheet 'prices' 中引用的单元格似乎是正确的。这就是为什么我只在这里发布 VBA 代码。由于我不太熟悉 VBA 和所用对象的局限性,所以我希望那里有问题。
您可以 运行 多次查询而无需每次都创建新连接。
Option Explicit
Sub insert()
Const BATCH_SIZE = 10000 ' 10,000
'Declare Variables
Dim conn As ADODB.Connection, sConnString As String, SQL As String
Dim ws As Worksheet, r As Long, LastRow As Long, n As Long
Dim t0 As Single: t0 = Timer
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=DB1;" 'rest of the string blackened
' Create the Connection
Set conn = New ADODB.Connection
conn.Open sConnString
Set ws = ThisWorkbook.Sheets("Prices")
With ws
LastRow = .Cells(Rows.Count, 9).End(xlUp).Row ' col I
For r = 3 To LastRow Step BATCH_SIZE
SQL = Join(Application.Transpose(ws.Cells(r, 9).Resize(BATCH_SIZE).Value), " ")
conn.Execute SQL
n = n + 1
Next
End With
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
MsgBox LastRow - 2 & " rows in " & n & " batches of max size " & BATCH_SIZE, _
vbInformation, Format(t0 - Timer, "0.0 secs")
End Sub
我正在尝试将来自金融服务提供商的实时数据插入到 Excel 中,只能使用 Excel 插件获取大约 100,000 个值的块。
代码似乎按预期工作:
Sub insert()
'Declare Variables
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim ColumnsSchema As ADODB.Recordset
Dim rsT As Variant
Dim i As Integer
For i = 0 To 2
rsT = Join(Application.Transpose(ThisWorkbook.Sheets("Prices").Range(Cells(3 + i * 10000, 9), Cells(10002 + i * 10000, 9)).Value), " ")
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=DB1;" 'rest of the string blackened
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute(rsT)
Next i
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
如果我将计数器 i 增加到 3,从而使用 for 循环插入超过 30000,则会导致溢出错误。
我试图将它分成更小的块,并且计数器变为 49:类似错误。
sheet 'prices' 中引用的单元格似乎是正确的。这就是为什么我只在这里发布 VBA 代码。由于我不太熟悉 VBA 和所用对象的局限性,所以我希望那里有问题。
您可以 运行 多次查询而无需每次都创建新连接。
Option Explicit
Sub insert()
Const BATCH_SIZE = 10000 ' 10,000
'Declare Variables
Dim conn As ADODB.Connection, sConnString As String, SQL As String
Dim ws As Worksheet, r As Long, LastRow As Long, n As Long
Dim t0 As Single: t0 = Timer
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=DB1;" 'rest of the string blackened
' Create the Connection
Set conn = New ADODB.Connection
conn.Open sConnString
Set ws = ThisWorkbook.Sheets("Prices")
With ws
LastRow = .Cells(Rows.Count, 9).End(xlUp).Row ' col I
For r = 3 To LastRow Step BATCH_SIZE
SQL = Join(Application.Transpose(ws.Cells(r, 9).Resize(BATCH_SIZE).Value), " ")
conn.Execute SQL
n = n + 1
Next
End With
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
MsgBox LastRow - 2 & " rows in " & n & " batches of max size " & BATCH_SIZE, _
vbInformation, Format(t0 - Timer, "0.0 secs")
End Sub