abap对行记录进行锁定
在实际的运用中,可能对查询到的alv中的数据进行某些操作,而在操作的过程中,需要对正操作的数据进行锁定,防止在操作的过程中,他人进行操作,可以用以下方法对所操作的数据行进行锁定.
具体操作步骤:
1. 使用TCODE: SE11,选择最后一项,输入名称(注意一定要以字母‘E’开头,这是SAP内部规定的,如EZTESTING)
2. 输入名称后,按新增,进入界面后输入要加锁的Table名,及加锁类型(E:专用,累计;S:共享;X:专用,不累计)。保存后,系统会自动生成两个函数: ENQUEUE_EZTESTING(加锁)和 DEQUEUE_EZTESTING(解锁)。
*&---------------------------------*
*& Report Z009
*&
*&---------------------------------*
*&
*&
*&---------------------------------*
REPORT ZLXY.
perform LOCK_RECORD using 'liuxy2'.
*锁行
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 %ZTESTING,
MANDT TYPE ZTESTING-MANDT,
FIELDKEY TYPE ZTESTING-FIELDKEY,
END OF %ZTESTING.
CALL 'C_ENQ_WILDCARD' ID 'HEX0' FIELD %ZTESTING.
MOVE SY-MANDT TO: %ZTESTING-MANDT.
IF NOT P_KEY IS INITIAL.
MOVE P_KEY TO: %ZTESTING-FIELDKEY.
ENDIF.
GNAME = 'ZTESTING'.
GARG = %ZTESTING.
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 P_KEY '正由' IT_SEQG3-GUNAME '处理!' INTO CMESSAGE.
CONDENSE CMESSAGE.
MESSAGE CMESSAGE TYPE 'E'.
ELSE.
CALL FUNCTION 'ENQUEUE_EZTESTING'
EXPORTING
MODE_ZTESTING = '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 USING P_KEY.
CALL FUNCTION 'DEQUEUE_EZTESTING'
EXPORTING
MODE_ZTESTING = 'E'
MANDT = SY-MANDT
ZFIELDKEY = P_KEY
X_ZFIELDKEY = ' '
_SCOPE = '3'
_SYNCHRON = ' '
_COLLECT = ' '.
ENDFORM.