使用顺序主键插入查询

Posted

技术标签:

【中文标题】使用顺序主键插入查询【英文标题】:insert query with sequential primary key 【发布时间】:2012-09-20 17:09:28 【问题描述】:

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

这里有一些说明可能会帮助您实现目标,但是使用自动编号会让生活变得更轻松、更安全。正如您提到的 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

【讨论】:

以上是关于使用顺序主键插入查询的主要内容,如果未能解决你的问题,请参考以下文章

根据主键在表上以正确的顺序插入行。

JSP实用教程(第三版 清华大学出版社)中遇到的问题和解释

mysql基本操作之增删改查

基于主键插入

如何在 SQLite 表中插入日期和查询日期

mysql索引