ABAP Select data from SAP table /1CN/WS_ID_000PRT_1HR1 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 /1CN/WS_ID_000PRT_1HR1 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 /1CN/WS_ID_000PRT_1HR1. 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 /1CN/WS_ID_000PRT_1HR1 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_/1CN/WS_ID_000PRT_1HR1 TYPE STANDARD TABLE OF /1CN/WS_ID_000PRT_1HR1,
      WA_/1CN/WS_ID_000PRT_1HR1 TYPE /1CN/WS_ID_000PRT_1HR1,
      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: </1CN/WS_ID_000PRT_1HR1> TYPE /1CN/WS_ID_000PRT_1HR1.

*Process all fields in table header/work area as string values
  PERFORM process_as_string_field_values CHANGING wa_/1CN/WS_ID_000PRT_1HR1.

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM /1CN/WS_ID_000PRT_1HR1
  INTO TABLE IT_/1CN/WS_ID_000PRT_1HR1.

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM /1CN/WS_ID_000PRT_1HR1
*  INTO TABLE @DATA(IT_/1CN/WS_ID_000PRT_1HR12).
*--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_/1CN/WS_ID_000PRT_1HR1 INDEX 1 INTO DATA(WA_/1CN/WS_ID_000PRT_1HR12).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_/1CN/WS_ID_000PRT_1HR1 ASSIGNING </1CN/WS_ID_000PRT_1HR1>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
</1CN/WS_ID_000PRT_1HR1>-/SAPCND/MNT_IS_ARCHIVED = 1.
</1CN/WS_ID_000PRT_1HR1>-/SAPCND/SPTB__LIN = 1.
</1CN/WS_ID_000PRT_1HR1>-/SAPCND/SPTC__LIN = 1.
</1CN/WS_ID_000PRT_1HR1>-/SAPCND/SPTDEF = 1.
</1CN/WS_ID_000PRT_1HR1>-/SAPCND/SPTDIM = 1.
ENDLOOP.

LOOP AT IT_/1CN/WS_ID_000PRT_1HR1 INTO WA_/1CN/WS_ID_000PRT_1HR1.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_/1CN/WS_ID_000PRT_1HR1-/SAPCND/SPTD__LIN, sy-vline,
WA_/1CN/WS_ID_000PRT_1HR1-/SAPCND/SPTEVL, sy-vline,
WA_/1CN/WS_ID_000PRT_1HR1-/SAPCND/SPTE__LIN, sy-vline,
WA_/1CN/WS_ID_000PRT_1HR1-/SAPCND/SPTF__LIN, sy-vline,
WA_/1CN/WS_ID_000PRT_1HR1-/SAPCND/SPTL__LIN, sy-vline,
WA_/1CN/WS_ID_000PRT_1HR1-/SAPCND/SPTM__LIN, sy-vline.
ENDLOOP. *Add any further fields from structure WA_/1CN/WS_ID_000PRT_1HR1 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_/1CN/WS_ID_000PRT_1HR1 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_/1CN/WS_ID_000PRT_1HR1 INTO WA_/1CN/WS_ID_000PRT_1HR1. *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 CUNIT, internal->external for field KMEIN CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_/1CN/WS_ID_000PRT_1HR1-KMEIN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/1CN/WS_ID_000PRT_1HR1-KMEIN.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field KONMS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_/1CN/WS_ID_000PRT_1HR1-KONMS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/1CN/WS_ID_000PRT_1HR1-KONMS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field MEINS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_/1CN/WS_ID_000PRT_1HR1-MEINS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/1CN/WS_ID_000PRT_1HR1-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_/1CN/WS_ID_000PRT_1HR1_STR,
/SAPCND/MNT_IS_ARCHIVED TYPE STRING,
/SAPCND/SPTB__LIN TYPE STRING,
/SAPCND/SPTC__LIN TYPE STRING,
/SAPCND/SPTDEF TYPE STRING,
/SAPCND/SPTDIM TYPE STRING,
/SAPCND/SPTD__LIN TYPE STRING,
/SAPCND/SPTEVL TYPE STRING,
/SAPCND/SPTE__LIN TYPE STRING,
/SAPCND/SPTF__LIN TYPE STRING,
/SAPCND/SPTL__LIN TYPE STRING,
/SAPCND/SPTM__LIN TYPE STRING,
/SAPCND/SPTN__LIN TYPE STRING,
/SAPCND/SPTO__LIN TYPE STRING,
/SAPCND/SPTP__LIN TYPE STRING,
/SAPCND/SPTR__LIN TYPE STRING,
/SAPCND/SPTS__LIN TYPE STRING,
/SAPCND/SPTTC1LIN TYPE STRING,
/SAPCND/SPTV01 TYPE STRING,
/SAPCND/SPTV02 TYPE STRING,
CLIENT TYPE STRING,
COND_GROUP_ID TYPE STRING,
CREATED_BY TYPE STRING,
CREATED_ON TYPE STRING,
DBACTION_SUPP TYPE STRING,
DBACTION_TABL TYPE STRING,
EXPNT TYPE STRING,
GKWRT TYPE STRING,
KAPPL TYPE STRING,
KBETR TYPE STRING,
KMEIN TYPE STRING,
KONMS TYPE STRING,
KONWA TYPE STRING,
KONWS TYPE STRING,
KOPOS TYPE STRING,
KOTABNR TYPE STRING,
KPEIN TYPE STRING,
KRECH TYPE STRING,
KSCHL TYPE STRING,
KSCHL_SUPP TYPE STRING,
KSTBM TYPE STRING,
KUMNE TYPE STRING,
KUMZA TYPE STRING,
KVEWE TYPE STRING,
KZBZG TYPE STRING,
KZNEP TYPE STRING,
MAINT_STATUS TYPE STRING,
MEINS TYPE STRING,
MNT_OW_ERROR_BY_COND_REC TYPE STRING,
MNT_OW_ERROR_BY_RELATION TYPE STRING,
MNT_OW_EXISTS_AT_DB TYPE STRING,
MNT_OW_MAINT_MODE_ON_SELECT TYPE STRING,
MNT_OW_RESULT TYPE STRING,
MWSK1 TYPE STRING,
MWSK2 TYPE STRING,
MXWRT TYPE STRING,
OBJECT_ID TYPE STRING,
PRODUCT TYPE STRING,
PRT_PARTNER TYPE STRING,
PTERM TYPE STRING,
RELEASE_STATUS TYPE STRING,
SCALE_DIM TYPE STRING,
STFKZ TYPE STRING,
SUPP_EXIST TYPE STRING,
TIMESTAMP_FROM TYPE STRING,
TIMESTAMP_TO TYPE STRING,
VALDT TYPE STRING,
VALTG TYPE STRING,
VARNUMH TYPE STRING,END OF T_EKKO_STR. DATA: WA_/1CN/WS_ID_000PRT_1HR1_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_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/MNT_IS_ARCHIVED sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTB__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTC__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTDEF sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTDIM sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTD__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTEVL sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTE__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTF__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTL__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTM__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTN__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTO__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTP__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTR__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTS__LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTTC1LIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTV01 sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-/SAPCND/SPTV02 sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-CLIENT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-COND_GROUP_ID sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-CREATED_BY sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-CREATED_ON sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-DBACTION_SUPP sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-DBACTION_TABL sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-EXPNT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-GKWRT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KAPPL sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KBETR sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KMEIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KONMS sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KONWA sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KONWS sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KOPOS sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KOTABNR sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KPEIN sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KRECH sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KSCHL sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KSCHL_SUPP sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KSTBM sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KUMNE sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KUMZA sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KVEWE sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KZBZG sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-KZNEP sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MAINT_STATUS sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MEINS sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MNT_OW_ERROR_BY_COND_REC sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MNT_OW_ERROR_BY_RELATION sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MNT_OW_EXISTS_AT_DB sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MNT_OW_MAINT_MODE_ON_SELECT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MNT_OW_RESULT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MWSK1 sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MWSK2 sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-MXWRT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-OBJECT_ID sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-PRODUCT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-PRT_PARTNER sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-PTERM sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-RELEASE_STATUS sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-SCALE_DIM sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-STFKZ sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-SUPP_EXIST sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-TIMESTAMP_FROM sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-TIMESTAMP_TO sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-VALDT sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-VALTG sy-vline
WA_/1CN/WS_ID_000PRT_1HR1_STR-VARNUMH sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.