ABAP Select data from SAP table MDG_S_BP_POSTAL_ENRICHMENT 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 MDG_S_BP_POSTAL_ENRICHMENT 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 MDG_S_BP_POSTAL_ENRICHMENT. 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 MDG_S_BP_POSTAL_ENRICHMENT 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_MDG_S_BP_POSTAL_ENRICHMENT TYPE STANDARD TABLE OF MDG_S_BP_POSTAL_ENRICHMENT,
      WA_MDG_S_BP_POSTAL_ENRICHMENT TYPE MDG_S_BP_POSTAL_ENRICHMENT,
      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: <MDG_S_BP_POSTAL_ENRICHMENT> TYPE MDG_S_BP_POSTAL_ENRICHMENT.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM MDG_S_BP_POSTAL_ENRICHMENT
*  INTO TABLE @DATA(IT_MDG_S_BP_POSTAL_ENRICHMENT2).
*--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_MDG_S_BP_POSTAL_ENRICHMENT INDEX 1 INTO DATA(WA_MDG_S_BP_POSTAL_ENRICHMENT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_MDG_S_BP_POSTAL_ENRICHMENT ASSIGNING <MDG_S_BP_POSTAL_ENRICHMENT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<MDG_S_BP_POSTAL_ENRICHMENT>-ADDRNO = 1.
<MDG_S_BP_POSTAL_ENRICHMENT>-BP_HEADER = 1.
<MDG_S_BP_POSTAL_ENRICHMENT>-AD_NATION = 1.
<MDG_S_BP_POSTAL_ENRICHMENT>-BUILDING = 1.
<MDG_S_BP_POSTAL_ENRICHMENT>-CHCKSTATU = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_MDG_S_BP_POSTAL_ENRICHMENT-CITY1, sy-vline,
WA_MDG_S_BP_POSTAL_ENRICHMENT-CO_NAME, sy-vline,
WA_MDG_S_BP_POSTAL_ENRICHMENT-CTY_CODE, sy-vline,
WA_MDG_S_BP_POSTAL_ENRICHMENT-DEFLTCOMM, sy-vline,
WA_MDG_S_BP_POSTAL_ENRICHMENT-DISTRICT, sy-vline,
WA_MDG_S_BP_POSTAL_ENRICHMENT-DLVSRV_NR, sy-vline.
ENDLOOP. *Add any further fields from structure WA_MDG_S_BP_POSTAL_ENRICHMENT 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_MDG_S_BP_POSTAL_ENRICHMENT 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_MDG_S_BP_POSTAL_ENRICHMENT INTO WA_MDG_S_BP_POSTAL_ENRICHMENT. *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 ADDRNO CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MDG_S_BP_POSTAL_ENRICHMENT-ADDRNO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MDG_S_BP_POSTAL_ENRICHMENT-ADDRNO.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ISOLA, internal->external for field LANGU_COM CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_MDG_S_BP_POSTAL_ENRICHMENT-LANGU_COM IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MDG_S_BP_POSTAL_ENRICHMENT-LANGU_COM.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ALPHA, internal->external for field RF_POSTAL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MDG_S_BP_POSTAL_ENRICHMENT-RF_POSTAL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MDG_S_BP_POSTAL_ENRICHMENT-RF_POSTAL.
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_MDG_S_BP_POSTAL_ENRICHMENT_STR,
ADDRNO TYPE STRING,
BP_HEADER TYPE STRING,
AD_NATION TYPE STRING,
BUILDING TYPE STRING,
CHCKSTATU TYPE STRING,
CITY1 TYPE STRING,
CO_NAME TYPE STRING,
CTY_CODE TYPE STRING,
DEFLTCOMM TYPE STRING,
DISTRICT TYPE STRING,
DLVSRV_NR TYPE STRING,
DLVSRV_TY TYPE STRING,
DONTUSE_P TYPE STRING,
DONTUSE_S TYPE STRING,
FLAGGROUP TYPE STRING,
FLOOR TYPE STRING,
HOME_CITY TYPE STRING,
HOUSE_NR1 TYPE STRING,
HOUSE_NR2 TYPE STRING,
LANGU_COM TYPE STRING,
LOCATION TYPE STRING,
MC_CITY1 TYPE STRING,
MC_STREET TYPE STRING,
PERS_ADDR TYPE STRING,
POBOX_CTY TYPE STRING,
POBOX_LBY TYPE STRING,
POBOX_LOC TYPE STRING,
POBOX_NUM TYPE STRING,
POST_COD1 TYPE STRING,
POST_COD2 TYPE STRING,
POST_COD3 TYPE STRING,
PO_BOX TYPE STRING,
REF_POSTA TYPE STRING,
REF_PSTAL TYPE STRING,
REGIOGROU TYPE STRING,
RFE_POST TYPE STRING,
RFE_POSTA TYPE STRING,
RFE_POSTL TYPE STRING,
RFE_PSTAL TYPE STRING,
RF_POSTAL TYPE STRING,
ROOMNUM TYPE STRING,
SORT1_AD TYPE STRING,
SORT2_AD TYPE STRING,
STREET TYPE STRING,
STRSUPPL1 TYPE STRING,
STRSUPPL2 TYPE STRING,
STRSUPPL3 TYPE STRING,
TAXJURCOD TYPE STRING,
TIME_ZONE TYPE STRING,END OF T_EKKO_STR. DATA: WA_MDG_S_BP_POSTAL_ENRICHMENT_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_MDG_S_BP_POSTAL_ENRICHMENT_STR-ADDRNO sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-BP_HEADER sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-AD_NATION sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-BUILDING sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-CHCKSTATU sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-CITY1 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-CO_NAME sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-CTY_CODE sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-DEFLTCOMM sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-DISTRICT sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-DLVSRV_NR sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-DLVSRV_TY sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-DONTUSE_P sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-DONTUSE_S sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-FLAGGROUP sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-FLOOR sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-HOME_CITY sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-HOUSE_NR1 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-HOUSE_NR2 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-LANGU_COM sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-LOCATION sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-MC_CITY1 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-MC_STREET sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-PERS_ADDR sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-POBOX_CTY sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-POBOX_LBY sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-POBOX_LOC sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-POBOX_NUM sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-POST_COD1 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-POST_COD2 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-POST_COD3 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-PO_BOX sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-REF_POSTA sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-REF_PSTAL sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-REGIOGROU sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-RFE_POST sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-RFE_POSTA sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-RFE_POSTL sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-RFE_PSTAL sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-RF_POSTAL sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-ROOMNUM sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-SORT1_AD sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-SORT2_AD sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-STREET sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-STRSUPPL1 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-STRSUPPL2 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-STRSUPPL3 sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-TAXJURCOD sy-vline
WA_MDG_S_BP_POSTAL_ENRICHMENT_STR-TIME_ZONE sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.