ABAP Select data from SAP table AMHIS 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 AMHIS 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 AMHIS. 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 AMHIS 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_AMHIS TYPE STANDARD TABLE OF AMHIS,
      WA_AMHIS TYPE AMHIS,
      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: <AMHIS> TYPE AMHIS.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM AMHIS
*  INTO TABLE @DATA(IT_AMHIS2).
*--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_AMHIS INDEX 1 INTO DATA(WA_AMHIS2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_AMHIS ASSIGNING <AMHIS>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<AMHIS>-MANDT = 1.
<AMHIS>-WARPL = 1.
<AMHIS>-ABNUM = 1.
<AMHIS>-ZAEHL = 1.
<AMHIS>-NPLDA = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_AMHIS-TERMA, sy-vline,
WA_AMHIS-STADT, sy-vline,
WA_AMHIS-LRMDT, sy-vline,
WA_AMHIS-OFFZE, sy-vline,
WA_AMHIS-OFFZO, sy-vline,
WA_AMHIS-ABRUD, sy-vline.
ENDLOOP. *Add any further fields from structure WA_AMHIS 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_AMHIS 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_AMHIS INTO WA_AMHIS. *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 WARPL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_AMHIS-WARPL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_AMHIS-WARPL.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit CUNIT, internal->external for field ADUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_AMHIS-ADUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_AMHIS-ADUNIT.
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_AMHIS_STR,
MANDT TYPE STRING,
WARPL TYPE STRING,
ABNUM TYPE STRING,
ZAEHL TYPE STRING,
NPLDA TYPE STRING,
TERMA TYPE STRING,
STADT TYPE STRING,
LRMDT TYPE STRING,
OFFZE TYPE STRING,
OFFZO TYPE STRING,
ABRUD TYPE STRING,
ABRNA TYPE STRING,
SFAKT TYPE STRING,
TERMK TYPE STRING,
VERSCH TYPE STRING,
TOLER TYPE STRING,
KNNRM TYPE STRING,
KNPRM TYPE STRING,
NPLDO TYPE STRING,
NPLDZ TYPE STRING,
MANDA TYPE STRING,
OFFLE TYPE STRING,
OFFLO TYPE STRING,
KNAUT TYPE STRING,
KNASR TYPE STRING,
TSABR TYPE STRING,
TSVBT TYPE STRING,
TSENQ TYPE STRING,
TSENM TYPE STRING,
HORDA TYPE STRING,
TSTAT TYPE STRING,
FIXDU TYPE STRING,
FIXDA TYPE STRING,
VGNUM TYPE STRING,
ZYKZT TYPE STRING,
ZYKLS TYPE STRING,
PUFFP TYPE STRING,
PUFFN TYPE STRING,
SZAEH TYPE STRING,
RZAEH TYPE STRING,
NZAEH TYPE STRING,
USERMOD TYPE STRING,
DEMAND_ID TYPE STRING,
DEMAND_NO TYPE STRING,
REVISION_NO TYPE STRING,
REV_PLANT TYPE STRING,
ADPOINT TYPE STRING,
ADANDOR TYPE STRING,
ADTGOON TYPE STRING,
ADCYCLE TYPE STRING,
ADUNIT TYPE STRING,
ADOFFSET TYPE STRING,
ADNZAEH TYPE STRING,
ADSTTIME TYPE STRING,
ADCONFTIME TYPE STRING,
CYCLESEQIND TYPE STRING,
ADEXTSTARTDATE TYPE STRING,
ADEXTSTARTTIME TYPE STRING,
ADSYSTARTDATE TYPE STRING,
ADSYSTARTTIME TYPE STRING,
ADOBJECT TYPE STRING,
SETREPEATIND TYPE STRING,
IND_MPLA_API TYPE STRING,
DUE_DATE_TIME TYPE STRING,
DBKNZ TYPE STRING,END OF T_EKKO_STR. DATA: WA_AMHIS_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_AMHIS_STR-MANDT sy-vline
WA_AMHIS_STR-WARPL sy-vline
WA_AMHIS_STR-ABNUM sy-vline
WA_AMHIS_STR-ZAEHL sy-vline
WA_AMHIS_STR-NPLDA sy-vline
WA_AMHIS_STR-TERMA sy-vline
WA_AMHIS_STR-STADT sy-vline
WA_AMHIS_STR-LRMDT sy-vline
WA_AMHIS_STR-OFFZE sy-vline
WA_AMHIS_STR-OFFZO sy-vline
WA_AMHIS_STR-ABRUD sy-vline
WA_AMHIS_STR-ABRNA sy-vline
WA_AMHIS_STR-SFAKT sy-vline
WA_AMHIS_STR-TERMK sy-vline
WA_AMHIS_STR-VERSCH sy-vline
WA_AMHIS_STR-TOLER sy-vline
WA_AMHIS_STR-KNNRM sy-vline
WA_AMHIS_STR-KNPRM sy-vline
WA_AMHIS_STR-NPLDO sy-vline
WA_AMHIS_STR-NPLDZ sy-vline
WA_AMHIS_STR-MANDA sy-vline
WA_AMHIS_STR-OFFLE sy-vline
WA_AMHIS_STR-OFFLO sy-vline
WA_AMHIS_STR-KNAUT sy-vline
WA_AMHIS_STR-KNASR sy-vline
WA_AMHIS_STR-TSABR sy-vline
WA_AMHIS_STR-TSVBT sy-vline
WA_AMHIS_STR-TSENQ sy-vline
WA_AMHIS_STR-TSENM sy-vline
WA_AMHIS_STR-HORDA sy-vline
WA_AMHIS_STR-TSTAT sy-vline
WA_AMHIS_STR-FIXDU sy-vline
WA_AMHIS_STR-FIXDA sy-vline
WA_AMHIS_STR-VGNUM sy-vline
WA_AMHIS_STR-ZYKZT sy-vline
WA_AMHIS_STR-ZYKLS sy-vline
WA_AMHIS_STR-PUFFP sy-vline
WA_AMHIS_STR-PUFFN sy-vline
WA_AMHIS_STR-SZAEH sy-vline
WA_AMHIS_STR-RZAEH sy-vline
WA_AMHIS_STR-NZAEH sy-vline
WA_AMHIS_STR-USERMOD sy-vline
WA_AMHIS_STR-DEMAND_ID sy-vline
WA_AMHIS_STR-DEMAND_NO sy-vline
WA_AMHIS_STR-REVISION_NO sy-vline
WA_AMHIS_STR-REV_PLANT sy-vline
WA_AMHIS_STR-ADPOINT sy-vline
WA_AMHIS_STR-ADANDOR sy-vline
WA_AMHIS_STR-ADTGOON sy-vline
WA_AMHIS_STR-ADCYCLE sy-vline
WA_AMHIS_STR-ADUNIT sy-vline
WA_AMHIS_STR-ADOFFSET sy-vline
WA_AMHIS_STR-ADNZAEH sy-vline
WA_AMHIS_STR-ADSTTIME sy-vline
WA_AMHIS_STR-ADCONFTIME sy-vline
WA_AMHIS_STR-CYCLESEQIND sy-vline
WA_AMHIS_STR-ADEXTSTARTDATE sy-vline
WA_AMHIS_STR-ADEXTSTARTTIME sy-vline
WA_AMHIS_STR-ADSYSTARTDATE sy-vline
WA_AMHIS_STR-ADSYSTARTTIME sy-vline
WA_AMHIS_STR-ADOBJECT sy-vline
WA_AMHIS_STR-SETREPEATIND sy-vline
WA_AMHIS_STR-IND_MPLA_API sy-vline
WA_AMHIS_STR-DUE_DATE_TIME sy-vline
WA_AMHIS_STR-DBKNZ sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.