ABAP Select data from SAP table ADR_LSM6 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 ADR_LSM6 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 ADR_LSM6. 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 ADR_LSM6 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_ADR_LSM6 TYPE STANDARD TABLE OF ADR_LSM6,
      WA_ADR_LSM6 TYPE ADR_LSM6,
      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: <ADR_LSM6> TYPE ADR_LSM6.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM ADR_LSM6
*  INTO TABLE @DATA(IT_ADR_LSM62).
*--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_ADR_LSM6 INDEX 1 INTO DATA(WA_ADR_LSM62).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_ADR_LSM6 ASSIGNING <ADR_LSM6>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<ADR_LSM6>-LANGU = 1.
<ADR_LSM6>-DATA_ID = 1.
<ADR_LSM6>-STATUS = 1.
<ADR_LSM6>-CITY_CODE = 1.
<ADR_LSM6>-COMMU_CODE = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_ADR_LSM6-REGION, sy-vline,
WA_ADR_LSM6-TIME_ZONE, sy-vline,
WA_ADR_LSM6-TAXJURCODE, sy-vline,
WA_ADR_LSM6-TRANSPZONE, sy-vline,
WA_ADR_LSM6-REGIOGROUP, sy-vline,
WA_ADR_LSM6-CITY_NAME, sy-vline.
ENDLOOP. *Add any further fields from structure WA_ADR_LSM6 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_ADR_LSM6 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_ADR_LSM6 INTO WA_ADR_LSM6. *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 ISOLA, internal->external for field LANGU CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_ADR_LSM6-LANGU IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_ADR_LSM6-LANGU.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field R_TOWNSHIPCODE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_ADR_LSM6-R_TOWNSHIPCODE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_ADR_LSM6-R_TOWNSHIPCODE.
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_ADR_LSM6_STR,
LANGU TYPE STRING,
DATA_ID TYPE STRING,
STATUS TYPE STRING,
CITY_CODE TYPE STRING,
COMMU_CODE TYPE STRING,
REGION TYPE STRING,
TIME_ZONE TYPE STRING,
TAXJURCODE TYPE STRING,
TRANSPZONE TYPE STRING,
REGIOGROUP TYPE STRING,
CITY_NAME TYPE STRING,
CITY_SHORT TYPE STRING,
CITY_SH10 TYPE STRING,
CITY_EXT TYPE STRING,
CITYP_CODE TYPE STRING,
CITY_PART TYPE STRING,
PO_BOX_HGH TYPE STRING,
PO_BOX_LOW TYPE STRING,
PO_BOX_IND TYPE STRING,
PO_BOX_TXT TYPE STRING,
POST_CODE TYPE STRING,
FLAG_DELIV TYPE STRING,
FLAG_POBOX TYPE STRING,
FLAG_CUST TYPE STRING,
FLAG_CSTGR TYPE STRING,
STRT_CODE TYPE STRING,
STRTYPEAB TYPE STRING,
STREET TYPE STRING,
STREET_SHR TYPE STRING,
STREET_S15 TYPE STRING,
HOUSENUM_H TYPE STRING,
HSN_SUPP_H TYPE STRING,
HOUSENUM_L TYPE STRING,
HSN_SUPP_L TYPE STRING,
HSN_IND TYPE STRING,
POSTALAREA TYPE STRING,
CITY_NM_PS TYPE STRING,
CITY_CD_PS TYPE STRING,
MC_CITY TYPE STRING,
MC_STREET TYPE STRING,
COUNTY_CODE TYPE STRING,
COUNTY TYPE STRING,
TOWNSHIP_CODE TYPE STRING,
TOWNSHIP TYPE STRING,
MC_COUNTY TYPE STRING,
MC_TOWNSHIP TYPE STRING,
R_CITYCODE TYPE STRING,
R_COMMUCOD TYPE STRING,
R_REGION TYPE STRING,
R_TIMEZONE TYPE STRING,
R_TAXJURCD TYPE STRING,
R_TRANSPZN TYPE STRING,
R_REGIOGRP TYPE STRING,
R_CITYNAME TYPE STRING,
R_CITYSHRT TYPE STRING,
R_CITYSH10 TYPE STRING,
R_CITY_EXT TYPE STRING,
R_CITYP_CD TYPE STRING,
R_CITY_PRT TYPE STRING,
R_POBOXHGH TYPE STRING,
R_POBOXLOW TYPE STRING,
R_POBOXIND TYPE STRING,
R_POBOXTXT TYPE STRING,
R_POST_COD TYPE STRING,
R_FLGDELIV TYPE STRING,
R_FLGPOBOX TYPE STRING,
R_FLAGCUST TYPE STRING,
R_FLGCSTGR TYPE STRING,
R_STRTCODE TYPE STRING,
R_STRTYPAB TYPE STRING,
R_STREET TYPE STRING,
R_STRT_SHR TYPE STRING,
R_STRT_S15 TYPE STRING,
R_HSNUM_H TYPE STRING,
R_HSNSUPPH TYPE STRING,
R_HSNUM_L TYPE STRING,
R_HSNSUPPL TYPE STRING,
R_HSN_IND TYPE STRING,
R_POSTALAR TYPE STRING,
R_CT_NM_PS TYPE STRING,
R_CT_CD_PS TYPE STRING,
R_MC_CITY TYPE STRING,
R_MC_STREET TYPE STRING,
R_COUNTYCODE TYPE STRING,
R_COUNTY TYPE STRING,
R_TOWNSHIPCODE TYPE STRING,
R_TOWNSHIP TYPE STRING,
R_MC_COUNTY TYPE STRING,
R_MC_TOWNSHIP TYPE STRING,END OF T_EKKO_STR. DATA: WA_ADR_LSM6_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_ADR_LSM6_STR-LANGU sy-vline
WA_ADR_LSM6_STR-DATA_ID sy-vline
WA_ADR_LSM6_STR-STATUS sy-vline
WA_ADR_LSM6_STR-CITY_CODE sy-vline
WA_ADR_LSM6_STR-COMMU_CODE sy-vline
WA_ADR_LSM6_STR-REGION sy-vline
WA_ADR_LSM6_STR-TIME_ZONE sy-vline
WA_ADR_LSM6_STR-TAXJURCODE sy-vline
WA_ADR_LSM6_STR-TRANSPZONE sy-vline
WA_ADR_LSM6_STR-REGIOGROUP sy-vline
WA_ADR_LSM6_STR-CITY_NAME sy-vline
WA_ADR_LSM6_STR-CITY_SHORT sy-vline
WA_ADR_LSM6_STR-CITY_SH10 sy-vline
WA_ADR_LSM6_STR-CITY_EXT sy-vline
WA_ADR_LSM6_STR-CITYP_CODE sy-vline
WA_ADR_LSM6_STR-CITY_PART sy-vline
WA_ADR_LSM6_STR-PO_BOX_HGH sy-vline
WA_ADR_LSM6_STR-PO_BOX_LOW sy-vline
WA_ADR_LSM6_STR-PO_BOX_IND sy-vline
WA_ADR_LSM6_STR-PO_BOX_TXT sy-vline
WA_ADR_LSM6_STR-POST_CODE sy-vline
WA_ADR_LSM6_STR-FLAG_DELIV sy-vline
WA_ADR_LSM6_STR-FLAG_POBOX sy-vline
WA_ADR_LSM6_STR-FLAG_CUST sy-vline
WA_ADR_LSM6_STR-FLAG_CSTGR sy-vline
WA_ADR_LSM6_STR-STRT_CODE sy-vline
WA_ADR_LSM6_STR-STRTYPEAB sy-vline
WA_ADR_LSM6_STR-STREET sy-vline
WA_ADR_LSM6_STR-STREET_SHR sy-vline
WA_ADR_LSM6_STR-STREET_S15 sy-vline
WA_ADR_LSM6_STR-HOUSENUM_H sy-vline
WA_ADR_LSM6_STR-HSN_SUPP_H sy-vline
WA_ADR_LSM6_STR-HOUSENUM_L sy-vline
WA_ADR_LSM6_STR-HSN_SUPP_L sy-vline
WA_ADR_LSM6_STR-HSN_IND sy-vline
WA_ADR_LSM6_STR-POSTALAREA sy-vline
WA_ADR_LSM6_STR-CITY_NM_PS sy-vline
WA_ADR_LSM6_STR-CITY_CD_PS sy-vline
WA_ADR_LSM6_STR-MC_CITY sy-vline
WA_ADR_LSM6_STR-MC_STREET sy-vline
WA_ADR_LSM6_STR-COUNTY_CODE sy-vline
WA_ADR_LSM6_STR-COUNTY sy-vline
WA_ADR_LSM6_STR-TOWNSHIP_CODE sy-vline
WA_ADR_LSM6_STR-TOWNSHIP sy-vline
WA_ADR_LSM6_STR-MC_COUNTY sy-vline
WA_ADR_LSM6_STR-MC_TOWNSHIP sy-vline
WA_ADR_LSM6_STR-R_CITYCODE sy-vline
WA_ADR_LSM6_STR-R_COMMUCOD sy-vline
WA_ADR_LSM6_STR-R_REGION sy-vline
WA_ADR_LSM6_STR-R_TIMEZONE sy-vline
WA_ADR_LSM6_STR-R_TAXJURCD sy-vline
WA_ADR_LSM6_STR-R_TRANSPZN sy-vline
WA_ADR_LSM6_STR-R_REGIOGRP sy-vline
WA_ADR_LSM6_STR-R_CITYNAME sy-vline
WA_ADR_LSM6_STR-R_CITYSHRT sy-vline
WA_ADR_LSM6_STR-R_CITYSH10 sy-vline
WA_ADR_LSM6_STR-R_CITY_EXT sy-vline
WA_ADR_LSM6_STR-R_CITYP_CD sy-vline
WA_ADR_LSM6_STR-R_CITY_PRT sy-vline
WA_ADR_LSM6_STR-R_POBOXHGH sy-vline
WA_ADR_LSM6_STR-R_POBOXLOW sy-vline
WA_ADR_LSM6_STR-R_POBOXIND sy-vline
WA_ADR_LSM6_STR-R_POBOXTXT sy-vline
WA_ADR_LSM6_STR-R_POST_COD sy-vline
WA_ADR_LSM6_STR-R_FLGDELIV sy-vline
WA_ADR_LSM6_STR-R_FLGPOBOX sy-vline
WA_ADR_LSM6_STR-R_FLAGCUST sy-vline
WA_ADR_LSM6_STR-R_FLGCSTGR sy-vline
WA_ADR_LSM6_STR-R_STRTCODE sy-vline
WA_ADR_LSM6_STR-R_STRTYPAB sy-vline
WA_ADR_LSM6_STR-R_STREET sy-vline
WA_ADR_LSM6_STR-R_STRT_SHR sy-vline
WA_ADR_LSM6_STR-R_STRT_S15 sy-vline
WA_ADR_LSM6_STR-R_HSNUM_H sy-vline
WA_ADR_LSM6_STR-R_HSNSUPPH sy-vline
WA_ADR_LSM6_STR-R_HSNUM_L sy-vline
WA_ADR_LSM6_STR-R_HSNSUPPL sy-vline
WA_ADR_LSM6_STR-R_HSN_IND sy-vline
WA_ADR_LSM6_STR-R_POSTALAR sy-vline
WA_ADR_LSM6_STR-R_CT_NM_PS sy-vline
WA_ADR_LSM6_STR-R_CT_CD_PS sy-vline
WA_ADR_LSM6_STR-R_MC_CITY sy-vline
WA_ADR_LSM6_STR-R_MC_STREET sy-vline
WA_ADR_LSM6_STR-R_COUNTYCODE sy-vline
WA_ADR_LSM6_STR-R_COUNTY sy-vline
WA_ADR_LSM6_STR-R_TOWNSHIPCODE sy-vline
WA_ADR_LSM6_STR-R_TOWNSHIP sy-vline
WA_ADR_LSM6_STR-R_MC_COUNTY sy-vline
WA_ADR_LSM6_STR-R_MC_TOWNSHIP sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.