ABAP Select data from SAP table MLSTR into internal table

Get Example source ABAP code based on a different SAP table
  

Below is a number of ABAP code snippets to demonstrate how to select data from SAP MLSTR table and store it within an internal table, including using the newer @DATA inline declaration methods. It also shows you various ways to process this data using ABAP work area, inline declaration or field symbols including executing all the relevant CONVERSION_EXIT routines specific to MLSTR. See here for more generic Select statement tips.

Sometimes data within SAP is stored within the database table in a different format to what it is displayed to the user. These input/output conversation FM routines are what translates the data between the two formats.

There is also a full declaration of the MLSTR table where each field has a char/string type for you to simply copy and paste. This allows you to use processing that is only available to these field types such as the CONCATENATE statement.

DATA: IT_MLSTR TYPE STANDARD TABLE OF MLSTR,
      WA_MLSTR TYPE MLSTR,
      GD_STR TYPE STRING.

DATA: lo_typedescr type REF TO cl_abap_typedescr.
DATA: lv_fieldname type fieldname.

FIELD-SYMBOLS: <FIELD> TYPE any.
FIELD-SYMBOLS: <MLSTR> TYPE MLSTR.

*Process all fields in table header/work area as string values
  PERFORM process_as_string_field_values CHANGING wa_MLSTR.

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM MLSTR
  INTO TABLE IT_MLSTR.

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM MLSTR
*  INTO TABLE @DATA(IT_MLSTR2).
*--Further methods of using ABAP code to  select data from SAP database tables

*You can also declare the header/work area using the in-line DATA declaration method
READ TABLE IT_MLSTR INDEX 1 INTO DATA(WA_MLSTR2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_MLSTR ASSIGNING <MLSTR>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<MLSTR>-ARNUMMER = 1.
<MLSTR>-VSGRUPPE = 1.
<MLSTR>-VSNUMMER = 1.
<MLSTR>-PRCTR = 1.
<MLSTR>-PRNAM = 1.
ENDLOOP.

LOOP AT IT_MLSTR INTO WA_MLSTR.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_MLSTR-PRPOS, sy-vline,
WA_MLSTR-PSPID, sy-vline,
WA_MLSTR-POSID, sy-vline,
WA_MLSTR-KDAUF, sy-vline,
WA_MLSTR-KDPOS, sy-vline,
WA_MLSTR-AUFNR, sy-vline.
ENDLOOP. *Add any further fields from structure WA_MLSTR you want to display... WRITE:/ sy-uline. * Aternatively use generic code to Write field values (and NAME) to screen report DO. ASSIGN COMPONENT sy-index OF STRUCTURE wa_MLSTR TO <field>. IF sy-subrc <> 0. EXIT. ENDIF. WRITE:/ 'Field Value', <field>, sy-vline. gd_str = <field> . lo_typedescr ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_DATA( <field> ). lv_fieldname = lo_typedescr->GET_RELATIVE_NAME( ). WRITE:/ 'Field Name', lv_fieldname. ENDDO. *Redo loop but convert all fields from internal to out value LOOP AT IT_MLSTR INTO WA_MLSTR. *Write horizonal line to screen report. WRITE:/ sy-uline. *Convert all fields to display/output versions using conversion routines PERFORM convert_all_field_values CHANGING wa_EKKO. ENDLOOP. *&---------------------------------------------------------------------* *& Form convert_all_field_values *&---------------------------------------------------------------------* FORM convert_all_field_values CHANGING p_EKKO LIKE wa_EKKO. DATA: ld_input(1000) TYPE c, ld_output(1000) TYPE C.

*Conversion exit ALPHA, internal->external for field PRCTR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MLSTR-PRCTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-PRCTR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit POSID, internal->external for field PRPOS CALL FUNCTION 'CONVERSION_EXIT_POSID_OUTPUT' EXPORTING input = WA_MLSTR-PRPOS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-PRPOS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ABPSN, internal->external for field PSPID CALL FUNCTION 'CONVERSION_EXIT_ABPSN_OUTPUT' EXPORTING input = WA_MLSTR-PSPID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-PSPID.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ABPSN, internal->external for field POSID CALL FUNCTION 'CONVERSION_EXIT_ABPSN_OUTPUT' EXPORTING input = WA_MLSTR-POSID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-POSID.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field KDAUF CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MLSTR-KDAUF IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-KDAUF.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field AUFNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MLSTR-AUFNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-AUFNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit NUMCV, internal->external for field VORNR CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_MLSTR-VORNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-VORNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field AENNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MLSTR-AENNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-AENNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field DELTE CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_MLSTR-DELTE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-DELTE.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ISOLA, internal->external for field LTXSP CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_MLSTR-LTXSP IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-LTXSP.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field VEINH CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_MLSTR-VEINH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MLSTR-VEINH.
WRITE:/ 'New Value:', ld_input.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_MLSTR_STR,
ARNUMMER TYPE STRING,
VSGRUPPE TYPE STRING,
VSNUMMER TYPE STRING,
PRCTR TYPE STRING,
PRNAM TYPE STRING,
PRPOS TYPE STRING,
PSPID TYPE STRING,
POSID TYPE STRING,
KDAUF TYPE STRING,
KDPOS TYPE STRING,
AUFNR TYPE STRING,
VORNR TYPE STRING,
MLST_ZAEHL TYPE STRING,
AUARTTXT TYPE STRING,
AENNR TYPE STRING,
MLSTN TYPE STRING,
MLSTXT TYPE STRING,
TEDAT TYPE STRING,
TETIM TYPE STRING,
TPDAT TYPE STRING,
TPTIM TYPE STRING,
EDATU TYPE STRING,
EZEIT TYPE STRING,
PDATU TYPE STRING,
PZEIT TYPE STRING,
LATED TYPE STRING,
DLTAS TYPE STRING,
DELTA TYPE STRING,
DELTE TYPE STRING,
PRCNT TYPE STRING,
KZ_TREND TYPE STRING,
KZ_LSTWR TYPE STRING,
KZ_WFLOW TYPE STRING,
KZ_FAKTP TYPE STRING,
KZ_FRMST TYPE STRING,
LST_FERTG TYPE STRING,
LST_ACTDT TYPE STRING,
LST_ACTTM TYPE STRING,
TASK_ID TYPE STRING,
RELSU TYPE STRING,
RELMS TYPE STRING,
INCPN TYPE STRING,
INCNW TYPE STRING,
INCSN TYPE STRING,
WKFLW TYPE STRING,
RELSU_TSS TYPE STRING,
RELMS_TSS TYPE STRING,
INCNW_TSS TYPE STRING,
INCPN_TSS TYPE STRING,
INCSN_TSS TYPE STRING,
WKFLW_TSS TYPE STRING,
RELSU_TUS TYPE STRING,
RELMS_TUS TYPE STRING,
INCNW_TUS TYPE STRING,
INCPN_TUS TYPE STRING,
INCSN_TUS TYPE STRING,
WKFLW_TUS TYPE STRING,
RELSU_CH TYPE STRING,
RELMS_CH TYPE STRING,
INCPN_CH TYPE STRING,
INCNW_CH TYPE STRING,
INCSN_CH TYPE STRING,
WKFLW_CH TYPE STRING,
RELSU_SG TYPE STRING,
RELMS_SG TYPE STRING,
INCPN_SG TYPE STRING,
INCNW_SG TYPE STRING,
INCSN_SG TYPE STRING,
WKFLW_SG TYPE STRING,
RELSU_TR TYPE STRING,
RELMS_TR TYPE STRING,
INCPN_TR TYPE STRING,
INCNW_TR TYPE STRING,
INCSN_TR TYPE STRING,
WKFLW_TR TYPE STRING,
KTEXT TYPE STRING,
LTXSP TYPE STRING,
FRUEHER TYPE STRING,
SPAETER TYPE STRING,
VERZUG TYPE STRING,
VORBEI TYPE STRING,
VEINH TYPE STRING,
WBSPOST1 TYPE STRING,
PROJPOST1 TYPE STRING,
NETPOST1 TYPE STRING,
EEW_MLST_PS_DUMMY TYPE STRING,END OF T_EKKO_STR. DATA: WA_MLSTR_STR type T_EKKO_STR. DATA: ld_text TYPE string. LOOP AT IT_EKKO INTO WA_EKKO. MOVE-CORRESPONDING wa_EKKO TO WA_EKKO_STR. CONCATENATE: sy-vline
WA_MLSTR_STR-ARNUMMER sy-vline
WA_MLSTR_STR-VSGRUPPE sy-vline
WA_MLSTR_STR-VSNUMMER sy-vline
WA_MLSTR_STR-PRCTR sy-vline
WA_MLSTR_STR-PRNAM sy-vline
WA_MLSTR_STR-PRPOS sy-vline
WA_MLSTR_STR-PSPID sy-vline
WA_MLSTR_STR-POSID sy-vline
WA_MLSTR_STR-KDAUF sy-vline
WA_MLSTR_STR-KDPOS sy-vline
WA_MLSTR_STR-AUFNR sy-vline
WA_MLSTR_STR-VORNR sy-vline
WA_MLSTR_STR-MLST_ZAEHL sy-vline
WA_MLSTR_STR-AUARTTXT sy-vline
WA_MLSTR_STR-AENNR sy-vline
WA_MLSTR_STR-MLSTN sy-vline
WA_MLSTR_STR-MLSTXT sy-vline
WA_MLSTR_STR-TEDAT sy-vline
WA_MLSTR_STR-TETIM sy-vline
WA_MLSTR_STR-TPDAT sy-vline
WA_MLSTR_STR-TPTIM sy-vline
WA_MLSTR_STR-EDATU sy-vline
WA_MLSTR_STR-EZEIT sy-vline
WA_MLSTR_STR-PDATU sy-vline
WA_MLSTR_STR-PZEIT sy-vline
WA_MLSTR_STR-LATED sy-vline
WA_MLSTR_STR-DLTAS sy-vline
WA_MLSTR_STR-DELTA sy-vline
WA_MLSTR_STR-DELTE sy-vline
WA_MLSTR_STR-PRCNT sy-vline
WA_MLSTR_STR-KZ_TREND sy-vline
WA_MLSTR_STR-KZ_LSTWR sy-vline
WA_MLSTR_STR-KZ_WFLOW sy-vline
WA_MLSTR_STR-KZ_FAKTP sy-vline
WA_MLSTR_STR-KZ_FRMST sy-vline
WA_MLSTR_STR-LST_FERTG sy-vline
WA_MLSTR_STR-LST_ACTDT sy-vline
WA_MLSTR_STR-LST_ACTTM sy-vline
WA_MLSTR_STR-TASK_ID sy-vline
WA_MLSTR_STR-RELSU sy-vline
WA_MLSTR_STR-RELMS sy-vline
WA_MLSTR_STR-INCPN sy-vline
WA_MLSTR_STR-INCNW sy-vline
WA_MLSTR_STR-INCSN sy-vline
WA_MLSTR_STR-WKFLW sy-vline
WA_MLSTR_STR-RELSU_TSS sy-vline
WA_MLSTR_STR-RELMS_TSS sy-vline
WA_MLSTR_STR-INCNW_TSS sy-vline
WA_MLSTR_STR-INCPN_TSS sy-vline
WA_MLSTR_STR-INCSN_TSS sy-vline
WA_MLSTR_STR-WKFLW_TSS sy-vline
WA_MLSTR_STR-RELSU_TUS sy-vline
WA_MLSTR_STR-RELMS_TUS sy-vline
WA_MLSTR_STR-INCNW_TUS sy-vline
WA_MLSTR_STR-INCPN_TUS sy-vline
WA_MLSTR_STR-INCSN_TUS sy-vline
WA_MLSTR_STR-WKFLW_TUS sy-vline
WA_MLSTR_STR-RELSU_CH sy-vline
WA_MLSTR_STR-RELMS_CH sy-vline
WA_MLSTR_STR-INCPN_CH sy-vline
WA_MLSTR_STR-INCNW_CH sy-vline
WA_MLSTR_STR-INCSN_CH sy-vline
WA_MLSTR_STR-WKFLW_CH sy-vline
WA_MLSTR_STR-RELSU_SG sy-vline
WA_MLSTR_STR-RELMS_SG sy-vline
WA_MLSTR_STR-INCPN_SG sy-vline
WA_MLSTR_STR-INCNW_SG sy-vline
WA_MLSTR_STR-INCSN_SG sy-vline
WA_MLSTR_STR-WKFLW_SG sy-vline
WA_MLSTR_STR-RELSU_TR sy-vline
WA_MLSTR_STR-RELMS_TR sy-vline
WA_MLSTR_STR-INCPN_TR sy-vline
WA_MLSTR_STR-INCNW_TR sy-vline
WA_MLSTR_STR-INCSN_TR sy-vline
WA_MLSTR_STR-WKFLW_TR sy-vline
WA_MLSTR_STR-KTEXT sy-vline
WA_MLSTR_STR-LTXSP sy-vline
WA_MLSTR_STR-FRUEHER sy-vline
WA_MLSTR_STR-SPAETER sy-vline
WA_MLSTR_STR-VERZUG sy-vline
WA_MLSTR_STR-VORBEI sy-vline
WA_MLSTR_STR-VEINH sy-vline
WA_MLSTR_STR-WBSPOST1 sy-vline
WA_MLSTR_STR-PROJPOST1 sy-vline
WA_MLSTR_STR-NETPOST1 sy-vline
WA_MLSTR_STR-EEW_MLST_PS_DUMMY sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.