Mastering Database Operations in ABAP: A Comprehensive Guide with Practical Examples
Database Operations :-
- Database operations deals with database tables.
- There are 5 database operations :-
- Select :- Select is used to fetch data from database tables.
- Insert :- Insert is used to insert the records to database tables.
- Update :- Update is used to update the existing records in the database tables.
- Delete :- Delete is used to delete the existing records from database tables.
- Modify :- It works for both - insert + update. For existing records - It acts as update, for non-existing records - It acts as insert.
Let’s take a requirement and understand these concepts.
Suppose, this is our order header table.
and below is data present in it.
Now, we will perform, insert, update, delete and modify in this table.
-
Step 1 :- Create a executable program and give 5 parameters for all the field of the above database tables.
-
Step 2 :- go to text elements and give a readable name foe these parameters.
-
Step 3 :- Declare radio button to perform respective database operations.
-
Step 4 :- Go to text elements and give respective radio buttons for the same.
1. Insert Operation :-
-
Step 1 :- Declare Internal and Work area for the table.
-
Step 2:- Write the insert operation.
-
Step 3 :- Execute the code and fill the records and press the execute the button.
-
Step 4 :- Let’s execute again and let’s enter the data with order number 6 again, then you will get the below output.
Note :- Insert Operation always gives dump for the duplicate records.
Requirement :-
- Now, suppose if Customer wants that, If the wrong input is there for order number then he should be getting a message on the selection screen rather than runtime error, So that he can rectify the mistake on the selection screen.
Solution :-
- Validate the input using At selection Screen Event.
-
Now execute the program for same order number 6.
- You will get the message as records exist for the same order number.
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,
p_r2 TYPE c RADIOBUTTON GROUP r1,
p_r3 TYPE c RADIOBUTTON GROUP r1,
p_r4 TYPE c RADIOBUTTON GROUP r1 DEFAULT 'X'.
*******************************************
***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.
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.
***********
*End of Program
**********************************************************
We will continue the rest portion in next blog.
Comments
Post a Comment