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

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

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM /SMB/S_ADDRESS
  INTO TABLE IT_/SMB/S_ADDRESS.

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


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_/SMB/S_ADDRESS ASSIGNING </SMB/S_ADDRESS>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
</SMB/S_ADDRESS>-I_ADDR_NAME1 = 1.
</SMB/S_ADDRESS>-I_ADDR_NAME2 = 1.
</SMB/S_ADDRESS>-I_NAME3 = 1.
</SMB/S_ADDRESS>-I_NAME4 = 1.
</SMB/S_ADDRESS>-I_NAME_TXT = 1.
ENDLOOP.

LOOP AT IT_/SMB/S_ADDRESS INTO WA_/SMB/S_ADDRESS.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_/SMB/S_ADDRESS-I_NAME_CO, sy-vline,
WA_/SMB/S_ADDRESS-I_CITY1, sy-vline,
WA_/SMB/S_ADDRESS-I_CITY2, sy-vline,
WA_/SMB/S_ADDRESS-I_CITY_CODE, sy-vline,
WA_/SMB/S_ADDRESS-I_HOME_CITY, sy-vline,
WA_/SMB/S_ADDRESS-I_REGIOGROUP, sy-vline.
ENDLOOP. *Add any further fields from structure WA_/SMB/S_ADDRESS 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_/SMB/S_ADDRESS 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_/SMB/S_ADDRESS INTO WA_/SMB/S_ADDRESS. *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 I_CITY_CODE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_/SMB/S_ADDRESS-I_CITY_CODE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SMB/S_ADDRESS-I_CITY_CODE.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit SXIDN, internal->external for field I_SMTP_ADDR CALL FUNCTION 'CONVERSION_EXIT_SXIDN_OUTPUT' EXPORTING input = WA_/SMB/S_ADDRESS-I_SMTP_ADDR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SMB/S_ADDRESS-I_SMTP_ADDR.
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_/SMB/S_ADDRESS_STR,
I_ADDR_NAME1 TYPE STRING,
I_ADDR_NAME2 TYPE STRING,
I_NAME3 TYPE STRING,
I_NAME4 TYPE STRING,
I_NAME_TXT TYPE STRING,
I_NAME_CO TYPE STRING,
I_CITY1 TYPE STRING,
I_CITY2 TYPE STRING,
I_CITY_CODE TYPE STRING,
I_HOME_CITY TYPE STRING,
I_REGIOGROUP TYPE STRING,
I_POST_CODE1 TYPE STRING,
I_POST_CODE2 TYPE STRING,
I_POST_CODE3 TYPE STRING,
I_PO_BOX TYPE STRING,
I_DONT_USE_P TYPE STRING,
I_PO_BOX_NUM TYPE STRING,
I_PO_BOX_LOC TYPE STRING,
I_PO_BOX_REG TYPE STRING,
I_PO_BOX_CTY TYPE STRING,
I_TRANSPZONE TYPE STRING,
I_STREET TYPE STRING,
I_DONT_USE_S TYPE STRING,
I_STREETCODE TYPE STRING,
I_HOUSE_NUM1 TYPE STRING,
I_HOUSE_NUM2 TYPE STRING,
I_STR_SUPPL1 TYPE STRING,
I_STR_SUPPL2 TYPE STRING,
I_STR_SUPPL3 TYPE STRING,
I_LOCATION TYPE STRING,
I_BUILDING TYPE STRING,
I_FLOOR TYPE STRING,
I_ROOMNUMBER TYPE STRING,
I_COUNTRY TYPE STRING,
I_LANGU TYPE STRING,
I_REGION TYPE STRING,
I_SORT1 TYPE STRING,
I_SORT2 TYPE STRING,
I_EXTENSION1 TYPE STRING,
I_EXTENSION2 TYPE STRING,
I_TIME_ZONE TYPE STRING,
I_TAXJURCODE TYPE STRING,
I_REMARK TYPE STRING,
I_DEFLT_COMM TYPE STRING,
I_TITLE_MEDI TYPE STRING,
I_POSTCODE2 TYPE STRING,
I_POSTCODE2C TYPE STRING,
I_TEL_NUMBER TYPE STRING,
I_TEL_EXTENS TYPE STRING,
I_FAX_NUMBER TYPE STRING,
I_FAX_EXTENS TYPE STRING,
I_SMTP_ADDR TYPE STRING,
I_MOB_NUMBER TYPE STRING,
NODE_ID TYPE STRING,END OF T_EKKO_STR. DATA: WA_/SMB/S_ADDRESS_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_/SMB/S_ADDRESS_STR-I_ADDR_NAME1 sy-vline
WA_/SMB/S_ADDRESS_STR-I_ADDR_NAME2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_NAME3 sy-vline
WA_/SMB/S_ADDRESS_STR-I_NAME4 sy-vline
WA_/SMB/S_ADDRESS_STR-I_NAME_TXT sy-vline
WA_/SMB/S_ADDRESS_STR-I_NAME_CO sy-vline
WA_/SMB/S_ADDRESS_STR-I_CITY1 sy-vline
WA_/SMB/S_ADDRESS_STR-I_CITY2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_CITY_CODE sy-vline
WA_/SMB/S_ADDRESS_STR-I_HOME_CITY sy-vline
WA_/SMB/S_ADDRESS_STR-I_REGIOGROUP sy-vline
WA_/SMB/S_ADDRESS_STR-I_POST_CODE1 sy-vline
WA_/SMB/S_ADDRESS_STR-I_POST_CODE2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_POST_CODE3 sy-vline
WA_/SMB/S_ADDRESS_STR-I_PO_BOX sy-vline
WA_/SMB/S_ADDRESS_STR-I_DONT_USE_P sy-vline
WA_/SMB/S_ADDRESS_STR-I_PO_BOX_NUM sy-vline
WA_/SMB/S_ADDRESS_STR-I_PO_BOX_LOC sy-vline
WA_/SMB/S_ADDRESS_STR-I_PO_BOX_REG sy-vline
WA_/SMB/S_ADDRESS_STR-I_PO_BOX_CTY sy-vline
WA_/SMB/S_ADDRESS_STR-I_TRANSPZONE sy-vline
WA_/SMB/S_ADDRESS_STR-I_STREET sy-vline
WA_/SMB/S_ADDRESS_STR-I_DONT_USE_S sy-vline
WA_/SMB/S_ADDRESS_STR-I_STREETCODE sy-vline
WA_/SMB/S_ADDRESS_STR-I_HOUSE_NUM1 sy-vline
WA_/SMB/S_ADDRESS_STR-I_HOUSE_NUM2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_STR_SUPPL1 sy-vline
WA_/SMB/S_ADDRESS_STR-I_STR_SUPPL2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_STR_SUPPL3 sy-vline
WA_/SMB/S_ADDRESS_STR-I_LOCATION sy-vline
WA_/SMB/S_ADDRESS_STR-I_BUILDING sy-vline
WA_/SMB/S_ADDRESS_STR-I_FLOOR sy-vline
WA_/SMB/S_ADDRESS_STR-I_ROOMNUMBER sy-vline
WA_/SMB/S_ADDRESS_STR-I_COUNTRY sy-vline
WA_/SMB/S_ADDRESS_STR-I_LANGU sy-vline
WA_/SMB/S_ADDRESS_STR-I_REGION sy-vline
WA_/SMB/S_ADDRESS_STR-I_SORT1 sy-vline
WA_/SMB/S_ADDRESS_STR-I_SORT2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_EXTENSION1 sy-vline
WA_/SMB/S_ADDRESS_STR-I_EXTENSION2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_TIME_ZONE sy-vline
WA_/SMB/S_ADDRESS_STR-I_TAXJURCODE sy-vline
WA_/SMB/S_ADDRESS_STR-I_REMARK sy-vline
WA_/SMB/S_ADDRESS_STR-I_DEFLT_COMM sy-vline
WA_/SMB/S_ADDRESS_STR-I_TITLE_MEDI sy-vline
WA_/SMB/S_ADDRESS_STR-I_POSTCODE2 sy-vline
WA_/SMB/S_ADDRESS_STR-I_POSTCODE2C sy-vline
WA_/SMB/S_ADDRESS_STR-I_TEL_NUMBER sy-vline
WA_/SMB/S_ADDRESS_STR-I_TEL_EXTENS sy-vline
WA_/SMB/S_ADDRESS_STR-I_FAX_NUMBER sy-vline
WA_/SMB/S_ADDRESS_STR-I_FAX_EXTENS sy-vline
WA_/SMB/S_ADDRESS_STR-I_SMTP_ADDR sy-vline
WA_/SMB/S_ADDRESS_STR-I_MOB_NUMBER sy-vline
WA_/SMB/S_ADDRESS_STR-NODE_ID sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.