ABAP Select data from SAP table PRDSCMMATKEY 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 PRDSCMMATKEY 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 PRDSCMMATKEY. 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 PRDSCMMATKEY 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_PRDSCMMATKEY TYPE STANDARD TABLE OF PRDSCMMATKEY,
      WA_PRDSCMMATKEY TYPE PRDSCMMATKEY,
      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: <PRDSCMMATKEY> TYPE PRDSCMMATKEY.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PRDSCMMATKEY
*  INTO TABLE @DATA(IT_PRDSCMMATKEY2).
*--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_PRDSCMMATKEY INDEX 1 INTO DATA(WA_PRDSCMMATKEY2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PRDSCMMATKEY ASSIGNING <PRDSCMMATKEY>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PRDSCMMATKEY>-MANDT = 1.
<PRDSCMMATKEY>-MATID = 1.
<PRDSCMMATKEY>-MATNR = 1.
<PRDSCMMATKEY>-PRODUCT_TYPE = 1.
<PRDSCMMATKEY>-LVORM = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PRDSCMMATKEY-FLAGCLASS, sy-vline,
WA_PRDSCMMATKEY-FLAGPACK, sy-vline,
WA_PRDSCMMATKEY-DISST, sy-vline,
WA_PRDSCMMATKEY-MEINS, sy-vline,
WA_PRDSCMMATKEY-ATTYP, sy-vline,
WA_PRDSCMMATKEY-LOGUNIT, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PRDSCMMATKEY 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_PRDSCMMATKEY 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_PRDSCMMATKEY INTO WA_PRDSCMMATKEY. *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_PRDSCMMATKEY-MATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-MATNR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

*Conversion exit TSTRG, internal->external for field SHELF_LIFE_DUR CALL FUNCTION 'CONVERSION_EXIT_TSTRG_OUTPUT' EXPORTING input = WA_PRDSCMMATKEY-SHELF_LIFE_DUR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-SHELF_LIFE_DUR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit TSTRG, internal->external for field MATURITY_DUR CALL FUNCTION 'CONVERSION_EXIT_TSTRG_OUTPUT' EXPORTING input = WA_PRDSCMMATKEY-MATURITY_DUR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-MATURITY_DUR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit TSTRG, internal->external for field SHLF_LFE_REQ_MIN CALL FUNCTION 'CONVERSION_EXIT_TSTRG_OUTPUT' EXPORTING input = WA_PRDSCMMATKEY-SHLF_LFE_REQ_MIN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-SHLF_LFE_REQ_MIN.
WRITE:/ 'New Value:', ld_input.

*Conversion exit TSTRG, internal->external for field SHLF_LFE_REQ_MAX CALL FUNCTION 'CONVERSION_EXIT_TSTRG_OUTPUT' EXPORTING input = WA_PRDSCMMATKEY-SHLF_LFE_REQ_MAX IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-SHLF_LFE_REQ_MAX.
WRITE:/ 'New Value:', ld_input.

*Conversion exit PERKZ, internal->external for field IPRKZ CALL FUNCTION 'CONVERSION_EXIT_PERKZ_OUTPUT' EXPORTING input = WA_PRDSCMMATKEY-IPRKZ IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-IPRKZ.
WRITE:/ 'New Value:', ld_input.

*Conversion exit TSTLC, internal->external for field DATA_CHECK_AT CALL FUNCTION 'CONVERSION_EXIT_TSTLC_OUTPUT' EXPORTING input = WA_PRDSCMMATKEY-DATA_CHECK_AT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-DATA_CHECK_AT.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit TSTLC, internal->external for field CREATEUTC CALL FUNCTION 'CONVERSION_EXIT_TSTLC_OUTPUT' EXPORTING input = WA_PRDSCMMATKEY-CREATEUTC IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PRDSCMMATKEY-CREATEUTC.
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_PRDSCMMATKEY_STR,
MANDT TYPE STRING,
MATID TYPE STRING,
MATNR TYPE STRING,
PRODUCT_TYPE TYPE STRING,
LVORM TYPE STRING,
FLAGCLASS TYPE STRING,
FLAGPACK TYPE STRING,
DISST TYPE STRING,
MEINS TYPE STRING,
ATTYP TYPE STRING,
LOGUNIT TYPE STRING,
CWREL TYPE STRING,
TY2TQ TYPE STRING,
BRGEW TYPE STRING,
NTGEW TYPE STRING,
GEWEI TYPE STRING,
VOLUM TYPE STRING,
VOLEH TYPE STRING,
SHELF_LIFE_FLAG TYPE STRING,
SHELF_LIFE_DUR TYPE STRING,
MATURITY_DUR TYPE STRING,
SHLF_LFE_REQ_MIN TYPE STRING,
SHLF_LFE_REQ_MAX TYPE STRING,
MHDLP TYPE STRING,
IPRKZ TYPE STRING,
SLED_BBD TYPE STRING,
SLDRND TYPE STRING,
ATT01 TYPE STRING,
ATT02 TYPE STRING,
ATT03 TYPE STRING,
ATT04 TYPE STRING,
ATT05 TYPE STRING,
BOD_DEPLVL TYPE STRING,
EXCL_EXPEDITE TYPE STRING,
PROD_PAOOPT TYPE STRING,
RESTRICT_INVBAL TYPE STRING,
DRP_GL_STOCK TYPE STRING,
MATKL TYPE STRING,
COMPL TYPE STRING,
STFAC TYPE STRING,
TRAGR TYPE STRING,
TS_SDP_USE TYPE STRING,
PRDHA TYPE STRING,
GTIN_IND TYPE STRING,
DISPLAY TYPE STRING,
HERKL TYPE STRING,
BATCH_REQ TYPE STRING,
KITCOMP TYPE STRING,
PRDSET_IND TYPE STRING,
SERIAL TYPE STRING,
DATA_CHECK_BY TYPE STRING,
DATA_CHECK_AT TYPE STRING,
KOSCH TYPE STRING,
KOSCH_GRP TYPE STRING,
VMIKOSCH TYPE STRING,
VMIKOSCH_GRP TYPE STRING,
PACKGR TYPE STRING,
RMATP TYPE STRING,
RMATID_PB TYPE STRING,
HUTYP_DFLT TYPE STRING,
CUOBJ TYPE STRING,
SATID TYPE STRING,
KZKFG TYPE STRING,
MATVERS_HEAD TYPE STRING,
MATVERS_TYPE TYPE STRING,
MATVERS_CHNR TYPE STRING,
TEMPRPBLOCK TYPE STRING,
TEMPRPFROM TYPE STRING,
TEMPRPTO TYPE STRING,
DUMMY_PRD_INCL_EEW_PS TYPE STRING,
LIKEID TYPE STRING,
PHIN_FROM TYPE STRING,
PHIN_TO TYPE STRING,
PHIN_TSID TYPE STRING,
PHOUT_FROM TYPE STRING,
PHOUT_TO TYPE STRING,
PHOUT_TSID TYPE STRING,
LIFE_CYCLE_LOCID TYPE STRING,
CREATEUSER TYPE STRING,
CHANGEUSER TYPE STRING,
PROD_SHAPE TYPE STRING,
MO_PROFILE_ID TYPE STRING,
OVERHANG_TRESH TYPE STRING,
BRIDGE_TRESH TYPE STRING,
BRIDGE_MAX_SLOPE TYPE STRING,
HEIGHT_NONFLAT TYPE STRING,
HEIGHT_NONFLAT_UOM TYPE STRING,
CREATEUTC TYPE STRING,
CHANGEUTC TYPE STRING,END OF T_EKKO_STR. DATA: WA_PRDSCMMATKEY_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_PRDSCMMATKEY_STR-MANDT sy-vline
WA_PRDSCMMATKEY_STR-MATID sy-vline
WA_PRDSCMMATKEY_STR-MATNR sy-vline
WA_PRDSCMMATKEY_STR-PRODUCT_TYPE sy-vline
WA_PRDSCMMATKEY_STR-LVORM sy-vline
WA_PRDSCMMATKEY_STR-FLAGCLASS sy-vline
WA_PRDSCMMATKEY_STR-FLAGPACK sy-vline
WA_PRDSCMMATKEY_STR-DISST sy-vline
WA_PRDSCMMATKEY_STR-MEINS sy-vline
WA_PRDSCMMATKEY_STR-ATTYP sy-vline
WA_PRDSCMMATKEY_STR-LOGUNIT sy-vline
WA_PRDSCMMATKEY_STR-CWREL sy-vline
WA_PRDSCMMATKEY_STR-TY2TQ sy-vline
WA_PRDSCMMATKEY_STR-BRGEW sy-vline
WA_PRDSCMMATKEY_STR-NTGEW sy-vline
WA_PRDSCMMATKEY_STR-GEWEI sy-vline
WA_PRDSCMMATKEY_STR-VOLUM sy-vline
WA_PRDSCMMATKEY_STR-VOLEH sy-vline
WA_PRDSCMMATKEY_STR-SHELF_LIFE_FLAG sy-vline
WA_PRDSCMMATKEY_STR-SHELF_LIFE_DUR sy-vline
WA_PRDSCMMATKEY_STR-MATURITY_DUR sy-vline
WA_PRDSCMMATKEY_STR-SHLF_LFE_REQ_MIN sy-vline
WA_PRDSCMMATKEY_STR-SHLF_LFE_REQ_MAX sy-vline
WA_PRDSCMMATKEY_STR-MHDLP sy-vline
WA_PRDSCMMATKEY_STR-IPRKZ sy-vline
WA_PRDSCMMATKEY_STR-SLED_BBD sy-vline
WA_PRDSCMMATKEY_STR-SLDRND sy-vline
WA_PRDSCMMATKEY_STR-ATT01 sy-vline
WA_PRDSCMMATKEY_STR-ATT02 sy-vline
WA_PRDSCMMATKEY_STR-ATT03 sy-vline
WA_PRDSCMMATKEY_STR-ATT04 sy-vline
WA_PRDSCMMATKEY_STR-ATT05 sy-vline
WA_PRDSCMMATKEY_STR-BOD_DEPLVL sy-vline
WA_PRDSCMMATKEY_STR-EXCL_EXPEDITE sy-vline
WA_PRDSCMMATKEY_STR-PROD_PAOOPT sy-vline
WA_PRDSCMMATKEY_STR-RESTRICT_INVBAL sy-vline
WA_PRDSCMMATKEY_STR-DRP_GL_STOCK sy-vline
WA_PRDSCMMATKEY_STR-MATKL sy-vline
WA_PRDSCMMATKEY_STR-COMPL sy-vline
WA_PRDSCMMATKEY_STR-STFAC sy-vline
WA_PRDSCMMATKEY_STR-TRAGR sy-vline
WA_PRDSCMMATKEY_STR-TS_SDP_USE sy-vline
WA_PRDSCMMATKEY_STR-PRDHA sy-vline
WA_PRDSCMMATKEY_STR-GTIN_IND sy-vline
WA_PRDSCMMATKEY_STR-DISPLAY sy-vline
WA_PRDSCMMATKEY_STR-HERKL sy-vline
WA_PRDSCMMATKEY_STR-BATCH_REQ sy-vline
WA_PRDSCMMATKEY_STR-KITCOMP sy-vline
WA_PRDSCMMATKEY_STR-PRDSET_IND sy-vline
WA_PRDSCMMATKEY_STR-SERIAL sy-vline
WA_PRDSCMMATKEY_STR-DATA_CHECK_BY sy-vline
WA_PRDSCMMATKEY_STR-DATA_CHECK_AT sy-vline
WA_PRDSCMMATKEY_STR-KOSCH sy-vline
WA_PRDSCMMATKEY_STR-KOSCH_GRP sy-vline
WA_PRDSCMMATKEY_STR-VMIKOSCH sy-vline
WA_PRDSCMMATKEY_STR-VMIKOSCH_GRP sy-vline
WA_PRDSCMMATKEY_STR-PACKGR sy-vline
WA_PRDSCMMATKEY_STR-RMATP sy-vline
WA_PRDSCMMATKEY_STR-RMATID_PB sy-vline
WA_PRDSCMMATKEY_STR-HUTYP_DFLT sy-vline
WA_PRDSCMMATKEY_STR-CUOBJ sy-vline
WA_PRDSCMMATKEY_STR-SATID sy-vline
WA_PRDSCMMATKEY_STR-KZKFG sy-vline
WA_PRDSCMMATKEY_STR-MATVERS_HEAD sy-vline
WA_PRDSCMMATKEY_STR-MATVERS_TYPE sy-vline
WA_PRDSCMMATKEY_STR-MATVERS_CHNR sy-vline
WA_PRDSCMMATKEY_STR-TEMPRPBLOCK sy-vline
WA_PRDSCMMATKEY_STR-TEMPRPFROM sy-vline
WA_PRDSCMMATKEY_STR-TEMPRPTO sy-vline
WA_PRDSCMMATKEY_STR-DUMMY_PRD_INCL_EEW_PS sy-vline
WA_PRDSCMMATKEY_STR-LIKEID sy-vline
WA_PRDSCMMATKEY_STR-PHIN_FROM sy-vline
WA_PRDSCMMATKEY_STR-PHIN_TO sy-vline
WA_PRDSCMMATKEY_STR-PHIN_TSID sy-vline
WA_PRDSCMMATKEY_STR-PHOUT_FROM sy-vline
WA_PRDSCMMATKEY_STR-PHOUT_TO sy-vline
WA_PRDSCMMATKEY_STR-PHOUT_TSID sy-vline
WA_PRDSCMMATKEY_STR-LIFE_CYCLE_LOCID sy-vline
WA_PRDSCMMATKEY_STR-CREATEUSER sy-vline
WA_PRDSCMMATKEY_STR-CHANGEUSER sy-vline
WA_PRDSCMMATKEY_STR-PROD_SHAPE sy-vline
WA_PRDSCMMATKEY_STR-MO_PROFILE_ID sy-vline
WA_PRDSCMMATKEY_STR-OVERHANG_TRESH sy-vline
WA_PRDSCMMATKEY_STR-BRIDGE_TRESH sy-vline
WA_PRDSCMMATKEY_STR-BRIDGE_MAX_SLOPE sy-vline
WA_PRDSCMMATKEY_STR-HEIGHT_NONFLAT sy-vline
WA_PRDSCMMATKEY_STR-HEIGHT_NONFLAT_UOM sy-vline
WA_PRDSCMMATKEY_STR-CREATEUTC sy-vline
WA_PRDSCMMATKEY_STR-CHANGEUTC sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.