ABAP Select data from SAP table BAPI_J_1IEXCHDR 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 BAPI_J_1IEXCHDR 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 BAPI_J_1IEXCHDR. 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 BAPI_J_1IEXCHDR 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_BAPI_J_1IEXCHDR TYPE STANDARD TABLE OF BAPI_J_1IEXCHDR,
      WA_BAPI_J_1IEXCHDR TYPE BAPI_J_1IEXCHDR,
      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: <BAPI_J_1IEXCHDR> TYPE BAPI_J_1IEXCHDR.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPI_J_1IEXCHDR
*  INTO TABLE @DATA(IT_BAPI_J_1IEXCHDR2).
*--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_BAPI_J_1IEXCHDR INDEX 1 INTO DATA(WA_BAPI_J_1IEXCHDR2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPI_J_1IEXCHDR ASSIGNING <BAPI_J_1IEXCHDR>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPI_J_1IEXCHDR>-LOGICAL_SYSTEM = 1.
<BAPI_J_1IEXCHDR>-COMPANY_CODE = 1.
<BAPI_J_1IEXCHDR>-PLANT = 1.
<BAPI_J_1IEXCHDR>-EXCISE_GROUP = 1.
<BAPI_J_1IEXCHDR>-SERIES_GROUP = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPI_J_1IEXCHDR-SUB_TRANSACTION, sy-vline,
WA_BAPI_J_1IEXCHDR-EXCISE_NUMBER, sy-vline,
WA_BAPI_J_1IEXCHDR-EXCISE_DATE, sy-vline,
WA_BAPI_J_1IEXCHDR-EXCISE_YEAR, sy-vline,
WA_BAPI_J_1IEXCHDR-REMOVAL_TIME, sy-vline,
WA_BAPI_J_1IEXCHDR-VENDOR, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPI_J_1IEXCHDR 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_BAPI_J_1IEXCHDR 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_BAPI_J_1IEXCHDR INTO WA_BAPI_J_1IEXCHDR. *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 LOGICAL_SYSTEM CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_J_1IEXCHDR-LOGICAL_SYSTEM IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_J_1IEXCHDR-LOGICAL_SYSTEM.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field EXCISE_YEAR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_BAPI_J_1IEXCHDR-EXCISE_YEAR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_J_1IEXCHDR-EXCISE_YEAR.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit ALPHA, internal->external for field SHIP_TO CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPI_J_1IEXCHDR-SHIP_TO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_J_1IEXCHDR-SHIP_TO.
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_BAPI_J_1IEXCHDR_STR,
LOGICAL_SYSTEM TYPE STRING,
COMPANY_CODE TYPE STRING,
PLANT TYPE STRING,
EXCISE_GROUP TYPE STRING,
SERIES_GROUP TYPE STRING,
SUB_TRANSACTION TYPE STRING,
EXCISE_NUMBER TYPE STRING,
EXCISE_DATE TYPE STRING,
EXCISE_YEAR TYPE STRING,
REMOVAL_TIME TYPE STRING,
VENDOR TYPE STRING,
SHIP_FROM TYPE STRING,
RECEIVING_PLANT TYPE STRING,
CURRENCY TYPE STRING,
EXCISE_BED TYPE STRING,
EXCISE_SED TYPE STRING,
EXCISE_AED TYPE STRING,
EXCISE_CESS TYPE STRING,
EXCISE_ED_CESS TYPE STRING,
EXCISE_HED_CESS TYPE STRING,
REFERENCE_DOC TYPE STRING,
REF_YEAR TYPE STRING,
REF_INDICATOR TYPE STRING,
SOLD_TO TYPE STRING,
SHIP_TO TYPE STRING,
BUSINESS_AREA TYPE STRING,
ECC_NUMBER TYPE STRING,
REGISTRATION_NO TYPE STRING,
RANGE TYPE STRING,
DIVISION TYPE STRING,
COLLECTORATE TYPE STRING,
CST_NO TYPE STRING,
LST_NO TYPE STRING,
PAN_NO TYPE STRING,
PREPRINTED_NO TYPE STRING,
EXPORT_TYPE TYPE STRING,
REJECTION_CODE TYPE STRING,
MRP_INDICATOR TYPE STRING,
CREATED_BY TYPE STRING,
REVERSAL TYPE STRING,
OBJ_TYPE TYPE STRING,
OBJ_KEY TYPE STRING,
OBJ_KEY_R TYPE STRING,
OBJ_KEY_P TYPE STRING,
NCCD TYPE STRING,
RULES TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPI_J_1IEXCHDR_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_BAPI_J_1IEXCHDR_STR-LOGICAL_SYSTEM sy-vline
WA_BAPI_J_1IEXCHDR_STR-COMPANY_CODE sy-vline
WA_BAPI_J_1IEXCHDR_STR-PLANT sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_GROUP sy-vline
WA_BAPI_J_1IEXCHDR_STR-SERIES_GROUP sy-vline
WA_BAPI_J_1IEXCHDR_STR-SUB_TRANSACTION sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_NUMBER sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_DATE sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_YEAR sy-vline
WA_BAPI_J_1IEXCHDR_STR-REMOVAL_TIME sy-vline
WA_BAPI_J_1IEXCHDR_STR-VENDOR sy-vline
WA_BAPI_J_1IEXCHDR_STR-SHIP_FROM sy-vline
WA_BAPI_J_1IEXCHDR_STR-RECEIVING_PLANT sy-vline
WA_BAPI_J_1IEXCHDR_STR-CURRENCY sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_BED sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_SED sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_AED sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_CESS sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_ED_CESS sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXCISE_HED_CESS sy-vline
WA_BAPI_J_1IEXCHDR_STR-REFERENCE_DOC sy-vline
WA_BAPI_J_1IEXCHDR_STR-REF_YEAR sy-vline
WA_BAPI_J_1IEXCHDR_STR-REF_INDICATOR sy-vline
WA_BAPI_J_1IEXCHDR_STR-SOLD_TO sy-vline
WA_BAPI_J_1IEXCHDR_STR-SHIP_TO sy-vline
WA_BAPI_J_1IEXCHDR_STR-BUSINESS_AREA sy-vline
WA_BAPI_J_1IEXCHDR_STR-ECC_NUMBER sy-vline
WA_BAPI_J_1IEXCHDR_STR-REGISTRATION_NO sy-vline
WA_BAPI_J_1IEXCHDR_STR-RANGE sy-vline
WA_BAPI_J_1IEXCHDR_STR-DIVISION sy-vline
WA_BAPI_J_1IEXCHDR_STR-COLLECTORATE sy-vline
WA_BAPI_J_1IEXCHDR_STR-CST_NO sy-vline
WA_BAPI_J_1IEXCHDR_STR-LST_NO sy-vline
WA_BAPI_J_1IEXCHDR_STR-PAN_NO sy-vline
WA_BAPI_J_1IEXCHDR_STR-PREPRINTED_NO sy-vline
WA_BAPI_J_1IEXCHDR_STR-EXPORT_TYPE sy-vline
WA_BAPI_J_1IEXCHDR_STR-REJECTION_CODE sy-vline
WA_BAPI_J_1IEXCHDR_STR-MRP_INDICATOR sy-vline
WA_BAPI_J_1IEXCHDR_STR-CREATED_BY sy-vline
WA_BAPI_J_1IEXCHDR_STR-REVERSAL sy-vline
WA_BAPI_J_1IEXCHDR_STR-OBJ_TYPE sy-vline
WA_BAPI_J_1IEXCHDR_STR-OBJ_KEY sy-vline
WA_BAPI_J_1IEXCHDR_STR-OBJ_KEY_R sy-vline
WA_BAPI_J_1IEXCHDR_STR-OBJ_KEY_P sy-vline
WA_BAPI_J_1IEXCHDR_STR-NCCD sy-vline
WA_BAPI_J_1IEXCHDR_STR-RULES sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.