在单个SQL语句/事务中,我需要:
这些步骤的执行必须是原子的(因此,一旦我开始第1阶段,其他任何事务都必须执行第2步或第3步)
我找到了一种使用MERGE INTO和LOCK TABLE语句的方法,但我担心的是2到3之间的步骤。
第1-2阶段:lock table DOC_COUNT in exclusive mode;
MERGE INTO DOC_COUNT AS mt USING (
SELECT * FROM TABLE (
VALUES
(?SEND_VALUE?, ?RECEIVE_VALUE?,1)
)
) AS vt (SEND, RECEIVE, QTY) ON (mt.SEND = vt.SEND AND mt.RECEIVE = vt.RECEIVE)
WHEN MATCHED THEN
UPDATE SET QTY = mt.QTY + 1
WHEN NOT MATCHED THEN
INSERT (SEND, RECEIVE, QTY) VALUES (vt.SEND, vt.RECEIVE, (SELECT COUNT(DOCUMENT_ID) AS DOC_CC_COUNT FROM V_DOC_COUNT WHERE DOCUMENT_ID <= ?DOC_ID_VALUE? AND RECEIVE = ?RECEIVE_VALUE?));
阶段3:
SELECT QTY FROM DOC_COUNT where SEND= ?SEND_VALUE? AND RECEIVE = ?RECEIVE_VALUE?;
如何在不使用SELECT FROM MERGE语句更新/插入后检索值QTY(遗憾的是,我的DB2版本不支持该功能)并且没有并发问题?