0

我正在使用 MS Access 追加查询将库存交易追加到我的 ERP 数据库 (MYSQL)。
请告知我将如何修改我的查询以自动将下一个顺序事务 ID(主键)插入到 Inventory_transaction 表中,并能够一次附加多条记录。

我现有的查询工作正常,但只有当我只附加一条记录时。
我通常需要同时追加多条记录。每条记录都需要有一个唯一的顺序事务 ID(主键)。将有多个用户同时使用此应用程序,因此我需要最小化重复密钥违规的机会,以防止回滚。我尝试在不使用主键的情况下进行附加,以查看我的数据库是否会自动分配事务 ID,但不幸的是,这个 ERP 字段不是自动编号,我无法修改表结构......

以下是 2 个查询。
这一项目前仅用于为一条记录生成事务 ID。

SELECT Max([SYSADM_INVENTORY_TRANS].[TRANSACTION_ID])+1 AS new_inventory_transaction_ID
FROM SYSADM_INVENTORY_TRANS;

第二个查询是包含第一个查询的附加查询,如果有人可以修改查询,我将非常感激,以便用户能够一次附加多个具有唯一事务 ID 的记录。

INSERT INTO SYSADM_INVENTORY_TRANS ( TRANSACTION_ID, WORKORDER_TYPE,
  WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, WORKORDER_SUB_ID,
  OPERATION_SEQ_NO, REQ_PIECE_NO, PART_ID, TYPE, CLASS, QTY, COSTED_QTY,
  TRANSACTION_DATE, WAREHOUSE_ID, LOCATION_ID, USER_ID, POSTING_CANDIDATE,
  ACT_MATERIAL_COST, ACT_LABOR_COST, ACT_BURDEN_COST, ACT_SERVICE_COST,
  CREATE_DATE, ADD_BURDEN, COUNT_SEQUENCE, DESCRIPTION )
SELECT T.new_inventory_transaction_ID, S.WORKORDER_TYPE, D.WORKORDER_BASE_ID,
  D.WORKORDER_LOT_ID, D.WORKORDER_SPLIT_ID, D.WORKORDER_SUB_ID, D.OPERATION_SEQ_NO,
  D.PIECE_NO, D.auto_issue_part_ID, S.TYPE, S.CLASS, D.[total_auto_issue Qty],
  0 AS Expr6, Date() AS Expr1, D.BACKFLUSH_WHS_ID, D.BACKFLUSH_LOC_ID,
  "SYSADM" AS Expr3, S.POSTING_CANDIDATE, S.ACT_MATERIAL_COST, S.ACT_LABOR_COST,
  S.ACT_BURDEN_COST, S.ACT_SERVICE_COST, Date() AS Expr2, S.ADD_BURDEN,
  S.COUNT_SEQUENCE, "ENTERED WITH ACCESS APP" AS Expr5
FROM tbl_static_autoissue_data AS S,
     tbl_dynamic_autoissue_data AS D,
     qry_transaction_ID_generator AS T;
4

1 回答 1

1

这里有一些注释可以帮助您实现目标,但是使用自动编号生活会更轻松、更安全。当您提到 MS Access 时,这是 VBA。

Function NextTranNumber(ByRef FirstTran As Long, _
         ByRef LastTran As Long, Optional BlockSize = 1)
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
Dim lngResult As Long
Dim strCon As String

    lngResult = 0  'assume fail

    strCon = TestCon ''Connection to back-end
    cn.Open strCon

    rs.CursorType = adOpenKeyset
    rs.LockType = adLockPessimistic
    rs.CursorLocation = adUseServer

    ''Where BEInfo is a single line table that holds a transaction seed
    strSQL = "SELECT ASeqNumber FROM BEInfo"

    rs.Open strSQL, cn, , , adCmdText

    'Note this is ADO, so no rs.Edit
    FirstTran = rs!ASeqNumber + 1
    rs!ASeqNumber = rs!ASeqNumber + BlockSize
    rs.Update

    LastTran = rs!ASeqNumber
    rs.Close
    Set rs = Nothing
End Function

Sub TransactionProcessing()
Dim FirstTran As Long
Dim LastTran As Long
Dim db As Database
Dim sSQL As String
Dim Block As Long
Dim rs As DAO.Recordset

    Set db = CurrentDb

    'Existing temporary table
    sSQL = "DELETE FROM FETempTrans"
    db.Execute sSQL, dbFailOnError
    'The records to be added to the main table
    sSQL = "INSERT INTO FETempTrans ( ID, AText ) SELECT 0 AS ID, AText FROM Table1"
    db.Execute sSQL, dbFailOnError

    Block = db.RecordsAffected

    'Reserve a transaction block based on the temp table count
    NextTranNumber FirstTran, LastTran, Block

    Set rs = db.OpenRecordset("FETempTrans")

    Do While Not rs.EOF
        rs.Edit
        rs!ID = FirstTran
        rs.Update
        FirstTran = FirstTran + 1
        rs.MoveNext
    Loop

    If FirstTran - 1 = LastTran Then
        'compare the temp set to the main table
        'if it passes, update the main table
    Else
        'fail
    End If
End Sub
于 2012-09-21T09:52:53.713 回答