ABAP Select data from SAP table PPCST 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 PPCST 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 PPCST. 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 PPCST 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_PPCST TYPE STANDARD TABLE OF PPCST,
      WA_PPCST TYPE PPCST,
      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: <PPCST> TYPE PPCST.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PPCST
*  INTO TABLE @DATA(IT_PPCST2).
*--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_PPCST INDEX 1 INTO DATA(WA_PPCST2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PPCST ASSIGNING <PPCST>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PPCST>-MANDT = 1.
<PPCST>-TITLE = 1.
<PPCST>-PLVAR = 1.
<PPCST>-VERSN = 1.
<PPCST>-BEGO = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PPCST-ENDO, sy-vline,
WA_PPCST-VNAME, sy-vline,
WA_PPCST-CURCY, sy-vline,
WA_PPCST-ABART, sy-vline,
WA_PPCST-OTITL, sy-vline,
WA_PPCST-OTEXT, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PPCST 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_PPCST 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_PPCST INTO WA_PPCST. *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 OKTNRA CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_PPCST-OKTNRA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PPCST-OKTNRA.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field KTNRA CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_PPCST-KTNRA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PPCST-KTNRA.
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_PPCST_STR,
MANDT TYPE STRING,
TITLE TYPE STRING,
PLVAR TYPE STRING,
VERSN TYPE STRING,
BEGO TYPE STRING,
ENDO TYPE STRING,
VNAME TYPE STRING,
CURCY TYPE STRING,
ABART TYPE STRING,
OTITL TYPE STRING,
OTEXT TYPE STRING,
OBUDN TYPE STRING,
OBUDO TYPE STRING,
OUNIT TYPE STRING,
OPAG TYPE STRING,
OTOT TYPE STRING,
ONUM TYPE STRING,
OREFTXT TYPE STRING,
OSHORT TYPE STRING,
ONUMBR TYPE STRING,
OBEGDA TYPE STRING,
OENDDA TYPE STRING,
ORSHORT TYPE STRING,
OOTYPE TYPE STRING,
OOBJIDC TYPE STRING,
OCTYPE TYPE STRING,
OKTNRA TYPE STRING,
ONUMC TYPE STRING,
OOFFC TYPE STRING,
OSHOW TYPE STRING,
OGRAF TYPE STRING,
OEXCL TYPE STRING,
JTITL TYPE STRING,
JTEXT TYPE STRING,
JBUDN TYPE STRING,
JBUDO TYPE STRING,
JUNIT TYPE STRING,
JPAG TYPE STRING,
JTOT TYPE STRING,
JNUM TYPE STRING,
JREFTXT TYPE STRING,
JOTYPE TYPE STRING,
JOBJID TYPE STRING,
JCTYPE TYPE STRING,
JBEGDA TYPE STRING,
JENDDA TYPE STRING,
JDIRCT TYPE STRING,
JNUMC TYPE STRING,
JOFFC TYPE STRING,
JDUNIT TYPE STRING,
JDBUDG TYPE STRING,
JATEXT TYPE STRING,
JSHOW TYPE STRING,
JGRAF TYPE STRING,
JEXCL TYPE STRING,
LTITLE TYPE STRING,
REFLG TYPE STRING,
CTYPE TYPE STRING,
KTNRA TYPE STRING,
GRPLB TYPE STRING,
BEGDA TYPE STRING,
ENDDA TYPE STRING,
VUNIT TYPE STRING,
BUDGT TYPE STRING,
ATEXT TYPE STRING,
LPAG TYPE STRING,
LTOT TYPE STRING,
LNUM TYPE STRING,
DTITLE TYPE STRING,
STITL TYPE STRING,
STEXT TYPE STRING,
PWD TYPE STRING,
PTITL TYPE STRING,
PCTYPE TYPE STRING,
PBEGDA TYPE STRING,
PPROZN TYPE STRING,
HIGH_ENDDA TYPE STRING,
SMODE TYPE STRING,
NO_COPY TYPE STRING,END OF T_EKKO_STR. DATA: WA_PPCST_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_PPCST_STR-MANDT sy-vline
WA_PPCST_STR-TITLE sy-vline
WA_PPCST_STR-PLVAR sy-vline
WA_PPCST_STR-VERSN sy-vline
WA_PPCST_STR-BEGO sy-vline
WA_PPCST_STR-ENDO sy-vline
WA_PPCST_STR-VNAME sy-vline
WA_PPCST_STR-CURCY sy-vline
WA_PPCST_STR-ABART sy-vline
WA_PPCST_STR-OTITL sy-vline
WA_PPCST_STR-OTEXT sy-vline
WA_PPCST_STR-OBUDN sy-vline
WA_PPCST_STR-OBUDO sy-vline
WA_PPCST_STR-OUNIT sy-vline
WA_PPCST_STR-OPAG sy-vline
WA_PPCST_STR-OTOT sy-vline
WA_PPCST_STR-ONUM sy-vline
WA_PPCST_STR-OREFTXT sy-vline
WA_PPCST_STR-OSHORT sy-vline
WA_PPCST_STR-ONUMBR sy-vline
WA_PPCST_STR-OBEGDA sy-vline
WA_PPCST_STR-OENDDA sy-vline
WA_PPCST_STR-ORSHORT sy-vline
WA_PPCST_STR-OOTYPE sy-vline
WA_PPCST_STR-OOBJIDC sy-vline
WA_PPCST_STR-OCTYPE sy-vline
WA_PPCST_STR-OKTNRA sy-vline
WA_PPCST_STR-ONUMC sy-vline
WA_PPCST_STR-OOFFC sy-vline
WA_PPCST_STR-OSHOW sy-vline
WA_PPCST_STR-OGRAF sy-vline
WA_PPCST_STR-OEXCL sy-vline
WA_PPCST_STR-JTITL sy-vline
WA_PPCST_STR-JTEXT sy-vline
WA_PPCST_STR-JBUDN sy-vline
WA_PPCST_STR-JBUDO sy-vline
WA_PPCST_STR-JUNIT sy-vline
WA_PPCST_STR-JPAG sy-vline
WA_PPCST_STR-JTOT sy-vline
WA_PPCST_STR-JNUM sy-vline
WA_PPCST_STR-JREFTXT sy-vline
WA_PPCST_STR-JOTYPE sy-vline
WA_PPCST_STR-JOBJID sy-vline
WA_PPCST_STR-JCTYPE sy-vline
WA_PPCST_STR-JBEGDA sy-vline
WA_PPCST_STR-JENDDA sy-vline
WA_PPCST_STR-JDIRCT sy-vline
WA_PPCST_STR-JNUMC sy-vline
WA_PPCST_STR-JOFFC sy-vline
WA_PPCST_STR-JDUNIT sy-vline
WA_PPCST_STR-JDBUDG sy-vline
WA_PPCST_STR-JATEXT sy-vline
WA_PPCST_STR-JSHOW sy-vline
WA_PPCST_STR-JGRAF sy-vline
WA_PPCST_STR-JEXCL sy-vline
WA_PPCST_STR-LTITLE sy-vline
WA_PPCST_STR-REFLG sy-vline
WA_PPCST_STR-CTYPE sy-vline
WA_PPCST_STR-KTNRA sy-vline
WA_PPCST_STR-GRPLB sy-vline
WA_PPCST_STR-BEGDA sy-vline
WA_PPCST_STR-ENDDA sy-vline
WA_PPCST_STR-VUNIT sy-vline
WA_PPCST_STR-BUDGT sy-vline
WA_PPCST_STR-ATEXT sy-vline
WA_PPCST_STR-LPAG sy-vline
WA_PPCST_STR-LTOT sy-vline
WA_PPCST_STR-LNUM sy-vline
WA_PPCST_STR-DTITLE sy-vline
WA_PPCST_STR-STITL sy-vline
WA_PPCST_STR-STEXT sy-vline
WA_PPCST_STR-PWD sy-vline
WA_PPCST_STR-PTITL sy-vline
WA_PPCST_STR-PCTYPE sy-vline
WA_PPCST_STR-PBEGDA sy-vline
WA_PPCST_STR-PPROZN sy-vline
WA_PPCST_STR-HIGH_ENDDA sy-vline
WA_PPCST_STR-SMODE sy-vline
WA_PPCST_STR-NO_COPY sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.