05-26-2008 1:49 PM
Hi all,
Can any one explain with example how to use Enque and Deque to lock a database table?
05-26-2008 2:21 PM
I think if you search the ABAP Forum you will get the answer.
Thanks
Arghadip
05-28-2008 5:35 AM
hi,
Activating a lock object in the ABAP Dictionary automatically creates function modules for setting (ENQUEUE_<lock object name>) and releasing (DEQUEUE_<lock object name>) locks.
check the link :
http://help.sap.com/saphelp_nw70/helpdata/en/41/7af4c8a79e11d1950f0000e82de14a/content.htm
05-28-2008 5:39 AM
hi,
check the below examle
check tcode abapdocu-->abapdatabaseacees--->setting and releasing locks
REPORT demo_transaction_enqueue MESSAGE-ID sabapdocu.
TABLES sflight.
DATA text(8) TYPE c.
DATA ok_code TYPE sy-ucomm.
CALL SCREEN 100.
MODULE init OUTPUT.
SET PF-STATUS 'BASIC'.
sflight-carrid = 'LH'. sflight-connid = '400'.
ENDMODULE.
MODULE exit INPUT.
LEAVE PROGRAM.
ENDMODULE.
MODULE enqueue INPUT.
CASE ok_code.
WHEN 'ENQUEUE'.
CALL FUNCTION 'ENQUEUE_EDEMOFLHT'
EXPORTING
mode_sflight = 'X'
carrid = sflight-carrid
connid = sflight-connid
fldate = sflight-fldate
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3.
CASE sy-subrc.
WHEN 0.
MESSAGE i888 WITH 'Enqueue successful'(001).
WHEN 1.
text = sy-msgv1.
MESSAGE e888 WITH 'Record already'(002) 'locked by'(003)
text.
CALL TRANSACTION 'SM12'.
WHEN 2 OR 3.
MESSAGE e888 WITH 'Error in enqueue!'(004)
'SY-SUBRC:' sy-subrc.
ENDCASE.
WHEN 'DEQUEUE'.
CALL FUNCTION 'DEQUEUE_EDEMOFLHT'
EXPORTING
mode_sflight = 'X'
carrid = sflight-carrid
connid = sflight-connid
fldate = sflight-fldate
EXCEPTIONS
OTHERS = 1.
CASE sy-subrc.
WHEN 0.
MESSAGE i888 WITH 'Dequeue successful'(005).
WHEN 1.
MESSAGE e888 WITH 'Error in dequeue!'(006).
ENDCASE.
WHEN 'SM12'.
CALL TRANSACTION 'SM12'.
ENDCASE.
ENDMODULE.
MODULE select INPUT.
CASE ok_code.
WHEN 'SELECT'.
SELECT * FROM sflight WHERE carrid = sflight-carrid
AND connid = sflight-connid
AND fldate = sflight-fldate.
ENDSELECT.
MESSAGE i888 WITH 'SY-SUBRC:' sy-subrc.
ENDCASE.
ENDMODULE.
reward if helpful
prasanth
05-28-2008 5:55 AM
Hi,
Check out this link. It would be helpful.
http://help.sap.com/saphelp_nw70/helpdata/en/41/7af4c5a79e11d1950f0000e82de14a/content.htm
Thanks
Reward points if useful
05-28-2008 5:58 AM
Hi,
To add on about the function modules-
Two function modules, one for setting locks and the other for releasing them, are automatically generated upon successful activation of the lock object in the ABAP Dictionary. They are of the form ENQUEUE_* and DEQUEUE_*. If we created the lock object E_MYLOCKOBJ, the corresponding locking and unlocking function modules generated would be ENQUEUE_ E_MYLOCKOBJ and DEQUEUE_E_MYLOCKOBJ, respectively.
http://articles.techrepublic.com.com/5100-10878_11-5066352.html
Thanks
Reward points if useful
05-28-2008 5:59 AM
Hi Rajashri,
Locking is quite simple..
lock the table using Function Module
ENQUEUE_TABLE_E (pass Table Name.)
....Write your update statements....
Now unlock the Table using Function Module
DEQUEUE_TABLE_E.
Please reward if useful
Manish Sharma
05-30-2008 12:03 PM
hi,
i am providing code to you.
&----
*& Report ZLOCKING *
*& *
&----
*& *
*& *
&----
REPORT zlocking .
DATA : tab_emp TYPE TABLE OF zemp_51772,
wa_emp TYPE zemp_51772.
wa_emp-emp_no = '102'.
wa_emp-emp_id = '157'.
wa_emp-emp_name = 'SIVA'.
wa_emp-emp_dept = 'SAP'.
APPEND wa_emp TO tab_emp.
CLEAR wa_emp.
wa_emp-emp_no = '128'.
wa_emp-emp_id = '138'.
wa_emp-emp_name = 'RAMA'.
wa_emp-emp_dept = 'SAP'.
APPEND wa_emp TO tab_emp.
CLEAR wa_emp.
wa_emp-emp_no = '133'.
wa_emp-emp_id = '121'.
wa_emp-emp_name = 'KRISHNA'.
wa_emp-emp_dept = 'SAP'.
APPEND wa_emp TO tab_emp.
CLEAR wa_emp.
CALL FUNCTION 'ENQUEUE_E_TABLEE'
EXPORTING
mode_rstable = 'X'
tabname = 'ZEMP_51772'
varkey =
X_TABNAME = ' '
X_VARKEY = ' '
_SCOPE = '2'
_WAIT = ' '
_COLLECT = ' '
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3
.
IF sy-subrc EQ 0.
INSERT zemp_51772 FROM TABLE tab_emp.
ENDIF.
CALL FUNCTION 'DEQUEUE_E_TABLEE'
EXPORTING
mode_rstable = 'X'
tabname = 'ZEMP_51772'
VARKEY =
X_TABNAME = ' '
X_VARKEY = ' '
_SCOPE = '3'
_SYNCHRON = ' '
_COLLECT = ' '
.
IF sy-subrc EQ 0.
WRITE:/ 'Unlocked the Table'.
ENDIF.
now you will get it how to use enque and dequeue
regards,
vipul
05-30-2008 12:07 PM
this editable alv using this to make change in standard table.
&----
*& Report ZALVF
*&
&----
*&
*&
&----
REPORT ZALVF.
*******************************************************************
TYPE-POOLS *
*******************************************************************
TYPE-POOLS: SLIS.
*******************************************************************
INTERNAL TABLES/WORK AREAS/VARIABLES *
*******************************************************************
DATA: I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
I_INDEX TYPE STANDARD TABLE OF I WITH HEADER LINE,
W_FIELD TYPE SLIS_FIELDCAT_ALV,
P_TABLE LIKE DD02L-TABNAME,
DY_TABLE TYPE REF TO DATA,
DY_TAB TYPE REF TO DATA,
DY_LINE TYPE REF TO DATA.
*******************************************************************
FIELD-SYMBOLS *
*******************************************************************
FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
<DYN_WA> TYPE ANY,
<DYN_FIELD> TYPE ANY,
<DYN_TAB_TEMP> TYPE STANDARD TABLE.
*******************************************************************
SELECTION SCREEN *
*******************************************************************
PARAMETERS: TABNAME(30) TYPE C,
LINES(100) TYPE N.
*******************************************************************
START-OF-SELECTION *
*******************************************************************
START-OF-SELECTION.
Storing table name
P_TABLE = TABNAME.
Create internal table dynamically with the stucture of table name
entered in the selection screen
CREATE DATA DY_TABLE TYPE STANDARD TABLE OF (P_TABLE).
ASSIGN DY_TABLE->* TO <DYN_TABLE>.
IF SY-SUBRC <> 0.
MESSAGE I000(Z_ZZZ_CA_MESSAGES) WITH ' No table found'.
LEAVE TO LIST-PROCESSING.
ENDIF.
Create workarea for the table
CREATE DATA DY_LINE LIKE LINE OF <DYN_TABLE>.
ASSIGN DY_LINE->* TO <DYN_WA>.
Create another temp. table
CREATE DATA DY_TAB TYPE STANDARD TABLE OF (P_TABLE).
ASSIGN DY_TAB->* TO <DYN_TAB_TEMP>.
SORT I_FIELDCAT BY COL_POS.
Select data from table
SELECT * FROM (P_TABLE)
INTO TABLE <DYN_TABLE>
UP TO LINES ROWS.
REFRESH <DYN_TAB_TEMP>.
Display report
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_STRUCTURE_NAME = P_TABLE
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
TABLES
T_OUTTAB = <DYN_TABLE>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
ENDIF.
&----
*& Form SET_PF_STATUS
&----
Setting custom PF-Status
----
-->RT_EXTAB Excluding table
----
FORM SET_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
SET PF-STATUS 'Z_STANDARD'.
ENDFORM. "SET_PF_STATUS
&----
*& Form user_command
&----
Handling custom function codes
----
-->R_UCOMM Function code value
-->RS_SELFIELD Info. of cursor position in ALV
----
FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
Local data declaration
DATA: LI_TAB TYPE REF TO DATA,
L_LINE TYPE REF TO DATA.
Local field-symbols
FIELD-SYMBOLS:<L_TAB> TYPE TABLE,
<L_WA> TYPE ANY.
Create table
CREATE DATA LI_TAB TYPE STANDARD TABLE OF (P_TABLE).
ASSIGN LI_TAB->* TO <L_TAB>.
Create workarea
CREATE DATA L_LINE LIKE LINE OF <L_TAB>.
ASSIGN L_LINE->* TO <L_WA>.
CASE R_UCOMM.
When a record is selected
WHEN '&IC1'.
Read the selected record
READ TABLE <DYN_TABLE> ASSIGNING <DYN_WA> INDEX
RS_SELFIELD-TABINDEX.
IF SY-SUBRC = 0.
Store the record in an internal table
APPEND <DYN_WA> TO <L_TAB>.
Fetch the field catalog info
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = 'Z_DEMO_PDF_JG'
I_STRUCTURE_NAME = P_TABLE
CHANGING
CT_FIELDCAT = I_FIELDCAT
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
IF SY-SUBRC = 0.
Make all the fields input enabled except key fields
W_FIELD-INPUT = 'X'.
MODIFY I_FIELDCAT FROM W_FIELD TRANSPORTING INPUT
WHERE KEY IS INITIAL.
ENDIF.
Display the record for editing purpose
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_STRUCTURE_NAME = P_TABLE
IT_FIELDCAT = I_FIELDCAT
I_SCREEN_START_COLUMN = 10
I_SCREEN_START_LINE = 15
I_SCREEN_END_COLUMN = 200
I_SCREEN_END_LINE = 20
TABLES
T_OUTTAB = <L_TAB>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC = 0.
Read the modified data
READ TABLE <L_TAB> INDEX 1 INTO <L_WA>.
If the record is changed then track its index no.
and populate it in an internal table for future
action
IF SY-SUBRC = 0 AND <DYN_WA> <> <L_WA>.
<DYN_WA> = <L_WA>.
I_INDEX = RS_SELFIELD-TABINDEX.
APPEND I_INDEX.
ENDIF.
ENDIF.
ENDIF.
When save button is pressed
WHEN 'SAVE'.
Sort the index table
SORT I_INDEX.
Delete all duplicate records
DELETE ADJACENT DUPLICATES FROM I_INDEX.
LOOP AT I_INDEX.
Find out the changes in the internal table
and populate these changes in another internal table
READ TABLE <DYN_TABLE> ASSIGNING <DYN_WA> INDEX I_INDEX.
IF SY-SUBRC = 0.
APPEND <DYN_WA> TO <DYN_TAB_TEMP>.
ENDIF.
ENDLOOP.
Lock the table
CALL FUNCTION 'ENQUEUE_E_TABLE'
EXPORTING
MODE_RSTABLE = 'E'
TABNAME = P_TABLE
EXCEPTIONS
FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2
OTHERS = 3.
IF SY-SUBRC = 0.
Modify the database table with these changes
MODIFY (P_TABLE) FROM TABLE <DYN_TAB_TEMP>.
REFRESH <DYN_TAB_TEMP>.
Unlock the table
CALL FUNCTION 'DEQUEUE_E_TABLE'
EXPORTING
MODE_RSTABLE = 'E'
TABNAME = P_TABLE.
ENDIF.
RS_SELFIELD-REFRESH = 'X'.
WHEN 'EXIT'.
LEAVE PROGRAM.
ENDCASE.
ENDFORM. "user_command