Posted: Sat Oct 13, 2007 2:00 pm Post subject: Database locking (prevent inconsistent data)
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)
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.
IF sy-subrc <> 0.
* Retrieve message displayed within Function Module
message id sy-msgid
type 'I'
number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
Step 3 - ABAP code to Remove table lock(s)
The following code will remove the lock for the specific table entries.
Age: 85 Joined: 04 Oct 2007 Posts: 1218 Location: Санкт-Петербург
Posted: Tue Feb 28, 2012 3:31 pm Post subject:
Code:
*&---------------------------------------------------------------------*
*& Form SAVE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM save USING p_confirm TYPE as4flag.
CONSTANTS: lc_save TYPE c VALUE '1'.
DATA: l_answer TYPE c.
DATA: ls_hist TYPE zeruf_ndssb_hist,
lt_hist TYPE TABLE OF zeruf_ndssb_hist.
FIELD-SYMBOLS <fs> TYPE t_sb.
READ TABLE gt_alvdata WITH KEY changed = 'X'.
CHECK sy-subrc IS INITIAL.
IF NOT p_confirm IS INITIAL.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = 'Confirm'(301)
text_question = 'Data was changed. Are you want to save?'(302)
text_button_1 = 'Yes'
text_button_2 = 'No'
default_button = '1'
display_cancel_button = ''
IMPORTING
answer = l_answer
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
ELSE.
l_answer = lc_save.
ENDIF.
IF l_answer = lc_save.
LOOP AT gt_alvdata ASSIGNING <fs> WHERE NOT changed IS INITIAL.
MOVE-CORRESPONDING <fs> TO ls_hist.
ls_hist-bukrs = p_bukrs.
APPEND ls_hist TO lt_hist.
ENDLOOP.
DATA: l_garg LIKE seqg3-garg,
lt_enq LIKE seqg3 OCCURS 1 WITH HEADER LINE,
l_ebeln TYPE rseg-ebeln,
l_lock.
DO 30 TIMES.
CLEAR l_lock.
REFRESH lt_enq.
CONCATENATE sy-mandt '*' INTO l_garg.
CALL FUNCTION 'ENQUEUE_READ'
EXPORTING
gclient = sy-mandt
gname = 'ZERUF_NDSSB_HIST'
garg = l_garg
guname = sy-uname
TABLES
enq = lt_enq
EXCEPTIONS
communication_failure = 1
system_failure = 2
OTHERS = 3.
IF NOT sy-subrc IS INITIAL OR NOT lt_enq[] IS INITIAL.
l_lock = 'X'.
ENDIF.
IF NOT l_lock IS INITIAL.
WAIT UP TO 2 SECONDS.
ELSE.
EXIT.
ENDIF.
ENDDO.
IF sy-subrc <> 0.
* Retrieve message displayed within Function Module
MESSAGE ID sy-msgid
TYPE 'I'
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
MODIFY zeruf_ndssb_hist FROM TABLE lt_hist.
COMMIT WORK AND WAIT.
IF sy-subrc IS INITIAL.
CLEAR gt_alvdata-changed.
MODIFY gt_alvdata FROM gt_alvdata TRANSPORTING changed
WHERE NOT changed IS INITIAL.
ENDIF.
CALL FUNCTION 'DEQUEUE_EZ_NDSSB_HIST'
EXPORTING
mode_zeruf_ndssb_hist = 'E'
mandt = sy-mandt.
ENDIF.
ENDFORM. " SAVE
_________________ Молитва - это запрос разработчику на изменение кода программы.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.