ABAP Select data from SAP table ABILLOFMATERIAL 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 ABILLOFMATERIAL 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 ABILLOFMATERIAL. 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 ABILLOFMATERIAL 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_ABILLOFMATERIAL TYPE STANDARD TABLE OF ABILLOFMATERIAL,
      WA_ABILLOFMATERIAL TYPE ABILLOFMATERIAL,
      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: <ABILLOFMATERIAL> TYPE ABILLOFMATERIAL.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM ABILLOFMATERIAL
*  INTO TABLE @DATA(IT_ABILLOFMATERIAL2).
*--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_ABILLOFMATERIAL INDEX 1 INTO DATA(WA_ABILLOFMATERIAL2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_ABILLOFMATERIAL ASSIGNING <ABILLOFMATERIAL>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<ABILLOFMATERIAL>-MANDT = 1.
<ABILLOFMATERIAL>-BILLOFMATERIALHEADERUUID = 1.
<ABILLOFMATERIAL>-BILLOFMATERIALVARIANTUSAGE = 1.
<ABILLOFMATERIAL>-BILLOFMATERIALCATEGORY = 1.
<ABILLOFMATERIAL>-BILLOFMATERIAL = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_ABILLOFMATERIAL-BILLOFMATERIALVARIANT, sy-vline,
WA_ABILLOFMATERIAL-MATERIAL, sy-vline,
WA_ABILLOFMATERIAL-PLANT, sy-vline,
WA_ABILLOFMATERIAL-ISMULTIPLEBOMALT, sy-vline,
WA_ABILLOFMATERIAL-BOMHEADERINTERNALCHANGECOUNT, sy-vline,
WA_ABILLOFMATERIAL-BOMUSAGEPRIORITY, sy-vline.
ENDLOOP. *Add any further fields from structure WA_ABILLOFMATERIAL 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_ABILLOFMATERIAL 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_ABILLOFMATERIAL INTO WA_ABILLOFMATERIAL. *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 BILLOFMATERIAL CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_ABILLOFMATERIAL-BILLOFMATERIAL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_ABILLOFMATERIAL-BILLOFMATERIAL.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit MATN1, internal->external for field MATERIAL CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_ABILLOFMATERIAL-MATERIAL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_ABILLOFMATERIAL-MATERIAL.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit CUNIT, internal->external for field BOMHEADERBASEUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_ABILLOFMATERIAL-BOMHEADERBASEUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_ABILLOFMATERIAL-BOMHEADERBASEUNIT.
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_ABILLOFMATERIAL_STR,
MANDT TYPE STRING,
BILLOFMATERIALHEADERUUID TYPE STRING,
BILLOFMATERIALVARIANTUSAGE TYPE STRING,
BILLOFMATERIALCATEGORY TYPE STRING,
BILLOFMATERIAL TYPE STRING,
BILLOFMATERIALVARIANT TYPE STRING,
MATERIAL TYPE STRING,
PLANT TYPE STRING,
ISMULTIPLEBOMALT TYPE STRING,
BOMHEADERINTERNALCHANGECOUNT TYPE STRING,
BOMUSAGEPRIORITY TYPE STRING,
BILLOFMATERIALAUTHSNGRP TYPE STRING,
BILLOFMATERIALVERSION TYPE STRING,
BOMVERSIONSTATUS TYPE STRING,
ISVERSIONBILLOFMATERIAL TYPE STRING,
ISLATESTBOMVERSION TYPE STRING,
ISCONFIGUREDMATERIAL TYPE STRING,
BOMTECHNICALTYPE TYPE STRING,
BOMGROUP TYPE STRING,
BOMHEADERTEXT TYPE STRING,
BOMALTERNATIVETEXT TYPE STRING,
BILLOFMATERIALSTATUS TYPE STRING,
HEADERVALIDITYSTARTDATE TYPE STRING,
HEADERVALIDITYENDDATE TYPE STRING,
ENGINEERINGCHANGEDOCUMENT TYPE STRING,
ENGINEERINGCHANGEDOCFOREDIT TYPE STRING,
CHGTOENGINEERINGCHGDOCUMENT TYPE STRING,
ISMARKEDFORDELETION TYPE STRING,
ISALE TYPE STRING,
BOMHEADERBASEUNIT TYPE STRING,
BOMHEADERQUANTITYINBASEUNIT TYPE STRING,
RECORDCREATIONDATE TYPE STRING,
LASTCHANGEDATE TYPE STRING,
CREATEDBYUSER TYPE STRING,
LASTCHANGEDBYUSER TYPE STRING,
BOMISTOBEDELETED TYPE STRING,
DOCUMENTISCREATEDBYCAD TYPE STRING,
LABORATORYORDESIGNOFFICE TYPE STRING,
SELECTEDBILLOFMATERIALVERSION TYPE STRING,END OF T_EKKO_STR. DATA: WA_ABILLOFMATERIAL_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_ABILLOFMATERIAL_STR-MANDT sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIALHEADERUUID sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIALVARIANTUSAGE sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIALCATEGORY sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIAL sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIALVARIANT sy-vline
WA_ABILLOFMATERIAL_STR-MATERIAL sy-vline
WA_ABILLOFMATERIAL_STR-PLANT sy-vline
WA_ABILLOFMATERIAL_STR-ISMULTIPLEBOMALT sy-vline
WA_ABILLOFMATERIAL_STR-BOMHEADERINTERNALCHANGECOUNT sy-vline
WA_ABILLOFMATERIAL_STR-BOMUSAGEPRIORITY sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIALAUTHSNGRP sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIALVERSION sy-vline
WA_ABILLOFMATERIAL_STR-BOMVERSIONSTATUS sy-vline
WA_ABILLOFMATERIAL_STR-ISVERSIONBILLOFMATERIAL sy-vline
WA_ABILLOFMATERIAL_STR-ISLATESTBOMVERSION sy-vline
WA_ABILLOFMATERIAL_STR-ISCONFIGUREDMATERIAL sy-vline
WA_ABILLOFMATERIAL_STR-BOMTECHNICALTYPE sy-vline
WA_ABILLOFMATERIAL_STR-BOMGROUP sy-vline
WA_ABILLOFMATERIAL_STR-BOMHEADERTEXT sy-vline
WA_ABILLOFMATERIAL_STR-BOMALTERNATIVETEXT sy-vline
WA_ABILLOFMATERIAL_STR-BILLOFMATERIALSTATUS sy-vline
WA_ABILLOFMATERIAL_STR-HEADERVALIDITYSTARTDATE sy-vline
WA_ABILLOFMATERIAL_STR-HEADERVALIDITYENDDATE sy-vline
WA_ABILLOFMATERIAL_STR-ENGINEERINGCHANGEDOCUMENT sy-vline
WA_ABILLOFMATERIAL_STR-ENGINEERINGCHANGEDOCFOREDIT sy-vline
WA_ABILLOFMATERIAL_STR-CHGTOENGINEERINGCHGDOCUMENT sy-vline
WA_ABILLOFMATERIAL_STR-ISMARKEDFORDELETION sy-vline
WA_ABILLOFMATERIAL_STR-ISALE sy-vline
WA_ABILLOFMATERIAL_STR-BOMHEADERBASEUNIT sy-vline
WA_ABILLOFMATERIAL_STR-BOMHEADERQUANTITYINBASEUNIT sy-vline
WA_ABILLOFMATERIAL_STR-RECORDCREATIONDATE sy-vline
WA_ABILLOFMATERIAL_STR-LASTCHANGEDATE sy-vline
WA_ABILLOFMATERIAL_STR-CREATEDBYUSER sy-vline
WA_ABILLOFMATERIAL_STR-LASTCHANGEDBYUSER sy-vline
WA_ABILLOFMATERIAL_STR-BOMISTOBEDELETED sy-vline
WA_ABILLOFMATERIAL_STR-DOCUMENTISCREATEDBYCAD sy-vline
WA_ABILLOFMATERIAL_STR-LABORATORYORDESIGNOFFICE sy-vline
WA_ABILLOFMATERIAL_STR-SELECTEDBILLOFMATERIALVERSION sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.