ABAP Select data from SAP table BAPISDHEADX 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 BAPISDHEADX 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 BAPISDHEADX. 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 BAPISDHEADX 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_BAPISDHEADX TYPE STANDARD TABLE OF BAPISDHEADX,
      WA_BAPISDHEADX TYPE BAPISDHEADX,
      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: <BAPISDHEADX> TYPE BAPISDHEADX.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPISDHEADX
*  INTO TABLE @DATA(IT_BAPISDHEADX2).
*--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_BAPISDHEADX INDEX 1 INTO DATA(WA_BAPISDHEADX2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPISDHEADX ASSIGNING <BAPISDHEADX>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPISDHEADX>-DOC_NUMBER = 1.
<BAPISDHEADX>-UPDATEFLAG = 1.
<BAPISDHEADX>-DOC_TYPE = 1.
<BAPISDHEADX>-COLLECT_NO = 1.
<BAPISDHEADX>-SALES_ORG = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPISDHEADX-DISTR_CHAN, sy-vline,
WA_BAPISDHEADX-DIVISION, sy-vline,
WA_BAPISDHEADX-SALES_GRP, sy-vline,
WA_BAPISDHEADX-SALES_OFF, sy-vline,
WA_BAPISDHEADX-REQ_DATE_H, sy-vline,
WA_BAPISDHEADX-DATE_TYPE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPISDHEADX 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_BAPISDHEADX 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_BAPISDHEADX INTO WA_BAPISDHEADX. *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 DOC_NUMBER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPISDHEADX-DOC_NUMBER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPISDHEADX-DOC_NUMBER.
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_BAPISDHEADX_STR,
DOC_NUMBER TYPE STRING,
UPDATEFLAG TYPE STRING,
DOC_TYPE TYPE STRING,
COLLECT_NO TYPE STRING,
SALES_ORG TYPE STRING,
DISTR_CHAN TYPE STRING,
DIVISION TYPE STRING,
SALES_GRP TYPE STRING,
SALES_OFF TYPE STRING,
REQ_DATE_H TYPE STRING,
DATE_TYPE TYPE STRING,
PURCH_NO TYPE STRING,
PURCH_DATE TYPE STRING,
PO_METHOD TYPE STRING,
PO_SUPPLEM TYPE STRING,
REF_1 TYPE STRING,
NAME TYPE STRING,
TELEPHONE TYPE STRING,
PRICE_GRP TYPE STRING,
CUST_GROUP TYPE STRING,
SALES_DIST TYPE STRING,
PRICE_LIST TYPE STRING,
INCOTERMS1 TYPE STRING,
INCOTERMS2 TYPE STRING,
PMNTTRMS TYPE STRING,
DLV_BLOCK TYPE STRING,
BILL_BLOCK TYPE STRING,
ORD_REASON TYPE STRING,
COMPL_DLV TYPE STRING,
PRICE_DATE TYPE STRING,
QT_VALID_F TYPE STRING,
QT_VALID_T TYPE STRING,
CT_VALID_F TYPE STRING,
CT_VALID_T TYPE STRING,
CUST_GRP1 TYPE STRING,
CUST_GRP2 TYPE STRING,
CUST_GRP3 TYPE STRING,
CUST_GRP4 TYPE STRING,
CUST_GRP5 TYPE STRING,
PURCH_NO_C TYPE STRING,
PURCH_NO_S TYPE STRING,
PO_DAT_S TYPE STRING,
PO_METH_S TYPE STRING,
REF_1_S TYPE STRING,
DOC_DATE TYPE STRING,
WAR_DATE TYPE STRING,
SHIP_COND TYPE STRING,
PP_SEARCH TYPE STRING,
DUN_COUNT TYPE STRING,
DUN_DATE TYPE STRING,
DLVSCHEDUSE TYPE STRING,
RP_DEL_SCHED TYPE STRING,
REF_DOC TYPE STRING,
COMP_CODE_B TYPE STRING,
ALT_TAX_CLASS TYPE STRING,
TAX_CLASS2 TYPE STRING,
TAX_CLASS3 TYPE STRING,
TAX_CLASS4 TYPE STRING,
TAX_CLASS5 TYPE STRING,
TAX_CLASS6 TYPE STRING,
TAX_CLASS7 TYPE STRING,
TAX_CLASS8 TYPE STRING,
TAX_CLASS9 TYPE STRING,
REF_DOC_L TYPE STRING,
ASS_NUMBER TYPE STRING,
REF_DOC_CAT TYPE STRING,
ORD_COMB_IN TYPE STRING,
BILL_SCHED TYPE STRING,
INVO_SCHED TYPE STRING,
MN_INVOICE TYPE STRING,
EXCH_RATE_FI TYPE STRING,
ADD_VAL_DY TYPE STRING,
FIX_VAL_DY TYPE STRING,
PYMT_METH TYPE STRING,
ACCNT_ASGN TYPE STRING,
EXCHG_RATE TYPE STRING,
BILL_DATE TYPE STRING,
SERV_DATE TYPE STRING,
DUNN_KEY TYPE STRING,
DUNN_BLOCK TYPE STRING,
PYMT_GAR_PROC TYPE STRING,
DEPARTMENT_NO TYPE STRING,
REC_POINT TYPE STRING,
PO_ITM_NO_S TYPE STRING,
DOC_NUM_FI TYPE STRING,
CUST_COND_GRP1 TYPE STRING,
CUST_COND_GRP2 TYPE STRING,
CUST_COND_GRP3 TYPE STRING,
CUST_COND_GRP4 TYPE STRING,
CUST_COND_GRP5 TYPE STRING,
DLV_TIME TYPE STRING,
CURRENCY TYPE STRING,
REF_DOC_CAT_LONG TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPISDHEADX_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_BAPISDHEADX_STR-DOC_NUMBER sy-vline
WA_BAPISDHEADX_STR-UPDATEFLAG sy-vline
WA_BAPISDHEADX_STR-DOC_TYPE sy-vline
WA_BAPISDHEADX_STR-COLLECT_NO sy-vline
WA_BAPISDHEADX_STR-SALES_ORG sy-vline
WA_BAPISDHEADX_STR-DISTR_CHAN sy-vline
WA_BAPISDHEADX_STR-DIVISION sy-vline
WA_BAPISDHEADX_STR-SALES_GRP sy-vline
WA_BAPISDHEADX_STR-SALES_OFF sy-vline
WA_BAPISDHEADX_STR-REQ_DATE_H sy-vline
WA_BAPISDHEADX_STR-DATE_TYPE sy-vline
WA_BAPISDHEADX_STR-PURCH_NO sy-vline
WA_BAPISDHEADX_STR-PURCH_DATE sy-vline
WA_BAPISDHEADX_STR-PO_METHOD sy-vline
WA_BAPISDHEADX_STR-PO_SUPPLEM sy-vline
WA_BAPISDHEADX_STR-REF_1 sy-vline
WA_BAPISDHEADX_STR-NAME sy-vline
WA_BAPISDHEADX_STR-TELEPHONE sy-vline
WA_BAPISDHEADX_STR-PRICE_GRP sy-vline
WA_BAPISDHEADX_STR-CUST_GROUP sy-vline
WA_BAPISDHEADX_STR-SALES_DIST sy-vline
WA_BAPISDHEADX_STR-PRICE_LIST sy-vline
WA_BAPISDHEADX_STR-INCOTERMS1 sy-vline
WA_BAPISDHEADX_STR-INCOTERMS2 sy-vline
WA_BAPISDHEADX_STR-PMNTTRMS sy-vline
WA_BAPISDHEADX_STR-DLV_BLOCK sy-vline
WA_BAPISDHEADX_STR-BILL_BLOCK sy-vline
WA_BAPISDHEADX_STR-ORD_REASON sy-vline
WA_BAPISDHEADX_STR-COMPL_DLV sy-vline
WA_BAPISDHEADX_STR-PRICE_DATE sy-vline
WA_BAPISDHEADX_STR-QT_VALID_F sy-vline
WA_BAPISDHEADX_STR-QT_VALID_T sy-vline
WA_BAPISDHEADX_STR-CT_VALID_F sy-vline
WA_BAPISDHEADX_STR-CT_VALID_T sy-vline
WA_BAPISDHEADX_STR-CUST_GRP1 sy-vline
WA_BAPISDHEADX_STR-CUST_GRP2 sy-vline
WA_BAPISDHEADX_STR-CUST_GRP3 sy-vline
WA_BAPISDHEADX_STR-CUST_GRP4 sy-vline
WA_BAPISDHEADX_STR-CUST_GRP5 sy-vline
WA_BAPISDHEADX_STR-PURCH_NO_C sy-vline
WA_BAPISDHEADX_STR-PURCH_NO_S sy-vline
WA_BAPISDHEADX_STR-PO_DAT_S sy-vline
WA_BAPISDHEADX_STR-PO_METH_S sy-vline
WA_BAPISDHEADX_STR-REF_1_S sy-vline
WA_BAPISDHEADX_STR-DOC_DATE sy-vline
WA_BAPISDHEADX_STR-WAR_DATE sy-vline
WA_BAPISDHEADX_STR-SHIP_COND sy-vline
WA_BAPISDHEADX_STR-PP_SEARCH sy-vline
WA_BAPISDHEADX_STR-DUN_COUNT sy-vline
WA_BAPISDHEADX_STR-DUN_DATE sy-vline
WA_BAPISDHEADX_STR-DLVSCHEDUSE sy-vline
WA_BAPISDHEADX_STR-RP_DEL_SCHED sy-vline
WA_BAPISDHEADX_STR-REF_DOC sy-vline
WA_BAPISDHEADX_STR-COMP_CODE_B sy-vline
WA_BAPISDHEADX_STR-ALT_TAX_CLASS sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS2 sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS3 sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS4 sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS5 sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS6 sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS7 sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS8 sy-vline
WA_BAPISDHEADX_STR-TAX_CLASS9 sy-vline
WA_BAPISDHEADX_STR-REF_DOC_L sy-vline
WA_BAPISDHEADX_STR-ASS_NUMBER sy-vline
WA_BAPISDHEADX_STR-REF_DOC_CAT sy-vline
WA_BAPISDHEADX_STR-ORD_COMB_IN sy-vline
WA_BAPISDHEADX_STR-BILL_SCHED sy-vline
WA_BAPISDHEADX_STR-INVO_SCHED sy-vline
WA_BAPISDHEADX_STR-MN_INVOICE sy-vline
WA_BAPISDHEADX_STR-EXCH_RATE_FI sy-vline
WA_BAPISDHEADX_STR-ADD_VAL_DY sy-vline
WA_BAPISDHEADX_STR-FIX_VAL_DY sy-vline
WA_BAPISDHEADX_STR-PYMT_METH sy-vline
WA_BAPISDHEADX_STR-ACCNT_ASGN sy-vline
WA_BAPISDHEADX_STR-EXCHG_RATE sy-vline
WA_BAPISDHEADX_STR-BILL_DATE sy-vline
WA_BAPISDHEADX_STR-SERV_DATE sy-vline
WA_BAPISDHEADX_STR-DUNN_KEY sy-vline
WA_BAPISDHEADX_STR-DUNN_BLOCK sy-vline
WA_BAPISDHEADX_STR-PYMT_GAR_PROC sy-vline
WA_BAPISDHEADX_STR-DEPARTMENT_NO sy-vline
WA_BAPISDHEADX_STR-REC_POINT sy-vline
WA_BAPISDHEADX_STR-PO_ITM_NO_S sy-vline
WA_BAPISDHEADX_STR-DOC_NUM_FI sy-vline
WA_BAPISDHEADX_STR-CUST_COND_GRP1 sy-vline
WA_BAPISDHEADX_STR-CUST_COND_GRP2 sy-vline
WA_BAPISDHEADX_STR-CUST_COND_GRP3 sy-vline
WA_BAPISDHEADX_STR-CUST_COND_GRP4 sy-vline
WA_BAPISDHEADX_STR-CUST_COND_GRP5 sy-vline
WA_BAPISDHEADX_STR-DLV_TIME sy-vline
WA_BAPISDHEADX_STR-CURRENCY sy-vline
WA_BAPISDHEADX_STR-REF_DOC_CAT_LONG sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.