将值从一个表插入另一个表,更好的性能 - Access 2000,VBA?

时间:2017-07-14 11:41:10

标签: sql vba ms-access

我已经制作了这个方法:

Public Sub Proba()

    Dim CPANonEmptyColumns As Integer
    CPANonEmptyColumns = 0

    Dim Max As Integer
    Max = 0

    Dim Koloni As String
    Koloni = ""

    Dim strSQL As String
    Dim intI As Integer

    Dim rsCPA As DAO.Recordset
    Dim rsCPANezbirni As DAO.Recordset
    Dim dbs_t1t2 As DAO.Database


On Error GoTo ErrorHandler

    Set dbs_t1t2 = CurrentDb

    'Open a recordset on all records from the Employees table that have
    'a Null value in the ReportsTo field.
    strSQL = "SELECT * FROM CPA_t1t2"
    Set rsCPA = dbs_t1t2.OpenRecordset(strSQL)    '//, dbOpenDynaset)

    'If the recordset is empty, exit.
    If rsCPA.EOF Then Exit Sub

    intI = 1
    With rsCPA
      Do Until .EOF
        DoCmd.RunSQL "INSERT INTO CPA_nezbirni (tipprod, promet) VALUES ('" & ![tipprod] & "', '" & ![promet] & "');"

        ' check individual column if it has a value and increment max if CPANonEmptyColumns
        If ![t4k1] <> Null Or ![t4k1] <> "" Then
           CPANonEmptyColumns = CPANonEmptyColumns + 1
           Koloni = Koloni & "t4k1,"

        End If

        If ![t4k2] <> Null Or ![t4k2] <> "" Then
           CPANonEmptyColumns = CPANonEmptyColumns + 1
           Koloni = Koloni & "t4k2,"
        End If

        If ![t4k3] <> Null Or ![t4k3] <> "" Then
           CPANonEmptyColumns = CPANonEmptyColumns + 1
           Koloni = Koloni & "t4k3,"
        End If

        If CPANonEmptyColumns > Max Then
            Max = CPANonEmptyColumns
        End If

        Debug.Print "Red: " & Str(intI) & " Max: " & Str(Max) & ", Koloni: " & Koloni

        .Edit
        .MoveNext
        CPANonEmptyColumns = 0
        Koloni = ""
        intI = intI + 1
      Loop
    End With

    rsCPA.Close
    dbs_t1t2.Close

    Set rsCPA = Nothing
    Set dbs_t1t2 = Nothing

ErrorHandler:
   MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description

End Sub

基本上,我打开两个表,CPA_t1t2和CPA_nezbirni。我想将适当的列,tipprod和promet的适当值从CPA_t1t2复制到CPA_nezbirni。 问题是,源表CPA_t1t2有18000行,需要时间运行所有这些&#34; INSERT&#34;查询语句:

DoCmd.RunSQL "INSERT INTO CPA_nezbirni (tipprod, promet) VALUES ('" & ![tipprod] & "', '" & ![promet] & "');"

当SQL出现性能时,我总是怀疑。由于完成过程需要3-4分钟并将值插入CPA_nezbirni,因此SQL将值从一个表复制到另一个表的速度更慢吗?

使用上面的程序和一些VBA是否有更好,更快的方式,通过相同的&#34; Do Until&#34;循环?

0 个答案:

没有答案