✒️ABAP Las sentencias INSERT, UPDATE, MODIFY y DELETE
ABAP Las sentencias INSERT, UPDATE, MODIFY y DELETE
INSERT Statement
The INSERT statement facilitates the introduction or insertion of new records into a base database table. Always check the value of the system variable SY-SUBRC after executing the INSERT statement. A value of zero indicates successful insertion; otherwise, an error occurred, and the record was not inserted.
DATA: ls_new_record TYPE database_table.
ls_new_record-field1 = value1.
ls_new_record-field2 = value2.
INSERT database_table FROM ls_new_record.
IF sy-subrc = 0.
" Record inserted successfully.
ELSE.
" Error occurred during insertion.
ENDIF.
* Example: Inserting various elements from an internal table
DATA: lt_users_to_insert TYPE TABLE OF lt_employee_data,
lt_new_users TYPE TABLE OF lt_employee_data.
SELECT * FROM lt_employee_data INTO TABLE lt_new_users.
APPEND VALUE #( client = sy-mandt
id_number = 'NewID123'
full_name = 'New Employee1'
status = 'Active' ) TO lt_users_to_insert.
APPEND VALUE #( client = sy-mandt
id_number = 'NewID456'
full_name = 'New Employee2'
status = 'Active' ) TO lt_users_to_insert.
INSERT lt_employee_data FROM TABLE lt_users_to_insert.
IF sy-subrc = 0.
" Records inserted successfully.
ELSE.
" Error occurred during insertion.
ENDIF.
UPDATE Statement
The UPDATE statement allows the modification of one or more records in a base database table. After executing the UPDATE statement, check the value of the system variable SY-SUBRC. A value of zero signifies a successful update; otherwise, an error occurred, and the record was not updated.
* Example 1: Updating a specific record
CLEAR ls_user_to_update.
ls_user_to_update-client = sy-mandt.
ls_user_to_update-id_number = 'UniqueID123'.
ls_user_to_update-full_name = 'Jega'.
ls_user_to_update-status = 'Active'.
UPDATE z_users FROM ls_user_to_update.
IF sy-subrc = 0.
" Specific record updated successfully.
ELSE.
" Error occurred during update.
ENDIF.
* Example 2: Updating records based on a condition
UPDATE z_users SET full_name = 'JEGA'
WHERE id_number = 'ConditionID123'.
IF sy-subrc = 0.
" Records updated based on condition successfully.
ELSE.
" Error occurred during update.
ENDIF.
* Example 3: Updating all records from an internal table
SELECT * FROM z_users INTO TABLE lt_internal_users.
UPDATE z_users FROM TABLE lt_internal_users.
IF sy-subrc = 0.
" All records updated from internal table successfully.
ELSE.
" Error occurred during update.
ENDIF.
MODIFY Statement
The MODIFY statement permits the modification of one or more records in a base database table. Always check the value of the system variable SY-SUBRC after executing the MODIFY statement. A value of zero indicates successful modification; otherwise, an error occurred, and the record was not modified.
DATA: ls_record_to_modify TYPE database_table.
ls_record_to_modify-field1 = value1.
ls_record_to_modify-field2 = value2.
MODIFY database_table FROM ls_record_to_modify WHERE condition.
IF sy-subrc = 0.
" Record modified/created successfully.
ELSE.
" Error occurred during modification.
ENDIF.
* Example: Modifying all records in the database table based on an internal table
DATA: lt_users_to_modify TYPE TABLE OF ztabla_usuarios.
SELECT * FROM ztabla_usuarios INTO TABLE lt_users_to_modify.
MODIFY ztabla_usuarios FROM TABLE lt_users_to_modify.
IF sy-subrc = 0.
" All records modified successfully.
ELSE.
" Error occurred during modification.
ENDIF.
Note: If the record being modified doesn't exist in the database table, it will be created. Suppose we want to be very careful about which group our record belongs to when updating a database table. In that case, we use the 'CLIENT SPECIFY' clause to control and organize the information just the way we want it.
DELETE Statement
The DELETE statement is used to delete the content of one or more records from a base database table. After executing the DELETE statement, check the value of the system variable SY-SUBRC. A value of zero signifies successful deletion; otherwise, an error occurred, and the record was not deleted. The number of deleted records is stored in the system variable SY-DBCNT.
DELETE FROM database_table WHERE condition.
IF sy-subrc = 0.
" Record deleted successfully.
ELSE.
" Error occurred during deletion.
ENDIF.
* Example: Deleting all records from the database table based on an internal table
DATA: lt_users_to_delete TYPE TABLE OF ztabla_usuarios.
SELECT * FROM ztabla_usuarios INTO TABLE lt_users_to_delete.
DELETE ztabla_usuarios FROM TABLE lt_users_to_delete.
IF sy-subrc = 0.
" All records deleted successfully.
ELSE.
" Error occurred during deletion.
ENDIF.
Always attempt to insert or modify data through SAP's standard transactions or other techniques discussed later. It's essential to use INSERT, UPDATE, MODIFY, and DELETE just if it is strictly needed.
E.g
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_11
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_11.
* Declaration of Structure
DATA: it_users TYPE STANDARD TABLE OF zuser_table_jega,
wa_users LIKE LINE OF it_users.
* Declaration of Variables
DATA: v_dni TYPE z_dni,
v_nomape TYPE z_nombre_ape,
v_estado TYPE z_estado_usu,
v_birthdate TYPE z_bd.
*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS: p_dni LIKE v_dni OBLIGATORY, " DNI
p_nomape LIKE v_nomape, " Name and Surname
p_estado LIKE v_estado, " User Status
p_bd LIKE v_birthdate. " Birthdate
SELECTION-SCREEN SKIP 1.
PARAMETERS: p_create RADIOBUTTON GROUP rbg, " Create
p_modify RADIOBUTTON GROUP rbg, " Modify
p_delete RADIOBUTTON GROUP rbg. " Delete
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
* Create Users
IF p_create EQ 'X'.
CLEAR v_dni.
SELECT SINGLE dni
FROM zuser_table_jega
INTO v_dni
WHERE dni EQ p_dni.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record you are trying to create already exists'
DISPLAY LIKE 'E'.
ELSE.
CLEAR wa_users.
wa_users-mandt = sy-mandt.
wa_users-dni = p_dni.
wa_users-nombre_ape = p_nomape.
wa_users-estado = p_estado.
wa_users-birthdate = p_bd.
INSERT INTO zuser_table_jega VALUES wa_users.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record was created successfully'.
ENDIF.
ENDIF.
* Modify Users
ELSEIF p_modify EQ 'X'.
CLEAR v_dni.
SELECT SINGLE dni
FROM zuser_table_jega
INTO v_dni
WHERE dni EQ p_dni.
IF sy-subrc NE 0.
MESSAGE s001(z_prueba) WITH 'The record you are trying to modify does not exist'
DISPLAY LIKE 'E'.
ELSE.
CLEAR wa_users.
wa_users-mandt = sy-mandt.
wa_users-dni = p_dni.
wa_users-nombre_ape = p_nomape.
wa_users-estado = p_estado.
wa_users-birthdate = p_bd.
MODIFY zuser_table_jega FROM wa_users.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record was modified successfully'.
ENDIF.
ENDIF.
* Delete Users
ELSEIF p_delete EQ 'X'.
CLEAR v_dni.
SELECT SINGLE dni
FROM zuser_table_jega
INTO v_dni
WHERE dni EQ p_dni.
IF sy-subrc NE 0.
MESSAGE s001(z_prueba) WITH 'The record you are trying to delete does not exist'
DISPLAY LIKE 'E'.
ELSE.
CLEAR wa_users.
wa_users-mandt = sy-mandt.
wa_users-dni = p_dni.
wa_users-nombre_ape = p_nomape.
wa_users-estado = p_estado.
wa_users-birthdate = p_bd.
DELETE zuser_table_jega FROM wa_users.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record was deleted successfully'.
ENDIF.
ENDIF.
ENDIF.
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 99 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez