ABAP Select data from SAP table BAPINADDRX 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 BAPINADDRX 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 BAPINADDRX. 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 BAPINADDRX 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_BAPINADDRX TYPE STANDARD TABLE OF BAPINADDRX, WA_BAPINADDRX TYPE BAPINADDRX, 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: <BAPINADDRX> TYPE BAPINADDRX. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_BAPINADDRX. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM BAPINADDRX INTO TABLE IT_BAPINADDRX. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM BAPINADDRX * INTO TABLE @DATA(IT_BAPINADDRX2). *--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_BAPINADDRX INDEX 1 INTO DATA(WA_BAPINADDRX2). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_BAPINADDRX ASSIGNING <BAPINADDRX>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPINADDRX>-ADDR_NO = 1.
<BAPINADDRX>-COUNTRY = 1.
<BAPINADDRX>-COUNTRYX = 1.
<BAPINADDRX>-COUNTRY_ISO = 1.
<BAPINADDRX>-REGION = 1.
ENDLOOP. LOOP AT IT_BAPINADDRX INTO WA_BAPINADDRX. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_BAPINADDRX-REGIONX, sy-vline,
WA_BAPINADDRX-PCD, sy-vline,
WA_BAPINADDRX-PCDX, sy-vline,
WA_BAPINADDRX-CITY, sy-vline,
WA_BAPINADDRX-CITYX, sy-vline,
WA_BAPINADDRX-STD_CITY, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPINADDRX 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_BAPINADDRX 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_BAPINADDRX INTO WA_BAPINADDRX. *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 ADDR_NO CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPINADDRX-ADDR_NO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPINADDRX-ADDR_NO.
WRITE:/ 'New Value:', ld_input.
*Conversion exit SXIDN, internal->external for field E_MAIL CALL FUNCTION 'CONVERSION_EXIT_SXIDN_OUTPUT' EXPORTING input = WA_BAPINADDRX-E_MAIL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPINADDRX-E_MAIL.
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_BAPINADDRX_STR,
ADDR_NO TYPE STRING,
COUNTRY TYPE STRING,
COUNTRYX TYPE STRING,
COUNTRY_ISO TYPE STRING,
REGION TYPE STRING,
REGIONX TYPE STRING,
PCD TYPE STRING,
PCDX TYPE STRING,
CITY TYPE STRING,
CITYX TYPE STRING,
STD_CITY TYPE STRING,
DISTRICT TYPE STRING,
DISTRICTX TYPE STRING,
STR_NO TYPE STRING,
STR_NOX TYPE STRING,
STR_SUP TYPE STRING,
STR_SUPX TYPE STRING,
GEOGR_AREA TYPE STRING,
GEOGR_AREAX TYPE STRING,
POBOX_PCD TYPE STRING,
POBOX_PCDX TYPE STRING,
POBOX TYPE STRING,
POBOXX TYPE STRING,
POB_COUNTRY TYPE STRING,
POB_COUNTRYX TYPE STRING,
POB_COUNTRY_ISO TYPE STRING,
POBOX_CITY TYPE STRING,
POBOX_CITYX TYPE STRING,
COMPANY_PCD TYPE STRING,
COMPANY_PCDX TYPE STRING,
PHONESX TYPE STRING,
FAXNO TYPE STRING,
FAXNOX TYPE STRING,
FAXEXTENSION TYPE STRING,
FAXEXTENSIONX TYPE STRING,
TELEXNO TYPE STRING,
TELEXNOX TYPE STRING,
BUILDING TYPE STRING,
BUILDINGX TYPE STRING,
FLOOR TYPE STRING,
FLOORX TYPE STRING,
UNIT TYPE STRING,
UNITX TYPE STRING,
E_MAIL TYPE STRING,
E_MAILX TYPE STRING,
HOUSE_NO TYPE STRING,
HOUSE_NOX TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPINADDRX_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_BAPINADDRX_STR-ADDR_NO sy-vline
WA_BAPINADDRX_STR-COUNTRY sy-vline
WA_BAPINADDRX_STR-COUNTRYX sy-vline
WA_BAPINADDRX_STR-COUNTRY_ISO sy-vline
WA_BAPINADDRX_STR-REGION sy-vline
WA_BAPINADDRX_STR-REGIONX sy-vline
WA_BAPINADDRX_STR-PCD sy-vline
WA_BAPINADDRX_STR-PCDX sy-vline
WA_BAPINADDRX_STR-CITY sy-vline
WA_BAPINADDRX_STR-CITYX sy-vline
WA_BAPINADDRX_STR-STD_CITY sy-vline
WA_BAPINADDRX_STR-DISTRICT sy-vline
WA_BAPINADDRX_STR-DISTRICTX sy-vline
WA_BAPINADDRX_STR-STR_NO sy-vline
WA_BAPINADDRX_STR-STR_NOX sy-vline
WA_BAPINADDRX_STR-STR_SUP sy-vline
WA_BAPINADDRX_STR-STR_SUPX sy-vline
WA_BAPINADDRX_STR-GEOGR_AREA sy-vline
WA_BAPINADDRX_STR-GEOGR_AREAX sy-vline
WA_BAPINADDRX_STR-POBOX_PCD sy-vline
WA_BAPINADDRX_STR-POBOX_PCDX sy-vline
WA_BAPINADDRX_STR-POBOX sy-vline
WA_BAPINADDRX_STR-POBOXX sy-vline
WA_BAPINADDRX_STR-POB_COUNTRY sy-vline
WA_BAPINADDRX_STR-POB_COUNTRYX sy-vline
WA_BAPINADDRX_STR-POB_COUNTRY_ISO sy-vline
WA_BAPINADDRX_STR-POBOX_CITY sy-vline
WA_BAPINADDRX_STR-POBOX_CITYX sy-vline
WA_BAPINADDRX_STR-COMPANY_PCD sy-vline
WA_BAPINADDRX_STR-COMPANY_PCDX sy-vline
WA_BAPINADDRX_STR-PHONESX sy-vline
WA_BAPINADDRX_STR-FAXNO sy-vline
WA_BAPINADDRX_STR-FAXNOX sy-vline
WA_BAPINADDRX_STR-FAXEXTENSION sy-vline
WA_BAPINADDRX_STR-FAXEXTENSIONX sy-vline
WA_BAPINADDRX_STR-TELEXNO sy-vline
WA_BAPINADDRX_STR-TELEXNOX sy-vline
WA_BAPINADDRX_STR-BUILDING sy-vline
WA_BAPINADDRX_STR-BUILDINGX sy-vline
WA_BAPINADDRX_STR-FLOOR sy-vline
WA_BAPINADDRX_STR-FLOORX sy-vline
WA_BAPINADDRX_STR-UNIT sy-vline
WA_BAPINADDRX_STR-UNITX sy-vline
WA_BAPINADDRX_STR-E_MAIL sy-vline
WA_BAPINADDRX_STR-E_MAILX sy-vline
WA_BAPINADDRX_STR-HOUSE_NO sy-vline
WA_BAPINADDRX_STR-HOUSE_NOX sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.