MichaelB.
7/25/2018 - 6:25 PM

MODIFY, update or insert

A combination of INSERT or UPDATE statement. Use these 2 statements where you can, because they offer more clarity. Sometimes you need MODIFY to insert or update, depending on the situation. Primary keys in work area are checked against those in the table. If record exists, record will be updated. If not, record will be inserted. If MODIFY statement is not able to execute either of these statements, sy-subrc = 4, if successful, it is 0. Modify in a loop only changes the contents of the existing line in the internal table that we have read. It does not create a new record. If a MODIFY statement is in a loop, it is always the current line that is changed. Do not try and modify key fields of an internal table with a unique key. If MODIFY is used outside of a loop, we must specify the record index number of an internal table. In a loop the system monitors the index number automatically.

**********************
*** - MODIFY

wa_employees-employee = '10000006'. "Record should already be inserted, so UPDATE record should be executed
wa_employees-surname = 'NORTHMORE'.
wa_employees-forename = 'PETER'.
wa_employees-title = 'MR'.
wa_employees-dob = '19921213'.

MODIFY zemployees FROM wa_employees.

IF sy-subrc = 0.
  WRITE: / 'Record Modified Correctly'.
ELSE.
  WRITE: / 'We have a return code of ', sy-subrc.
ENDIF.
**********************
CLEAR wa_employees.

wa_employees-employee = '10000007'. "new record so INSERT statement should be executed
wa_employees-surname = 'SOUTHMORE'.
wa_employees-forename = 'SUSAN'.
wa_employees-title = 'MRS'.
wa_employees-dob = '19921213'.

MODIFY zemployees FROM wa_employees.

IF sy-subrc = 0.
  WRITE: / 'Record Modified Correctly'.
ELSE.
  WRITE: / 'We have a return code of ', sy-subrc.
ENDIF.