ABAP Select data from SAP table P15M1 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 P15M1 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 P15M1. 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 P15M1 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_P15M1 TYPE STANDARD TABLE OF P15M1,
      WA_P15M1 TYPE P15M1,
      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: <P15M1> TYPE P15M1.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM P15M1
*  INTO TABLE @DATA(IT_P15M12).
*--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_P15M1 INDEX 1 INTO DATA(WA_P15M12).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_P15M1 ASSIGNING <P15M1>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<P15M1>-SOCIE = 1.
<P15M1>-CFSOC = 1.
<P15M1>-COGNO = 1.
<P15M1>-NOMED = 1.
<P15M1>-CFCID = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_P15M1-SESSO, sy-vline,
WA_P15M1-DATAN, sy-vline,
WA_P15M1-COMUN, sy-vline,
WA_P15M1-PROVN, sy-vline,
WA_P15M1-CMP1B, sy-vline,
WA_P15M1-CMP01, sy-vline.
ENDLOOP. *Add any further fields from structure WA_P15M1 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_P15M1 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_P15M1 INTO WA_P15M1. *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.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_P15M1_STR,
SOCIE TYPE STRING,
CFSOC TYPE STRING,
COGNO TYPE STRING,
NOMED TYPE STRING,
CFCID TYPE STRING,
SESSO TYPE STRING,
DATAN TYPE STRING,
COMUN TYPE STRING,
PROVN TYPE STRING,
CMP1B TYPE STRING,
CMP01 TYPE STRING,
CMP02 TYPE STRING,
CMP03 TYPE STRING,
CMP04 TYPE STRING,
CMP05 TYPE STRING,
CMP06 TYPE STRING,
CMP7B TYPE STRING,
CMP07 TYPE STRING,
CMP8B TYPE STRING,
CMP08 TYPE STRING,
CMP09 TYPE STRING,
CMP10 TYPE STRING,
CMP11 TYPE STRING,
CMP12 TYPE STRING,
CM12B TYPE STRING,
CMP13 TYPE STRING,
CMP14 TYPE STRING,
CMP15 TYPE STRING,
CMP16 TYPE STRING,
CMP17 TYPE STRING,
CMP18 TYPE STRING,
CMP19 TYPE STRING,
CMP20 TYPE STRING,
CMP21 TYPE STRING,
CMP22 TYPE STRING,
CMP23 TYPE STRING,
CMP24 TYPE STRING,
CM25A TYPE STRING,
CM25B TYPE STRING,
CM25C TYPE STRING,
CM25D TYPE STRING,
CMP26 TYPE STRING,
CMP27 TYPE STRING,
CMP28 TYPE STRING,
CMP29 TYPE STRING,
CMP30 TYPE STRING,
CMP31 TYPE STRING,
CMP32 TYPE STRING,
CMP33 TYPE STRING,
CMP34 TYPE STRING,
CDATA TYPE STRING,
NOTE1 TYPE STRING,
NOTE2 TYPE STRING,
NOTE3 TYPE STRING,
NOTE4 TYPE STRING,
NOTE5 TYPE STRING,END OF T_EKKO_STR. DATA: WA_P15M1_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_P15M1_STR-SOCIE sy-vline
WA_P15M1_STR-CFSOC sy-vline
WA_P15M1_STR-COGNO sy-vline
WA_P15M1_STR-NOMED sy-vline
WA_P15M1_STR-CFCID sy-vline
WA_P15M1_STR-SESSO sy-vline
WA_P15M1_STR-DATAN sy-vline
WA_P15M1_STR-COMUN sy-vline
WA_P15M1_STR-PROVN sy-vline
WA_P15M1_STR-CMP1B sy-vline
WA_P15M1_STR-CMP01 sy-vline
WA_P15M1_STR-CMP02 sy-vline
WA_P15M1_STR-CMP03 sy-vline
WA_P15M1_STR-CMP04 sy-vline
WA_P15M1_STR-CMP05 sy-vline
WA_P15M1_STR-CMP06 sy-vline
WA_P15M1_STR-CMP7B sy-vline
WA_P15M1_STR-CMP07 sy-vline
WA_P15M1_STR-CMP8B sy-vline
WA_P15M1_STR-CMP08 sy-vline
WA_P15M1_STR-CMP09 sy-vline
WA_P15M1_STR-CMP10 sy-vline
WA_P15M1_STR-CMP11 sy-vline
WA_P15M1_STR-CMP12 sy-vline
WA_P15M1_STR-CM12B sy-vline
WA_P15M1_STR-CMP13 sy-vline
WA_P15M1_STR-CMP14 sy-vline
WA_P15M1_STR-CMP15 sy-vline
WA_P15M1_STR-CMP16 sy-vline
WA_P15M1_STR-CMP17 sy-vline
WA_P15M1_STR-CMP18 sy-vline
WA_P15M1_STR-CMP19 sy-vline
WA_P15M1_STR-CMP20 sy-vline
WA_P15M1_STR-CMP21 sy-vline
WA_P15M1_STR-CMP22 sy-vline
WA_P15M1_STR-CMP23 sy-vline
WA_P15M1_STR-CMP24 sy-vline
WA_P15M1_STR-CM25A sy-vline
WA_P15M1_STR-CM25B sy-vline
WA_P15M1_STR-CM25C sy-vline
WA_P15M1_STR-CM25D sy-vline
WA_P15M1_STR-CMP26 sy-vline
WA_P15M1_STR-CMP27 sy-vline
WA_P15M1_STR-CMP28 sy-vline
WA_P15M1_STR-CMP29 sy-vline
WA_P15M1_STR-CMP30 sy-vline
WA_P15M1_STR-CMP31 sy-vline
WA_P15M1_STR-CMP32 sy-vline
WA_P15M1_STR-CMP33 sy-vline
WA_P15M1_STR-CMP34 sy-vline
WA_P15M1_STR-CDATA sy-vline
WA_P15M1_STR-NOTE1 sy-vline
WA_P15M1_STR-NOTE2 sy-vline
WA_P15M1_STR-NOTE3 sy-vline
WA_P15M1_STR-NOTE4 sy-vline
WA_P15M1_STR-NOTE5 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.