ABAP Select data from SAP table BESLL 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 BESLL 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 BESLL. 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 BESLL 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_BESLL TYPE STANDARD TABLE OF BESLL,
      WA_BESLL TYPE BESLL,
      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: <BESLL> TYPE BESLL.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BESLL
*  INTO TABLE @DATA(IT_BESLL2).
*--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_BESLL INDEX 1 INTO DATA(WA_BESLL2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BESLL ASSIGNING <BESLL>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BESLL>-STYPE = 1.
<BESLL>-TBNAM = 1.
<BESLL>-EXTROW = 1.
<BESLL>-PUSHTEXT = 1.
<BESLL>-DEL = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BESLL-SRVPOS, sy-vline,
WA_BESLL-MATKL, sy-vline,
WA_BESLL-LBNUM, sy-vline,
WA_BESLL-AUSGB, sy-vline,
WA_BESLL-STLVPOS, sy-vline,
WA_BESLL-KTEXT1, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BESLL 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_BESLL 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_BESLL INTO WA_BESLL. *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 SRVPOS CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BESLL-SRVPOS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BESLL-SRVPOS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field MEINS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_BESLL-MEINS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BESLL-MEINS.
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_BESLL_STR,
STYPE TYPE STRING,
TBNAM TYPE STRING,
EXTROW TYPE STRING,
PUSHTEXT TYPE STRING,
DEL TYPE STRING,
SRVPOS TYPE STRING,
MATKL TYPE STRING,
LBNUM TYPE STRING,
AUSGB TYPE STRING,
STLVPOS TYPE STRING,
KTEXT1 TYPE STRING,
PUSHTEXT_L TYPE STRING,
EXTSRVNO TYPE STRING,
PERNR TYPE STRING,
PERSEXT TYPE STRING,
SDATE TYPE STRING,
BEGTIME TYPE STRING,
ENDTIME TYPE STRING,
MENGE TYPE STRING,
MEINS TYPE STRING,
UEBTO TYPE STRING,
UEBTK TYPE STRING,
BRTWR TYPE STRING,
WAERS TYPE STRING,
NETWR TYPE STRING,
PRS_CHG TYPE STRING,
NORMALPOS TYPE STRING,
PAUSCH TYPE STRING,
EVENTUAL TYPE STRING,
FREEQTY TYPE STRING,
INFORM TYPE STRING,
NOALTER TYPE STRING,
BASIC TYPE STRING,
ALTERNAT TYPE STRING,
ALT_EXTROW TYPE STRING,
NOTYPE TYPE STRING,
BIDDER TYPE STRING,
SUPPLE TYPE STRING,
FORMELNR TYPE STRING,
FRMVAL1 TYPE STRING,
FRMVAL2 TYPE STRING,
FRMVAL3 TYPE STRING,
FRMVAL4 TYPE STRING,
FRMVAL5 TYPE STRING,END OF T_EKKO_STR. DATA: WA_BESLL_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_BESLL_STR-STYPE sy-vline
WA_BESLL_STR-TBNAM sy-vline
WA_BESLL_STR-EXTROW sy-vline
WA_BESLL_STR-PUSHTEXT sy-vline
WA_BESLL_STR-DEL sy-vline
WA_BESLL_STR-SRVPOS sy-vline
WA_BESLL_STR-MATKL sy-vline
WA_BESLL_STR-LBNUM sy-vline
WA_BESLL_STR-AUSGB sy-vline
WA_BESLL_STR-STLVPOS sy-vline
WA_BESLL_STR-KTEXT1 sy-vline
WA_BESLL_STR-PUSHTEXT_L sy-vline
WA_BESLL_STR-EXTSRVNO sy-vline
WA_BESLL_STR-PERNR sy-vline
WA_BESLL_STR-PERSEXT sy-vline
WA_BESLL_STR-SDATE sy-vline
WA_BESLL_STR-BEGTIME sy-vline
WA_BESLL_STR-ENDTIME sy-vline
WA_BESLL_STR-MENGE sy-vline
WA_BESLL_STR-MEINS sy-vline
WA_BESLL_STR-UEBTO sy-vline
WA_BESLL_STR-UEBTK sy-vline
WA_BESLL_STR-BRTWR sy-vline
WA_BESLL_STR-WAERS sy-vline
WA_BESLL_STR-NETWR sy-vline
WA_BESLL_STR-PRS_CHG sy-vline
WA_BESLL_STR-NORMALPOS sy-vline
WA_BESLL_STR-PAUSCH sy-vline
WA_BESLL_STR-EVENTUAL sy-vline
WA_BESLL_STR-FREEQTY sy-vline
WA_BESLL_STR-INFORM sy-vline
WA_BESLL_STR-NOALTER sy-vline
WA_BESLL_STR-BASIC sy-vline
WA_BESLL_STR-ALTERNAT sy-vline
WA_BESLL_STR-ALT_EXTROW sy-vline
WA_BESLL_STR-NOTYPE sy-vline
WA_BESLL_STR-BIDDER sy-vline
WA_BESLL_STR-SUPPLE sy-vline
WA_BESLL_STR-FORMELNR sy-vline
WA_BESLL_STR-FRMVAL1 sy-vline
WA_BESLL_STR-FRMVAL2 sy-vline
WA_BESLL_STR-FRMVAL3 sy-vline
WA_BESLL_STR-FRMVAL4 sy-vline
WA_BESLL_STR-FRMVAL5 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.