ABAP Select data from SAP table HEADER_BOM 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 HEADER_BOM 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 HEADER_BOM. 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 HEADER_BOM 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_HEADER_BOM TYPE STANDARD TABLE OF HEADER_BOM,
      WA_HEADER_BOM TYPE HEADER_BOM,
      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: <HEADER_BOM> TYPE HEADER_BOM.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM HEADER_BOM
*  INTO TABLE @DATA(IT_HEADER_BOM2).
*--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_HEADER_BOM INDEX 1 INTO DATA(WA_HEADER_BOM2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_HEADER_BOM ASSIGNING <HEADER_BOM>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<HEADER_BOM>-MANDT = 1.
<HEADER_BOM>-STLTY = 1.
<HEADER_BOM>-STLNR = 1.
<HEADER_BOM>-STLAL = 1.
<HEADER_BOM>-STKOZ = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_HEADER_BOM-DATUV, sy-vline,
WA_HEADER_BOM-TECHV, sy-vline,
WA_HEADER_BOM-AENNR, sy-vline,
WA_HEADER_BOM-LKENZ, sy-vline,
WA_HEADER_BOM-LOEKZ, sy-vline,
WA_HEADER_BOM-VGKZL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_HEADER_BOM 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_HEADER_BOM 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_HEADER_BOM INTO WA_HEADER_BOM. *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 NUMCV, internal->external for field STLNR CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_HEADER_BOM-STLNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_HEADER_BOM-STLNR.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit CUNIT, internal->external for field BMEIN CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_HEADER_BOM-BMEIN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_HEADER_BOM-BMEIN.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ALPHA, internal->external for field AENRA CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_HEADER_BOM-AENRA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_HEADER_BOM-AENRA.
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_HEADER_BOM_STR,
MANDT TYPE STRING,
STLTY TYPE STRING,
STLNR TYPE STRING,
STLAL TYPE STRING,
STKOZ TYPE STRING,
DATUV TYPE STRING,
TECHV TYPE STRING,
AENNR TYPE STRING,
LKENZ TYPE STRING,
LOEKZ TYPE STRING,
VGKZL TYPE STRING,
ANDAT TYPE STRING,
ANNAM TYPE STRING,
AEDAT TYPE STRING,
AENAM TYPE STRING,
BMEIN TYPE STRING,
BMENG TYPE STRING,
CADKZ TYPE STRING,
LABOR TYPE STRING,
LTXSP TYPE STRING,
STKTX TYPE STRING,
STLST TYPE STRING,
WRKAN TYPE STRING,
DVDAT TYPE STRING,
DVNAM TYPE STRING,
AEHLP TYPE STRING,
ALEKZ TYPE STRING,
GUIDX TYPE STRING,
VALID_TO TYPE STRING,
ECN_TO TYPE STRING,
BOM_VERSN TYPE STRING,
VERSNST TYPE STRING,
VERSNLASTIND TYPE STRING,
LASTCHANGEDATETIME TYPE STRING,
BOM_AIN_IND TYPE STRING,
BOM_PREV_VERSN TYPE STRING,
DUMMY_STKO_INCL_EEW_PS TYPE STRING,
LTXPS TYPE STRING,
LTXVB TYPE STRING,
VBKZ TYPE STRING,
MVBKZ TYPE STRING,
DATUB TYPE STRING,
AENRA TYPE STRING,
AENKZ TYPE STRING,
SELKZ TYPE STRING,
KZDOC TYPE STRING,
EFRNK TYPE STRING,
CAPID TYPE STRING,END OF T_EKKO_STR. DATA: WA_HEADER_BOM_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_HEADER_BOM_STR-MANDT sy-vline
WA_HEADER_BOM_STR-STLTY sy-vline
WA_HEADER_BOM_STR-STLNR sy-vline
WA_HEADER_BOM_STR-STLAL sy-vline
WA_HEADER_BOM_STR-STKOZ sy-vline
WA_HEADER_BOM_STR-DATUV sy-vline
WA_HEADER_BOM_STR-TECHV sy-vline
WA_HEADER_BOM_STR-AENNR sy-vline
WA_HEADER_BOM_STR-LKENZ sy-vline
WA_HEADER_BOM_STR-LOEKZ sy-vline
WA_HEADER_BOM_STR-VGKZL sy-vline
WA_HEADER_BOM_STR-ANDAT sy-vline
WA_HEADER_BOM_STR-ANNAM sy-vline
WA_HEADER_BOM_STR-AEDAT sy-vline
WA_HEADER_BOM_STR-AENAM sy-vline
WA_HEADER_BOM_STR-BMEIN sy-vline
WA_HEADER_BOM_STR-BMENG sy-vline
WA_HEADER_BOM_STR-CADKZ sy-vline
WA_HEADER_BOM_STR-LABOR sy-vline
WA_HEADER_BOM_STR-LTXSP sy-vline
WA_HEADER_BOM_STR-STKTX sy-vline
WA_HEADER_BOM_STR-STLST sy-vline
WA_HEADER_BOM_STR-WRKAN sy-vline
WA_HEADER_BOM_STR-DVDAT sy-vline
WA_HEADER_BOM_STR-DVNAM sy-vline
WA_HEADER_BOM_STR-AEHLP sy-vline
WA_HEADER_BOM_STR-ALEKZ sy-vline
WA_HEADER_BOM_STR-GUIDX sy-vline
WA_HEADER_BOM_STR-VALID_TO sy-vline
WA_HEADER_BOM_STR-ECN_TO sy-vline
WA_HEADER_BOM_STR-BOM_VERSN sy-vline
WA_HEADER_BOM_STR-VERSNST sy-vline
WA_HEADER_BOM_STR-VERSNLASTIND sy-vline
WA_HEADER_BOM_STR-LASTCHANGEDATETIME sy-vline
WA_HEADER_BOM_STR-BOM_AIN_IND sy-vline
WA_HEADER_BOM_STR-BOM_PREV_VERSN sy-vline
WA_HEADER_BOM_STR-DUMMY_STKO_INCL_EEW_PS sy-vline
WA_HEADER_BOM_STR-LTXPS sy-vline
WA_HEADER_BOM_STR-LTXVB sy-vline
WA_HEADER_BOM_STR-VBKZ sy-vline
WA_HEADER_BOM_STR-MVBKZ sy-vline
WA_HEADER_BOM_STR-DATUB sy-vline
WA_HEADER_BOM_STR-AENRA sy-vline
WA_HEADER_BOM_STR-AENKZ sy-vline
WA_HEADER_BOM_STR-SELKZ sy-vline
WA_HEADER_BOM_STR-KZDOC sy-vline
WA_HEADER_BOM_STR-EFRNK sy-vline
WA_HEADER_BOM_STR-CAPID sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.