ABAP Select data from SAP table PAYP 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 PAYP 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 PAYP. 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 PAYP 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_PAYP TYPE STANDARD TABLE OF PAYP,
      WA_PAYP TYPE PAYP,
      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: <PAYP> TYPE PAYP.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PAYP
*  INTO TABLE @DATA(IT_PAYP2).
*--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_PAYP INDEX 1 INTO DATA(WA_PAYP2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PAYP ASSIGNING <PAYP>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PAYP>-MANDT = 1.
<PAYP>-LAUFD = 1.
<PAYP>-LAUFI = 1.
<PAYP>-ORIGN = 1.
<PAYP>-XVORL = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PAYP-PAYNO, sy-vline,
WA_PAYP-SUBNO, sy-vline,
WA_PAYP-DOC2T, sy-vline,
WA_PAYP-DOC2R, sy-vline,
WA_PAYP-GPA2T, sy-vline,
WA_PAYP-GPA2R, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PAYP 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_PAYP 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_PAYP INTO WA_PAYP. *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 ADRNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_PAYP-ADRNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PAYP-ADRNR.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ALPHA, internal->external for field VTRE2 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_PAYP-VTRE2 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PAYP-VTRE2.
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_PAYP_STR,
MANDT TYPE STRING,
LAUFD TYPE STRING,
LAUFI TYPE STRING,
ORIGN TYPE STRING,
XVORL TYPE STRING,
PAYNO TYPE STRING,
SUBNO TYPE STRING,
DOC2T TYPE STRING,
DOC2R TYPE STRING,
GPA2T TYPE STRING,
GPA2R TYPE STRING,
ACC1T TYPE STRING,
ACC1R TYPE STRING,
VOR1T TYPE STRING,
VOR1R TYPE STRING,
VOR2T TYPE STRING,
VOR2R TYPE STRING,
TXT1T TYPE STRING,
TXT1R TYPE STRING,
SUBAP TYPE STRING,
ADRNR TYPE STRING,
NAME1 TYPE STRING,
LAND1 TYPE STRING,
REGIO TYPE STRING,
ORT01 TYPE STRING,
BLDAT TYPE STRING,
BUDAT TYPE STRING,
FAEDT TYPE STRING,
SKTPA TYPE STRING,
WAERS TYPE STRING,
BETRW TYPE STRING,
AUGBH TYPE STRING,
AUGBW TYPE STRING,
ASKTH TYPE STRING,
ASKTW TYPE STRING,
STTAX TYPE STRING,
POKEN TYPE STRING,
ZLSPR TYPE STRING,
CLRLO TYPE STRING,
BUKRS TYPE STRING,
GSBER TYPE STRING,
C4EYE TYPE STRING,
OPTXT TYPE STRING,
REF1T TYPE STRING,
REF1R TYPE STRING,
REF2T TYPE STRING,
REF2R TYPE STRING,
REF3T TYPE STRING,
REF3R TYPE STRING,
VTREF TYPE STRING,
VTRE2 TYPE STRING,
LZBKZ TYPE STRING,
LANDL TYPE STRING,
STCD1 TYPE STRING,
AQSBW TYPE STRING,
AQSBH TYPE STRING,
QSSKZ TYPE STRING,
STAKZ TYPE STRING,
VKBEZ TYPE STRING,
EXVKO TYPE STRING,
STXTACT TYPE STRING,
OPBUK TYPE STRING,
BLART TYPE STRING,
SPART TYPE STRING,
VTEXT TYPE STRING,END OF T_EKKO_STR. DATA: WA_PAYP_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_PAYP_STR-MANDT sy-vline
WA_PAYP_STR-LAUFD sy-vline
WA_PAYP_STR-LAUFI sy-vline
WA_PAYP_STR-ORIGN sy-vline
WA_PAYP_STR-XVORL sy-vline
WA_PAYP_STR-PAYNO sy-vline
WA_PAYP_STR-SUBNO sy-vline
WA_PAYP_STR-DOC2T sy-vline
WA_PAYP_STR-DOC2R sy-vline
WA_PAYP_STR-GPA2T sy-vline
WA_PAYP_STR-GPA2R sy-vline
WA_PAYP_STR-ACC1T sy-vline
WA_PAYP_STR-ACC1R sy-vline
WA_PAYP_STR-VOR1T sy-vline
WA_PAYP_STR-VOR1R sy-vline
WA_PAYP_STR-VOR2T sy-vline
WA_PAYP_STR-VOR2R sy-vline
WA_PAYP_STR-TXT1T sy-vline
WA_PAYP_STR-TXT1R sy-vline
WA_PAYP_STR-SUBAP sy-vline
WA_PAYP_STR-ADRNR sy-vline
WA_PAYP_STR-NAME1 sy-vline
WA_PAYP_STR-LAND1 sy-vline
WA_PAYP_STR-REGIO sy-vline
WA_PAYP_STR-ORT01 sy-vline
WA_PAYP_STR-BLDAT sy-vline
WA_PAYP_STR-BUDAT sy-vline
WA_PAYP_STR-FAEDT sy-vline
WA_PAYP_STR-SKTPA sy-vline
WA_PAYP_STR-WAERS sy-vline
WA_PAYP_STR-BETRW sy-vline
WA_PAYP_STR-AUGBH sy-vline
WA_PAYP_STR-AUGBW sy-vline
WA_PAYP_STR-ASKTH sy-vline
WA_PAYP_STR-ASKTW sy-vline
WA_PAYP_STR-STTAX sy-vline
WA_PAYP_STR-POKEN sy-vline
WA_PAYP_STR-ZLSPR sy-vline
WA_PAYP_STR-CLRLO sy-vline
WA_PAYP_STR-BUKRS sy-vline
WA_PAYP_STR-GSBER sy-vline
WA_PAYP_STR-C4EYE sy-vline
WA_PAYP_STR-OPTXT sy-vline
WA_PAYP_STR-REF1T sy-vline
WA_PAYP_STR-REF1R sy-vline
WA_PAYP_STR-REF2T sy-vline
WA_PAYP_STR-REF2R sy-vline
WA_PAYP_STR-REF3T sy-vline
WA_PAYP_STR-REF3R sy-vline
WA_PAYP_STR-VTREF sy-vline
WA_PAYP_STR-VTRE2 sy-vline
WA_PAYP_STR-LZBKZ sy-vline
WA_PAYP_STR-LANDL sy-vline
WA_PAYP_STR-STCD1 sy-vline
WA_PAYP_STR-AQSBW sy-vline
WA_PAYP_STR-AQSBH sy-vline
WA_PAYP_STR-QSSKZ sy-vline
WA_PAYP_STR-STAKZ sy-vline
WA_PAYP_STR-VKBEZ sy-vline
WA_PAYP_STR-EXVKO sy-vline
WA_PAYP_STR-STXTACT sy-vline
WA_PAYP_STR-OPBUK sy-vline
WA_PAYP_STR-BLART sy-vline
WA_PAYP_STR-SPART sy-vline
WA_PAYP_STR-VTEXT sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.