在某项目中,按客户要求协同联动程序(直送、非直送)在收货或交货时对查询出来的一条记录用BDC录屏的方式在前台操作,因为前台操作的时间可能比较长,在某一个人进行操作的时候,必须锁定这条行记录,防止其他人操作,这样就用到了行记录锁定函数,把查出来用ALV显示的记录的某几个可以唯一标识该行的字段作为关键字P_KEY,调用行记录锁函数LOCK_RECORD,对该行记录进行锁定。这样,即使别人查出该条记录,在进行操作,会有行记录锁定的提示。调用该函数处理完该行后,可以调UnLock_RECORD函数进行解锁。也可以不调用,程序退出后,系统能进行自动解锁。也可以通过事务代码SM12进行解锁。
这两个函数可以应用到所有的需要处理数据的程序中,防止几个人同时操作的并发。
函数如下:
*&———————————————————————*
*& Form LOCK_RECORD
*&———————————————————————*
* text 锁定
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM LOCK_RECORD USING P_KEY.
DATA: IT_SEQG3 TYPE SEQG3 OCCURS 01 WITH HEADER LINE.
DATA: GNAME LIKE SEQG3-GNAME, GARG LIKE SEQG3-GARG.
DATA: BEGIN OF %ZRECORD_LOCK,
MANDT TYPE ZRECORD_LOCK-MANDT,
FIELDKEY TYPE ZRECORD_LOCK-FIELDKEY,
END OF %ZRECORD_LOCK.
CALL ‘C_ENQ_WILDCARD’ ID ‘HEX0’ FIELD %ZRECORD_LOCK.
MOVE SY-MANDT TO: %ZRECORD_LOCK-MANDT.
IF NOT P_KEY IS INITIAL.
MOVE P_KEY TO: %ZRECORD_LOCK-FIELDKEY.
ENDIF.
GNAME = ‘ZRECORD_LOCK’.
GARG = %ZRECORD_LOCK.
CALL FUNCTION ‘ENQUEUE_READ’
EXPORTING
GCLIENT = SY-MANDT
GNAME = GNAME
GARG = GARG
TABLES
ENQ = IT_SEQG3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
IF IT_SEQG3 IS NOT INITIAL.
DATA:CMESSAGE(100) TYPE C.
CONCATENATE ‘所选择记录正由‘ IT_SEQG3-GUNAME ‘处理!’ INTO CMESSAGE.
CONDENSE CMESSAGE.
MESSAGE CMESSAGE TYPE ‘E’.
ELSE.
CALL FUNCTION ‘ENQUEUE_EZRECORD_LOCK’
EXPORTING
MODE_ZRECORD_LOCK = ‘E’
MANDT = SY-MANDT
FIELDKEY = P_KEY
X_FIELDKEY = ‘ ‘
_SCOPE = ‘2’
_WAIT = ‘ ‘
_COLLECT = ‘ ‘
EXCEPTIONS
FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.
ENDFORM.
*&———————————————————————*
*& Form UNLOCK_RECORD
*&———————————————————————*
* text 解锁
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM UNLOCK_RECORD USING P_KEY.
CALL FUNCTION ‘DEQUEUE_EZRECORD_LOCK’
EXPORTING
MODE_ZRECORD_LOCK = ‘E’
MANDT = SY-MANDT
FIELDKEY = P_KEY
X_FIELDKEY = ‘ ‘
_SCOPE = ‘3’
_SYNCHRON = ‘ ‘
_COLLECT = ‘ ‘.
ENDFORM.
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20016.html