ABAP Select data from SAP table RJPACKPRODUCT 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 RJPACKPRODUCT 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 RJPACKPRODUCT. 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 RJPACKPRODUCT 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_RJPACKPRODUCT TYPE STANDARD TABLE OF RJPACKPRODUCT,
      WA_RJPACKPRODUCT TYPE RJPACKPRODUCT,
      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: <RJPACKPRODUCT> TYPE RJPACKPRODUCT.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM RJPACKPRODUCT
*  INTO TABLE @DATA(IT_RJPACKPRODUCT2).
*--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_RJPACKPRODUCT INDEX 1 INTO DATA(WA_RJPACKPRODUCT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_RJPACKPRODUCT ASSIGNING <RJPACKPRODUCT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<RJPACKPRODUCT>-CARRIER = 1.
<RJPACKPRODUCT>-PGID = 1.
<RJPACKPRODUCT>-JOINGRP = 1.
<RJPACKPRODUCT>-MATNR = 1.
<RJPACKPRODUCT>-MAKTX = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_RJPACKPRODUCT-CHARG, sy-vline,
WA_RJPACKPRODUCT-XCHAR, sy-vline,
WA_RJPACKPRODUCT-XCHPF, sy-vline,
WA_RJPACKPRODUCT-BESTQ, sy-vline,
WA_RJPACKPRODUCT-QPLOS, sy-vline,
WA_RJPACKPRODUCT-SOBKZ, sy-vline.
ENDLOOP. *Add any further fields from structure WA_RJPACKPRODUCT 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_RJPACKPRODUCT 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_RJPACKPRODUCT INTO WA_RJPACKPRODUCT. *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 MATN1, internal->external for field MATNR CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_RJPACKPRODUCT-MATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_RJPACKPRODUCT-MATNR.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit NUMCV, internal->external for field REVLEV CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_RJPACKPRODUCT-REVLEV IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_RJPACKPRODUCT-REVLEV.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATN1, internal->external for field RMATP CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_RJPACKPRODUCT-RMATP IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_RJPACKPRODUCT-RMATP.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit MATN1, internal->external for field LOADCARR_REQ CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_RJPACKPRODUCT-LOADCARR_REQ IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_RJPACKPRODUCT-LOADCARR_REQ.
WRITE:/ 'New Value:', ld_input.

*Conversion exit VHUPI, internal->external for field PACKNR_REQ CALL FUNCTION 'CONVERSION_EXIT_VHUPI_OUTPUT' EXPORTING input = WA_RJPACKPRODUCT-PACKNR_REQ IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_RJPACKPRODUCT-PACKNR_REQ.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit CUNIT, internal->external for field QUNIT_B CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_RJPACKPRODUCT-QUNIT_B IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_RJPACKPRODUCT-QUNIT_B.
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_RJPACKPRODUCT_STR,
CARRIER TYPE STRING,
PGID TYPE STRING,
JOINGRP TYPE STRING,
MATNR TYPE STRING,
MAKTX TYPE STRING,
CHARG TYPE STRING,
XCHAR TYPE STRING,
XCHPF TYPE STRING,
BESTQ TYPE STRING,
QPLOS TYPE STRING,
SOBKZ TYPE STRING,
SONUM TYPE STRING,
REVLEV TYPE STRING,
VHART TYPE STRING,
MAGRV TYPE STRING,
RMATP TYPE STRING,
VFDAT TYPE STRING,
WERKS TYPE STRING,
LGORT TYPE STRING,
CLASS TYPE STRING,
KUNWE TYPE STRING,
ABLAD TYPE STRING,
LIFNR TYPE STRING,
ROUTE TYPE STRING,
KUNAG TYPE STRING,
VERSART TYPE STRING,
ABRVW TYPE STRING,
VSTEL TYPE STRING,
LGNUM TYPE STRING,
LGTYP TYPE STRING,
LOADCARR_REQ TYPE STRING,
VALDAT_REQ TYPE STRING,
PACKNR_REQ TYPE STRING,
VKORG TYPE STRING,
VTWEG TYPE STRING,
LGORT_VON TYPE STRING,
KUNNR TYPE STRING,
SPART TYPE STRING,
MAXQUA TYPE STRING,
PNQUA TYPE STRING,
ACQUA TYPE STRING,
OPQUA TYPE STRING,
QUNIT_A TYPE STRING,
QUNIT_B TYPE STRING,
DOCTYPE TYPE STRING,
DOCNUM TYPE STRING,
DOCITEM TYPE STRING,
RESOBJTYPE TYPE STRING,
RESOBJ TYPE STRING,
RESOBJITEM TYPE STRING,
SPLIT TYPE STRING,
LFART TYPE STRING,
PACKING_TAG TYPE STRING,
PACKGOODS TYPE STRING,
HEADER TYPE STRING,
ITEMS TYPE STRING,END OF T_EKKO_STR. DATA: WA_RJPACKPRODUCT_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_RJPACKPRODUCT_STR-CARRIER sy-vline
WA_RJPACKPRODUCT_STR-PGID sy-vline
WA_RJPACKPRODUCT_STR-JOINGRP sy-vline
WA_RJPACKPRODUCT_STR-MATNR sy-vline
WA_RJPACKPRODUCT_STR-MAKTX sy-vline
WA_RJPACKPRODUCT_STR-CHARG sy-vline
WA_RJPACKPRODUCT_STR-XCHAR sy-vline
WA_RJPACKPRODUCT_STR-XCHPF sy-vline
WA_RJPACKPRODUCT_STR-BESTQ sy-vline
WA_RJPACKPRODUCT_STR-QPLOS sy-vline
WA_RJPACKPRODUCT_STR-SOBKZ sy-vline
WA_RJPACKPRODUCT_STR-SONUM sy-vline
WA_RJPACKPRODUCT_STR-REVLEV sy-vline
WA_RJPACKPRODUCT_STR-VHART sy-vline
WA_RJPACKPRODUCT_STR-MAGRV sy-vline
WA_RJPACKPRODUCT_STR-RMATP sy-vline
WA_RJPACKPRODUCT_STR-VFDAT sy-vline
WA_RJPACKPRODUCT_STR-WERKS sy-vline
WA_RJPACKPRODUCT_STR-LGORT sy-vline
WA_RJPACKPRODUCT_STR-CLASS sy-vline
WA_RJPACKPRODUCT_STR-KUNWE sy-vline
WA_RJPACKPRODUCT_STR-ABLAD sy-vline
WA_RJPACKPRODUCT_STR-LIFNR sy-vline
WA_RJPACKPRODUCT_STR-ROUTE sy-vline
WA_RJPACKPRODUCT_STR-KUNAG sy-vline
WA_RJPACKPRODUCT_STR-VERSART sy-vline
WA_RJPACKPRODUCT_STR-ABRVW sy-vline
WA_RJPACKPRODUCT_STR-VSTEL sy-vline
WA_RJPACKPRODUCT_STR-LGNUM sy-vline
WA_RJPACKPRODUCT_STR-LGTYP sy-vline
WA_RJPACKPRODUCT_STR-LOADCARR_REQ sy-vline
WA_RJPACKPRODUCT_STR-VALDAT_REQ sy-vline
WA_RJPACKPRODUCT_STR-PACKNR_REQ sy-vline
WA_RJPACKPRODUCT_STR-VKORG sy-vline
WA_RJPACKPRODUCT_STR-VTWEG sy-vline
WA_RJPACKPRODUCT_STR-LGORT_VON sy-vline
WA_RJPACKPRODUCT_STR-KUNNR sy-vline
WA_RJPACKPRODUCT_STR-SPART sy-vline
WA_RJPACKPRODUCT_STR-MAXQUA sy-vline
WA_RJPACKPRODUCT_STR-PNQUA sy-vline
WA_RJPACKPRODUCT_STR-ACQUA sy-vline
WA_RJPACKPRODUCT_STR-OPQUA sy-vline
WA_RJPACKPRODUCT_STR-QUNIT_A sy-vline
WA_RJPACKPRODUCT_STR-QUNIT_B sy-vline
WA_RJPACKPRODUCT_STR-DOCTYPE sy-vline
WA_RJPACKPRODUCT_STR-DOCNUM sy-vline
WA_RJPACKPRODUCT_STR-DOCITEM sy-vline
WA_RJPACKPRODUCT_STR-RESOBJTYPE sy-vline
WA_RJPACKPRODUCT_STR-RESOBJ sy-vline
WA_RJPACKPRODUCT_STR-RESOBJITEM sy-vline
WA_RJPACKPRODUCT_STR-SPLIT sy-vline
WA_RJPACKPRODUCT_STR-LFART sy-vline
WA_RJPACKPRODUCT_STR-PACKING_TAG sy-vline
WA_RJPACKPRODUCT_STR-PACKGOODS sy-vline
WA_RJPACKPRODUCT_STR-HEADER sy-vline
WA_RJPACKPRODUCT_STR-ITEMS sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.