Mastering ABAP Database Operations: Delete and Update Operations Unveiled
Welcome back everyone, So we were discussing about Database operations and we saw the practical implementation of insert operation, let’s continue the rest.
2. Delete Operation :-
Requirement :-
- We will use our previous program, that we used during insert operation.
- We will delete the matching record based upon order number.
- Also, I want that when I tick the delete radio button, then the rest of the parameters should be invisible.
Implementation :-
-
Step 1 :- Use At selection screen output event to make sure that, When I tick the delete radio button, the rest of the radio buttons gets invisible.
-
Step 2 :- Write a message in At selection screen event to show that, if the order number does not exist then, a message should be displayed on the selection screen.
-
Step 3 :- Write the delete query in start of selection event.
Code :-
*********************************************
*Start of Program
**********
*Declaring input parameters
PARAMETERS : p_ono TYPE zar_ono OBLIGATORY,
p_odt TYPE zar_odate,
p_pm TYPE zar_pm,
p_ta TYPE zar_tc,
p_cur TYPE zar_cur.
***************************
*Declare radio buttons for database Operations.
PARAMETERS : p_r1 TYPE c RADIOBUTTON GROUP r1 MODIF ID ins,
p_r2 TYPE c RADIOBUTTON GROUP r1 MODIF ID del,
p_r3 TYPE c RADIOBUTTON GROUP r1 MODIF ID upd,
p_r4 TYPE c RADIOBUTTON GROUP r1 DEFAULT 'X' MODIF ID cur.
*******************************************
***Declaring Internal table and work area
DATA : lt_header TYPE TABLE OF zar_order_header,
ls_header TYPE zar_order_header.
AT SELECTION-SCREEN.
IF p_r1 = 'X'.
SELECT * FROM zar_order_header
INTO TABLE lt_header
WHERE order_no = p_ono.
IF sy-subrc EQ 0.
MESSAGE 'Records exist for the same order nummber' TYPE 'E'.
ENDIF.
CLEAR lt_header.
ENDIF.
IF p_r2 = 'X'.
SELECT SINGLE * FROM zar_order_header
INTO ls_header
WHERE order_no = p_ono.
IF sy-subrc NE 0.
MESSAGE 'order_no does not exist' TYPE 'E'.
ENDIF.
ENDIF.
*************************************
*Modifying the Selection Screen for Delete Operation.
AT SELECTION-SCREEN OUTPUT.
IF p_r2 = 'X'.
LOOP AT SCREEN.
IF screen-group1 = 'INS' OR screen-group1 = 'UPD' OR screen-group1 = 'CUR'..
screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.
START-OF-SELECTION.
********************
*Insert Operation
IF p_r1 = 'X'.
ls_header-order_no = p_ono.
ls_header-order_date = p_odt.
ls_header-payment_mode = p_pm.
ls_header-total_amount = p_ta.
ls_header-currency = p_cur.
APPEND ls_header TO lt_header.
CLEAR ls_header.
INSERT zar_order_header FROM TABLE lt_header.
IF sy-subrc EQ 0.
MESSAGE 'Records inserted Successfully' TYPE 'S'.
ENDIF.
ENDIF.
****************************
*Delete Operation
IF p_r2 = 'X'.
ls_header-order_no = p_ono.
DELETE zar_order_header from ls_header.
IF sy-subrc eq 0.
MESSAGE 'Record has been successfully deleted' type 'S'.
ENDIF.
ENDIF.
***********
*End of Program
**********************************************************
Execute the Program :-
-
Select the delete radio button, and give a order number, that does not exist in table.
-
Now, give a order number that exist and press F8.
3. Update :-
Requirement :-
- If user is passing a wrong order number, then we will show a message, that order number does not exist.
- If user will give correct order number that exist in our table, then we will display the details of that order number, so that user can change/update the details of it.
Implementation :-
-
Step 1 :- Write a select query in At Selection Screen event to validate the order number entered by user is correct or not.
Also, in else condition if order number is correct, we will display the order details.
-
Step 2 :- Now, data has been fetched into the work area for correct order number, we need to bind it with our parameters, So we will use At Selection screen output event.
-
Step 3 :- Now We have our data in parameters, so user can change it on selection screen.
-
Step 4 :- We will write the update logic on start of selection, so that if user performs any changes, then if he press F8 the changes can be updated.
Code :-
*********************************************
*Start of Program
*****************
*Type structure
TYPES : BEGIN OF ty_data,
order_date TYPE zar_odate,
payment_mode TYPE zar_pm,
total_amount TYPE zar_tc,
currency TYPE zar_cur,
END OF ty_data.
DATA : ls_data TYPE ty_data.
**********
*Declaring input parameters
PARAMETERS : p_ono TYPE zar_ono OBLIGATORY,
p_odt TYPE zar_odate,
p_pm TYPE zar_pm,
p_ta TYPE zar_tc,
p_cur TYPE zar_cur.
***************************
*Declare radio buttons for database Operations.
PARAMETERS : p_r1 TYPE c RADIOBUTTON GROUP r1 MODIF ID ins,
p_r2 TYPE c RADIOBUTTON GROUP r1 MODIF ID del,
p_r3 TYPE c RADIOBUTTON GROUP r1 MODIF ID upd,
p_r4 TYPE c RADIOBUTTON GROUP r1 DEFAULT 'X' MODIF ID cur.
*******************************************
***Declaring Internal table and work area
DATA : lt_header TYPE TABLE OF zar_order_header,
ls_header TYPE zar_order_header.
AT SELECTION-SCREEN.
IF p_r1 = 'X'.
SELECT * FROM zar_order_header
INTO TABLE lt_header
WHERE order_no = p_ono.
IF sy-subrc EQ 0.
MESSAGE 'Records exist for the same order nummber' TYPE 'E'.
ENDIF.
CLEAR lt_header.
ENDIF.
IF p_r2 = 'X'.
SELECT SINGLE * FROM zar_order_header
INTO ls_header
WHERE order_no = p_ono.
IF sy-subrc NE 0.
MESSAGE 'order_no does not exist' TYPE 'E'.
ENDIF.
ENDIF.
IF p_r3 = 'X'.
SELECT SINGLE * FROM zar_order_header
INTO ls_header
WHERE order_no = p_ono.
IF sy-subrc NE 0.
MESSAGE 'order_no does not exist' TYPE 'E'.
ELSE.
SELECT SINGLE order_date payment_mode total_amount currency
FROM zar_order_header INTO ls_data WHERE order_no = p_ono.
ENDIF.
ENDIF.
*************************************
*Modifying the Selection Screen for Delete Operation.
AT SELECTION-SCREEN OUTPUT.
IF p_r2 = 'X'.
LOOP AT SCREEN.
IF screen-group1 = 'INS' OR screen-group1 = 'UPD' OR screen-group1 = 'CUR'..
screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.
***************************
*For Update
IF p_r3 = 'X'.
p_odt = ls_data-order_date.
p_pm = ls_data-payment_mode.
p_ta = ls_data-total_amount.
p_cur = ls_data-currency.
ENDIF.
START-OF-SELECTION.
********************
*Insert Operation
IF p_r1 = 'X'.
ls_header-order_no = p_ono.
ls_header-order_date = p_odt.
ls_header-payment_mode = p_pm.
ls_header-total_amount = p_ta.
ls_header-currency = p_cur.
APPEND ls_header TO lt_header.
CLEAR ls_header.
INSERT zar_order_header FROM TABLE lt_header.
IF sy-subrc EQ 0.
MESSAGE 'Records inserted Successfully' TYPE 'S'.
ENDIF.
ENDIF.
****************************
*Delete Operation
IF p_r2 = 'X'.
ls_header-order_no = p_ono.
DELETE zar_order_header FROM ls_header.
IF sy-subrc EQ 0.
MESSAGE 'Record has been successfully deleted' TYPE 'S'.
ENDIF.
ENDIF.
***********
*Update
IF p_r3 = 'X'.
ls_header-order_no = p_ono.
ls_header-order_date = p_odt.
ls_header-payment_mode = p_pm.
ls_header-total_amount = p_ta.
ls_header-currency = p_cur.
UPDATE zar_order_header FROM ls_header.
IF sy-subrc EQ 0.
MESSAGE 'Data Updated' TYPE 'S'.
ENDIF.
ENDIF.
**********************
*End of Program
**********************************************************
Let’s execute our program :-
-
Select the update radio button
-
Give a wrong order number which does not exist and press enter.
-
Now, give a correct order number which exist in table and press enter.
-
Now, suppose , I change the date and payment mode and press F8.
- I got the message data updated.
So, this was all about Classical Report, We will start our next chapter in the next blog.
Thanx a lot for being a part of this wonderful journey.
Comments
Post a Comment