对于单个插入操作,一些记录插入两次

时间:2015-04-29 12:14:29

标签: sql-server vb.net sql-server-2008

我创建了一个方法insert_detail,并且在该方法中,基于条件仅对每个表执行一次插入操作,但对于某些记录,它插入两次并在数据库中进行复制,此问题仅在一周内发生。所以请帮我找到解决方案。

Private Sub insert_detail(mservice_order_no, mprod_code, malt_prod_code, mqty, mtype, mfrom_original_hub, maxno)
On Error GoTo ErrTrap
    Dim mcmt As String
    Dim strSQL$

    If mfrom_original_hub = "TRUE" Then
        mcmt = "Y"
    Else
        mcmt = "N"
    End If

    strSQL = ""
    If mtype = "A" Then
        strSQL = "insert into service_order_alt_parts(fk_num_service_ord_no,fk_var_orig_prod_code," & _
            "fk_var_alt_prod_code,num_alt_prod_qty,num_flag,commited,From_CPL) " & _
            "values('" & mservice_order_no & "','" & mprod_code & "','" & malt_prod_code & "'," & _
            mqty & "," & maxno & ",'" & mcmt & "','TRUE')"

    ElseIf mtype = "R" Then
        strSQL = "insert into service_order_repl_parts(fk_num_service_ord_no,fk_var_orig_prod_code," & _
            "fk_var_repl_prod_code,num_repl_prod_qty,num_flag,commited,From_CPL) " & _
            "values('" & mservice_order_no & "','" & mprod_code & "','" & malt_prod_code & "'," & _
            mqty & "," & maxno & ",'" & mcmt & "','TRUE')"
    End If

    If strSQL <> "" Then conn.Execute strSQL

ErrTrap:
    If Err Then
        If Err.Number <> 5555 Then _
            Call WriteToCommonLog("insert_detail", Err.Number & ": " & Err.Description & " - " & strSQL)

        Err.Raise 55555, , ""
    End If
End Sub

我将insert_detail称为: -

If mstock_found = True Then
    search_for_alternate_parts_from_cpl = 1

    maxno = get_max_no(mservice_order_no, mprod_code, "A")
    For i = 1 To alt_collection.Count
        Call add_parts_to_collection(alt_collection.Item(i), "A", alt_qty_collection.Item(i))
        Call insert_detail(mservice_order_no, mprod_code, alt_collection.Item(i), alt_qty_collection.Item(i), "A", mfrom_original_hub, maxno)

        Call WriteToDebugLog("search_for_alternate_parts_from_cpl -> StockFound", mservice_order_no & " : " & mprod_code & " : (A)" & alt_collection.Item(i) & " : " & alt_qty_collection.Item(i) & " : " & mfrom_original_hub)
        If mfrom_original_hub = "TRUE" Then
            str3 = "update product with (RowLock) set commited=commited + " & CInt(alt_qty_collection.Item(i)) & " where productcode='" & alt_collection.Item(i) & "' and whseid='" & mwhseid & "'"
            conn.Execute (str3)
        End If
    Next i

    If mfrom_original_hub = "TRUE" Then
        str3 = "update Service_Order_Detail with (RowLock) set chr_ignore='Y' where fk_num_service_ord_no='" & mservice_order_no & "' and fk_var_orig_prod_code='" & mprod_code & "' "
        conn.Execute (str3)

        str3 = "update service_order_alt_parts with (RowLock) set chr_ignore='Y' where fk_num_service_ord_no='" & mservice_order_no & "' and fk_var_orig_prod_code='" & mprod_code & "' and num_flag <>" & maxno
        conn.Execute (str3)
    End If

    Set alt_collection = Nothing
    Set alt_qty_collection = Nothing
Else
    Set alt_collection = Nothing
    Set alt_qty_collection = Nothing

    search_for_alternate_parts_from_cpl = 0
End If

0 个答案:

没有答案