从本地访问数据库批量加载到链接的ODBC SQL Server可更新表

时间:2018-06-08 16:51:37

标签: sql-server access-vba odbc

我是stackoverflow的新手,所以如果我打破任何论坛规则,请原谅我。我目前使用MS-Access作为许多ODBC链接的SQL服务器表的前端视图。当我尝试将一列数据从本地表加载到SQL服务器上的“可更新”表时,我的问题出现了。只需粘贴10k记录就需要15分钟,而我正在寻找一种解决方案来解决如何使用VB模块来减少这段时间的问题。使用此链接How to increase performance for bulk INSERTs to ODBC linked tables in Access?,我能够创建一个与刚刚粘贴的速度大致相同的宏,但我确实需要提高速度,因为它只是太慢

我的本​​地表名是 [tblTempSmartSSP] ,其中包含一个字段(电路)

我的SQL Server ODBC链接表 [dbo_REF_CIRCUIT_LIST_UPDATEABLE_RM] 具有匹配的字段名称(电路)

Sub PtqTest()
Dim cdb As DAO.Database, rst As DAO.Recordset
Dim t0 As Single, i As Long, valueList As String, separator As String

t0 = Timer
Set cdb = CurrentDb
Set rst = cdb.OpenRecordset("SELECT circuits FROM tblTempSmartSSP", dbOpenSnapshot)
i = 0
valueList = ""
separator = ""
Do Until rst.EOF
    i = i + 1
    valueList = valueList & separator & "(" & rst!circuits & ")"
    If i = 1 Then
        separator = ","
    End If
    If i = 1000 Then
        SendInsert valueList
        i = 0
        valueList = ""
        separator = ""
    End If
    rst.MoveNext
Loop
If i > 0 Then
    SendInsert valueList
End If
rst.Close
Set rst = Nothing
Set cdb = Nothing
Debug.Print "Elapsed time " & Format(Timer - t0, "0.0") & " seconds."
End Sub

Sub SendInsert(valueList As String)
Dim cdb As DAO.Database, qdf As DAO.QueryDef

Set cdb = CurrentDb
Set qdf = cdb.CreateQueryDef("")
qdf.Connect = cdb.TableDefs("dbo_REF_CIRCUIT_LIST_UPDATEABLE_RM").Connect
qdf.ReturnsRecords = False
qdf.SQL = "INSERT INTO dbo_REF_CIRCUIT_LIST_UPDATEABLE_RM (Circuits) VALUES 
" & valueList
qdf.Execute dbFailOnError
Set qdf = Nothing
Set cdb = Nothing
End Sub

提前感谢您提供的任何见解!!

赖安

0 个答案:

没有答案