You are on page 1of 4

Page 1 of 4

SAP Database Locking


SAP provides you with the ability to restrict access to data while the table is being updated. This is fairly simple to implement via the use of a lock object (Created in SE11). Step 1 - Create Lock object (SE11)

Page 2 of 4

Step 2 - ABAP code to lock table entries Add the following code in-order to create the table lock. This function module must be called before any update takes place. If a lock has already been taken out it will display the appropriate message.
CALL FUNCTION 'ENQUEUE_EZ_ZTABLENAME' EXPORTING mode_ZTABLENAME = 'E' "E = Write Lock, S = Read Lock, X = Exclusive not cumulative mandt = sy-mandt KEYFIELD1 = "Value KEYFIELD2 = "Value KEYFIELD3 = "Value ... * X_KEYFIELD1 = ' ' * X_KEYFIELD2 = ' ' * X_KEYFIELD3 = ' ' ... * _SCOPE = '2' * _WAIT = ' ' * _COLLECT = ' ' * If exceptions are not used, message is displayed within FM EXCEPTIONS FOREIGN_LOCK = 1 SYSTEM_FAILURE = 2 OTHERS = 3. * IF sy-subrc <> 0. Retrieve message displayed within Function Module

Page 3 of 4
message id sy-msgid type 'I' number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

***************OPTIONAL - Get more details of existing lock * Also within here you can also use ENQUEUE_READ to find out more details about the existing lock data: it_enq type STANDARD TABLE OF SEQG3, wa_enq like line of it_enq, ld_gname type SEQG3-GNAME, ld_garg type SEQG3-GARG, ld_gname = 'ZTABLENAME'. "This is the name of the lock object as shown in tcode SM12 CALL FUNCTION 'ENQUEUE_READ' EXPORTING GCLIENT = SY-MANDT GNAME = ld_gname "Lock opject name GUNAME = '*' "User name, default is SY-UNAME but need to use * to return locks for all users TABLES ENQ = it_enq EXCEPTIONS COMMUNICATION_FAILURE = 1 SYSTEM_FAILURE = 2 OTHERS = 3. "will need to check values returned by FM to check what values are needed to build ld_garg value concatenate sy-mandt KEYFIELD1 KEYFIELD2 KEYFIELD3 into ld_garg . loop at it_enq into wa_enq where garg cs ld_garg. exit. endloop. check sy-subrc eq 0. if wa_enq-guname eq sy-uname. "Entry already being updated by you else. "Entry already being updated by someone else endif. ***************OPTIONAL EXIT. ENDIF.

Step 3 - ABAP code to Remove table lock(s) The following code will remove the lock for the specific table entries.
CALL FUNCTION 'DEQUEUE_EZ_ZTABLENAME' EXPORTING MODE_ZTABLENAME = 'E' MANDT = SY-MANDT mandt = sy-mandt KEYFIELD1 = "Value KEYFIELD2 = "Value KEYFIELD3 = "Value ...

Page 4 of 4
* * * * * * X_KEYFIELD1 X_KEYFIELD2 X_KEYFIELD3 ... _SCOPE _SYNCHRON _COLLECT . = ' ' = ' ' = ' ' = '3' = ' ' = ' '