ABAP Select data from SAP table APOINPAR 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 APOINPAR 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 APOINPAR. 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 APOINPAR 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_APOINPAR TYPE STANDARD TABLE OF APOINPAR,
      WA_APOINPAR TYPE APOINPAR,
      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: <APOINPAR> TYPE APOINPAR.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM APOINPAR
*  INTO TABLE @DATA(IT_APOINPAR2).
*--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_APOINPAR INDEX 1 INTO DATA(WA_APOINPAR2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_APOINPAR ASSIGNING <APOINPAR>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<APOINPAR>-TRGUID = 1.
<APOINPAR>-INITIND = 1.
<APOINPAR>-UNAME = 1.
<APOINPAR>-ANWDG = 1.
<APOINPAR>-TZONE = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_APOINPAR-REQTMS, sy-vline,
WA_APOINPAR-REQQTY, sy-vline,
WA_APOINPAR-CCMOD, sy-vline,
WA_APOINPAR-DISPFLG, sy-vline,
WA_APOINPAR-AZERG, sy-vline,
WA_APOINPAR-RDMOD, sy-vline.
ENDLOOP. *Add any further fields from structure WA_APOINPAR 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_APOINPAR 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_APOINPAR INTO WA_APOINPAR. *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 LOGSYS CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_APOINPAR-LOGSYS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_APOINPAR-LOGSYS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field MEINS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_APOINPAR-MEINS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_APOINPAR-MEINS.
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_APOINPAR_STR,
TRGUID TYPE STRING,
INITIND TYPE STRING,
UNAME TYPE STRING,
ANWDG TYPE STRING,
TZONE TYPE STRING,
REQTMS TYPE STRING,
REQQTY TYPE STRING,
CCMOD TYPE STRING,
DISPFLG TYPE STRING,
AZERG TYPE STRING,
RDMOD TYPE STRING,
LOGSYS TYPE STRING,
CALID TYPE STRING,
MEINS TYPE STRING,
ENQMOD TYPE STRING,
VSTRM TYPE STRING,
TRTRM TYPE STRING,
APOINTVERS TYPE STRING,
WRKMOD TYPE STRING,
LOCFLG TYPE STRING,
ALLFLG TYPE STRING,
RETIND TYPE STRING,
MAD_MOVEBACK TYPE STRING,END OF T_EKKO_STR. DATA: WA_APOINPAR_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_APOINPAR_STR-TRGUID sy-vline
WA_APOINPAR_STR-INITIND sy-vline
WA_APOINPAR_STR-UNAME sy-vline
WA_APOINPAR_STR-ANWDG sy-vline
WA_APOINPAR_STR-TZONE sy-vline
WA_APOINPAR_STR-REQTMS sy-vline
WA_APOINPAR_STR-REQQTY sy-vline
WA_APOINPAR_STR-CCMOD sy-vline
WA_APOINPAR_STR-DISPFLG sy-vline
WA_APOINPAR_STR-AZERG sy-vline
WA_APOINPAR_STR-RDMOD sy-vline
WA_APOINPAR_STR-LOGSYS sy-vline
WA_APOINPAR_STR-CALID sy-vline
WA_APOINPAR_STR-MEINS sy-vline
WA_APOINPAR_STR-ENQMOD sy-vline
WA_APOINPAR_STR-VSTRM sy-vline
WA_APOINPAR_STR-TRTRM sy-vline
WA_APOINPAR_STR-APOINTVERS sy-vline
WA_APOINPAR_STR-WRKMOD sy-vline
WA_APOINPAR_STR-LOCFLG sy-vline
WA_APOINPAR_STR-ALLFLG sy-vline
WA_APOINPAR_STR-RETIND sy-vline
WA_APOINPAR_STR-MAD_MOVEBACK sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.