2016-11-09 149 views
0

我目前有拉從表A說做B表中不存在的所有記錄的工作表,然後我有一個插入這些值到表B.更新數據庫連接

但是我意識到,如果在插入宏之前查詢沒有刷新,它會產生重複項。

使用當前的宏,我如何在插入之前和之後包含刷新?

Sub Insert_New_Bills() 

    Dim conn As New ADODB.Connection 
    Dim iRowNo As Integer 
    Dim sBILL_NUM, sROCKTENN_DOC, sACTION, sNOTE1, sNOTE2 As String 

    With Sheets("NEW BILLS") 

     'Open a connection to SQL Server 
     conn.Open "Driver={IBM DB2 ODBC DRIVER};Database=BROWN;Hostname=192.168.100.44;Port=50000;Protocol=TCPIP;Uid=" & "User" & ";Pwd=" & "Maddox2009" & ";CurrentSchema=LYNX;" 

     'Skip the header row 
     iRowNo = 2 

     'Loop until empty cell in COLUMN1 
     Do Until .Cells(iRowNo, 2) = "" 
      sBILL_NUM = .Cells(iRowNo, 2) 
      sROCKTENN_DOC = .Cells(iRowNo, 3) 
      sACTION = .Cells(iRowNo, 4) 
      sNOTE1 = .Cells(iRowNo, 5) 
      sNOTE2 = .Cells(iRowNo, 6) 

      'Generate and execute sql statement to import the excel rows to SQL Server table 
      conn.Execute "INSERT INTO OH_CU_WR_TEMPLATE (BILL_NUMBER, ROCKTENN_DOC, ACTION, NOTE1, NOTE2) values ('" & sBILL_NUM & "','" & sROCKTENN_DOC & "', '" & sACTION & "', '" & sNOTE1 & "', '" & sNOTE2 & "')" 


      iRowNo = iRowNo + 1 
     Loop 

     MsgBox "RECORD UPDATED" 

     conn.Close 
     Set conn = Nothing 

     Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False 

    End With 

End Sub 
+0

我不知道關於'db2'的語法。但我寧願使用與此類似的解決方案:http://stackoverflow.com/questions/9911659/can-i-use-if-not-exists-to-insert-a-row-if-the-row -s-not-already-in-in-s尤其是如果您允許同一個Excel文件的多個實例,則無法再控制哪些記錄已經插入到數據庫中,哪些記錄仍處於待處理狀態。除此之外,我會實施一個工作表事件來跟蹤哪些已上傳,哪些尚未上傳。但是這個解決方案不會像我提出的第一個那樣簡潔。 – Ralph

回答

0

無論之前或之後有刷新,SQL語句本身都不會避免重複。如果您有一個主鍵或複合索引限制其中一個插入的單元格值使用重複值,則可能會出現錯誤並中斷代碼,因爲當前沒有設置錯誤/異常處理。

考慮填充常規清理的臨時表(確切的結構副本的最終表)。然後,運行經典LEFT JOIN ... NULL/NOT EXISTS/NOT IN附加查詢以插入唯一值。此外,由於我將是不鼓勵不負責任的,下面採用ADO parameterization

SQL(運行一次)

CREATE TABLE OH_CU_WR_TEMPLATE_TEMP AS (
    SELECT * FROM OH_CU_WR_TEMPLATE 
) WITH NO DATA 

VBA

Dim cmd As ADODB.Command 
    Dim strSQL As String 
    ... 

    ' CLEAN OUT TEMP TABLE 
    conn.Execute "DELETE FROM OH_CU_WR_TEMPLATE_TEMP" 

    ' PREPARE STATEMENT (INSERTS INTO TEMP TABLE) 
    strSQL = "INSERT INTO OH_CU_WR_TEMPLATE_TEMP (BILL_NUMBER, ROCKTENN_DOC, " _ 
         & " ACTION, NOTE1, NOTE2)" _ 
         & " VALUES (?, ?, ?, ?, ?)" 

    ' ITERATE ROWS 
    Do Until .Cells(iRowNo, 2) = "" 
     ' ASSIGN VALUES 
     sBILL_NUM = .Cells(iRowNo, 2) 
     sROCKTENN_DOC = .Cells(iRowNo, 3) 
     sACTION = .Cells(iRowNo, 4) 
     sNOTE1 = .Cells(iRowNo, 5) 
     sNOTE2 = .Cells(iRowNo, 6) 

     ' UPDATE CMD OBJECT 
     Set cmd = New ADODB.Command 

     With cmd 
      .ActiveConnection = conn 
      .CommandText = strSQL 
      .CommandType = adCmdText 
      .CommandTimeout = 15 
     End With 

     ' BIND PARAMETERS 
     cmd.Parameters.Append cmd.CreateParameter("param1", adVarChar, adParamInput, 255, sBILL_NUM) 
     cmd.Parameters.Append cmd.CreateParameter("param2", adVarChar, adParamInput, 255, sROCKTENN_DOC) 
     cmd.Parameters.Append cmd.CreateParameter("param3", adVarChar, adParamInput, 255, sAction) 
     cmd.Parameters.Append cmd.CreateParameter("param4", adVarChar, adParamInput, 255, sNOTE1) 
     cmd.Parameters.Append cmd.CreateParameter("param5", adVarChar, adParamInput, 255, sNOTE2) 

     ' EXECUTE SQL 
     cmd.Execute 
     iRowNo = iRowNo + 1 
    Loop 

    ' NON-DUPLICATE APPEND QUERY (USE APPROPRIATE JOIN IDs) 
    conn.Execute "INSERT INTO OH_CU_WR_TEMPLATE" _ 
        & " SELECT * FROM OH_CU_WR_TEMPLATE_TEMP temp" _ 
        & " LEFT JOIN OH_CU_WR_TEMPLATE main" _ 
        & " ON temp.ID = main.ID" _ 
        & " WHERE main.ID IS NULL;"