ABAP Select data from SAP table BAPICSHEADO 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 BAPICSHEADO 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 BAPICSHEADO. 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 BAPICSHEADO 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_BAPICSHEADO TYPE STANDARD TABLE OF BAPICSHEADO,
      WA_BAPICSHEADO TYPE BAPICSHEADO,
      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: <BAPICSHEADO> TYPE BAPICSHEADO.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPICSHEADO
*  INTO TABLE @DATA(IT_BAPICSHEADO2).
*--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_BAPICSHEADO INDEX 1 INTO DATA(WA_BAPICSHEADO2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPICSHEADO ASSIGNING <BAPICSHEADO>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPICSHEADO>-DOCUMENT_NUMBER = 1.
<BAPICSHEADO>-BILL_TYPE = 1.
<BAPICSHEADO>-BILL_DATE = 1.
<BAPICSHEADO>-BILL_TO = 1.
<BAPICSHEADO>-PAYER = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPICSHEADO-SALESORG, sy-vline,
WA_BAPICSHEADO-DISTR_CHAN, sy-vline,
WA_BAPICSHEADO-DIVISION, sy-vline,
WA_BAPICSHEADO-SALES_OFF, sy-vline,
WA_BAPICSHEADO-SALES_GRP, sy-vline,
WA_BAPICSHEADO-COMP_CODE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPICSHEADO 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_BAPICSHEADO 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_BAPICSHEADO INTO WA_BAPICSHEADO. *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 DOCUMENT_NUMBER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPICSHEADO-DOCUMENT_NUMBER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPICSHEADO-DOCUMENT_NUMBER.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit ALPHA, internal->external for field CANCELLED_DOC CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPICSHEADO-CANCELLED_DOC IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPICSHEADO-CANCELLED_DOC.
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_BAPICSHEADO_STR,
DOCUMENT_NUMBER TYPE STRING,
BILL_TYPE TYPE STRING,
BILL_DATE TYPE STRING,
BILL_TO TYPE STRING,
PAYER TYPE STRING,
SALESORG TYPE STRING,
DISTR_CHAN TYPE STRING,
DIVISION TYPE STRING,
SALES_OFF TYPE STRING,
SALES_GRP TYPE STRING,
COMP_CODE TYPE STRING,
ACCTSTATUS TYPE STRING,
CURRENCY TYPE STRING,
CURRENCY_ISO TYPE STRING,
DOC_DATE TYPE STRING,
REF_DOC_NO TYPE STRING,
PAYMT_REF TYPE STRING,
ALLOC_NMBR TYPE STRING,
GROSS_AMNT TYPE STRING,
EXCH_RATE TYPE STRING,
EXCH_RATE_V TYPE STRING,
EX_RATE_FX TYPE STRING,
TRANS_DATE TYPE STRING,
PMNTTRMS_DEB TYPE STRING,
DSCNT1_TO_DEB TYPE STRING,
DSCT_PCT1_DEB TYPE STRING,
DSCNT2_TO_DEB TYPE STRING,
DSCT_PCT2_DEB TYPE STRING,
DSCNT3_TO_DEB TYPE STRING,
ADD_VAL_DY_DEB TYPE STRING,
FIX_VAL_DY_DEB TYPE STRING,
LOGSYSTEM TYPE STRING,
REF_DOC_NO_LONG TYPE STRING,
PMNT_METH_DEB TYPE STRING,
INV_LIST_DATE TYPE STRING,
BILL_REASON TYPE STRING,
SETTLEMENT_TYPE TYPE STRING,
CANCELLED_DOC TYPE STRING,
BILL_CATEGORY TYPE STRING,
IDO_TYPE_CUST TYPE STRING,
ID_OBJECT_CUST TYPE STRING,
VAT_REG_NO TYPE STRING,
TAX_DEST_CTY TYPE STRING,
INCOTERMS1 TYPE STRING,
INCOTERMS2 TYPE STRING,
INCOTERMSV TYPE STRING,
INCOTERMS2L TYPE STRING,
INCOTERMS3L TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPICSHEADO_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_BAPICSHEADO_STR-DOCUMENT_NUMBER sy-vline
WA_BAPICSHEADO_STR-BILL_TYPE sy-vline
WA_BAPICSHEADO_STR-BILL_DATE sy-vline
WA_BAPICSHEADO_STR-BILL_TO sy-vline
WA_BAPICSHEADO_STR-PAYER sy-vline
WA_BAPICSHEADO_STR-SALESORG sy-vline
WA_BAPICSHEADO_STR-DISTR_CHAN sy-vline
WA_BAPICSHEADO_STR-DIVISION sy-vline
WA_BAPICSHEADO_STR-SALES_OFF sy-vline
WA_BAPICSHEADO_STR-SALES_GRP sy-vline
WA_BAPICSHEADO_STR-COMP_CODE sy-vline
WA_BAPICSHEADO_STR-ACCTSTATUS sy-vline
WA_BAPICSHEADO_STR-CURRENCY sy-vline
WA_BAPICSHEADO_STR-CURRENCY_ISO sy-vline
WA_BAPICSHEADO_STR-DOC_DATE sy-vline
WA_BAPICSHEADO_STR-REF_DOC_NO sy-vline
WA_BAPICSHEADO_STR-PAYMT_REF sy-vline
WA_BAPICSHEADO_STR-ALLOC_NMBR sy-vline
WA_BAPICSHEADO_STR-GROSS_AMNT sy-vline
WA_BAPICSHEADO_STR-EXCH_RATE sy-vline
WA_BAPICSHEADO_STR-EXCH_RATE_V sy-vline
WA_BAPICSHEADO_STR-EX_RATE_FX sy-vline
WA_BAPICSHEADO_STR-TRANS_DATE sy-vline
WA_BAPICSHEADO_STR-PMNTTRMS_DEB sy-vline
WA_BAPICSHEADO_STR-DSCNT1_TO_DEB sy-vline
WA_BAPICSHEADO_STR-DSCT_PCT1_DEB sy-vline
WA_BAPICSHEADO_STR-DSCNT2_TO_DEB sy-vline
WA_BAPICSHEADO_STR-DSCT_PCT2_DEB sy-vline
WA_BAPICSHEADO_STR-DSCNT3_TO_DEB sy-vline
WA_BAPICSHEADO_STR-ADD_VAL_DY_DEB sy-vline
WA_BAPICSHEADO_STR-FIX_VAL_DY_DEB sy-vline
WA_BAPICSHEADO_STR-LOGSYSTEM sy-vline
WA_BAPICSHEADO_STR-REF_DOC_NO_LONG sy-vline
WA_BAPICSHEADO_STR-PMNT_METH_DEB sy-vline
WA_BAPICSHEADO_STR-INV_LIST_DATE sy-vline
WA_BAPICSHEADO_STR-BILL_REASON sy-vline
WA_BAPICSHEADO_STR-SETTLEMENT_TYPE sy-vline
WA_BAPICSHEADO_STR-CANCELLED_DOC sy-vline
WA_BAPICSHEADO_STR-BILL_CATEGORY sy-vline
WA_BAPICSHEADO_STR-IDO_TYPE_CUST sy-vline
WA_BAPICSHEADO_STR-ID_OBJECT_CUST sy-vline
WA_BAPICSHEADO_STR-VAT_REG_NO sy-vline
WA_BAPICSHEADO_STR-TAX_DEST_CTY sy-vline
WA_BAPICSHEADO_STR-INCOTERMS1 sy-vline
WA_BAPICSHEADO_STR-INCOTERMS2 sy-vline
WA_BAPICSHEADO_STR-INCOTERMSV sy-vline
WA_BAPICSHEADO_STR-INCOTERMS2L sy-vline
WA_BAPICSHEADO_STR-INCOTERMS3L sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.