MichaelB.
7/23/2018 - 8:35 PM

INSERT new records

Allows us to insert new records into table. Also allows us to insert records into any position in an internal table by specifying the index number. INDEX 10 would insert a record between the 9th and 10th record. INSERT table_name (cannot be variable). sy-subrc = 4 is a warning (record with same key already exists) sy-subrc = 8 is an error or means the insert was unsuccessful.

DATA wa_employees LIKE zemployees.

wa_employees-EMPLOYEE = '10000006'.
wa_employees-SURNAME = 'WESTMORE'.
wa_employees-FORENAME = 'BRUCE'.
wa_employees-TITLE = 'MR'.
wa_employees-DOB = '19921213'.

INSERT zemployees FROM wa_employees.

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

*****************************************************
* insert based on index number with work area

TABLES zemployees.

*New Way of defining Internal Tables
*Declare a Line Type
TYPES: BEGIN OF line01_typ,
         surname LIKE zemployees-surname,
         dob     LIKE zemployees-dob,
       END OF line01_typ.

*Declare the 'Table Type' based on the Line Type.
TYPES itab02_typ TYPE STANDARD TABLE OF line01_typ.

*Declare the table based on the 'Table Type'
DATA itab02 TYPE itab02_typ.

*Declare the Work Area to use with our Internal Table
DATA wa_itab02 TYPE line01_typ.

DATA line_cnt TYPE i.

SELECT * FROM zemployees
  INTO CORRESPONDING FIELDS OF TABLE itab02.

LOOP AT itab01 INTO wa_itab02.
  WRITE wa_itab02-surname.
ENDLOOP.

LOOP AT itab02 INTO wa_itab02.
  IF wa_itab02-surname = 'JONES'.
     wa_itab02-surname = 'SMITH'.
     MODIFY itab01 FROM wa_itab02.
  ENDIF.
ENDLOOP.

DESCRIBE TABLE itab01 LINES line_cnt.

INSERT wa_itab02 INTO itab01 INDEX line_cnt.

WRITE itab01-surname.



*****************************************************
* index insert based on header line

TABLES zemployees.

*Internal Table with Header line
DATA: BEGIN OF itab01 OCCURS 0,
        employee LIKE zemployees-employee,
        surname  LIKE zemployees-surname,
        forename LIKE zemployees-forename,
        title    LIKE zemployees-title,
        dob      LIKE zemployees-dob,
        los      TYPE i VALUE 3,
      END OF itab01.

DATA line_cnt TYPE i.


SELECT * FROM zemployees.
  MOVE zemployees-employee TO itab01-employee.
  MOVE zemployees-surname  TO itab01-surname.
  MOVE zemployees-forename TO itab01-forename.
  MOVE zemployees-dob      TO itab01-dob.

  APPEND itab01.
ENDSELECT.

DESCRIBE TABLE itab01 LINES line_cnt.

INSERT itab01 INDEX line_cnt.