使用顺序主键插入查询

时间:2012-09-20 17:09:29

标签: ms-access

我正在使用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;

1 个答案:

答案 0 :(得分: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