ABAP Select data from SAP table MBCKSM13S_ALV 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 MBCKSM13S_ALV 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 MBCKSM13S_ALV. 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 MBCKSM13S_ALV 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_MBCKSM13S_ALV TYPE STANDARD TABLE OF MBCKSM13S_ALV,
      WA_MBCKSM13S_ALV TYPE MBCKSM13S_ALV,
      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: <MBCKSM13S_ALV> TYPE MBCKSM13S_ALV.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM MBCKSM13S_ALV
*  INTO TABLE @DATA(IT_MBCKSM13S_ALV2).
*--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_MBCKSM13S_ALV INDEX 1 INTO DATA(WA_MBCKSM13S_ALV2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_MBCKSM13S_ALV ASSIGNING <MBCKSM13S_ALV>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<MBCKSM13S_ALV>-MBLNR = 1.
<MBCKSM13S_ALV>-MJAHR = 1.
<MBCKSM13S_ALV>-VGART = 1.
<MBCKSM13S_ALV>-BLART = 1.
<MBCKSM13S_ALV>-BLAUM = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_MBCKSM13S_ALV-BLDAT, sy-vline,
WA_MBCKSM13S_ALV-BUDAT, sy-vline,
WA_MBCKSM13S_ALV-CPUDT, sy-vline,
WA_MBCKSM13S_ALV-CPUTM, sy-vline,
WA_MBCKSM13S_ALV-USNAM, sy-vline,
WA_MBCKSM13S_ALV-XBLNR, sy-vline.
ENDLOOP. *Add any further fields from structure WA_MBCKSM13S_ALV 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_MBCKSM13S_ALV 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_MBCKSM13S_ALV INTO WA_MBCKSM13S_ALV. *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 MBLNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-MBLNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-MBLNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field MJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-MJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-MJAHR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

*Conversion exit GJAHR, internal->external for field SJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-SJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-SJAHR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

*Conversion exit ABPSP, internal->external for field PS_PSP_PNR CALL FUNCTION 'CONVERSION_EXIT_ABPSP_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-PS_PSP_PNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-PS_PSP_PNR.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ABPSP, internal->external for field MAT_PSPNR CALL FUNCTION 'CONVERSION_EXIT_ABPSP_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-MAT_PSPNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-MAT_PSPNR.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit GJAHR, internal->external for field GJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-GJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-GJAHR.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit GJAHR, internal->external for field LFBJA CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-LFBJA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-LFBJA.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ALPHA, internal->external for field ANLN2 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MBCKSM13S_ALV-ANLN2 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MBCKSM13S_ALV-ANLN2.
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_MBCKSM13S_ALV_STR,
MBLNR TYPE STRING,
MJAHR TYPE STRING,
VGART TYPE STRING,
BLART TYPE STRING,
BLAUM TYPE STRING,
BLDAT TYPE STRING,
BUDAT TYPE STRING,
CPUDT TYPE STRING,
CPUTM TYPE STRING,
USNAM TYPE STRING,
XBLNR TYPE STRING,
TCODE2 TYPE STRING,
ZEILE TYPE STRING,
LINE_ID TYPE STRING,
PARENT_ID TYPE STRING,
BWART TYPE STRING,
BUKRS TYPE STRING,
XAUTO TYPE STRING,
MATNR TYPE STRING,
WERKS TYPE STRING,
LGORT TYPE STRING,
CHARG TYPE STRING,
INSMK TYPE STRING,
SOBKZ TYPE STRING,
LIFNR TYPE STRING,
KUNNR TYPE STRING,
KDAUF TYPE STRING,
KDPOS TYPE STRING,
SHKZG TYPE STRING,
WAERS TYPE STRING,
DMBTR TYPE STRING,
BWTAR TYPE STRING,
MENGE TYPE STRING,
EBELN TYPE STRING,
EBELP TYPE STRING,
SJAHR TYPE STRING,
SMBLN TYPE STRING,
ELIKZ TYPE STRING,
EQUNR TYPE STRING,
KOSTL TYPE STRING,
PROJN TYPE STRING,
AUFNR TYPE STRING,
UMMAT TYPE STRING,
UMWRK TYPE STRING,
UMLGO TYPE STRING,
UMCHA TYPE STRING,
UMBAR TYPE STRING,
UMSOK TYPE STRING,
KZBEW TYPE STRING,
KZVBR TYPE STRING,
KZZUG TYPE STRING,
WEUNB TYPE STRING,
PS_PSP_PNR TYPE STRING,
AUFPL TYPE STRING,
APLZL TYPE STRING,
EXBWR TYPE STRING,
VKWRT TYPE STRING,
EXVKW TYPE STRING,
BUSTM TYPE STRING,
BUSTW TYPE STRING,
MENGU TYPE STRING,
WERTU TYPE STRING,
LBKUM TYPE STRING,
SALK3 TYPE STRING,
KZBWS TYPE STRING,
MAT_KDAUF TYPE STRING,
MAT_KDPOS TYPE STRING,
MAT_PSPNR TYPE STRING,
VFDAT TYPE STRING,
HSDAT TYPE STRING,
BELNR TYPE STRING,
AWKEY TYPE STRING,
GJAHR TYPE STRING,
SMBLP TYPE STRING,
MEINS TYPE STRING,
ERFMG TYPE STRING,
ERFME TYPE STRING,
BPMNG TYPE STRING,
BPRME TYPE STRING,
LFBJA TYPE STRING,
LFBNR TYPE STRING,
LFPOS TYPE STRING,
ANLN1 TYPE STRING,
ANLN2 TYPE STRING,
RSNUM TYPE STRING,
RSPOS TYPE STRING,END OF T_EKKO_STR. DATA: WA_MBCKSM13S_ALV_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_MBCKSM13S_ALV_STR-MBLNR sy-vline
WA_MBCKSM13S_ALV_STR-MJAHR sy-vline
WA_MBCKSM13S_ALV_STR-VGART sy-vline
WA_MBCKSM13S_ALV_STR-BLART sy-vline
WA_MBCKSM13S_ALV_STR-BLAUM sy-vline
WA_MBCKSM13S_ALV_STR-BLDAT sy-vline
WA_MBCKSM13S_ALV_STR-BUDAT sy-vline
WA_MBCKSM13S_ALV_STR-CPUDT sy-vline
WA_MBCKSM13S_ALV_STR-CPUTM sy-vline
WA_MBCKSM13S_ALV_STR-USNAM sy-vline
WA_MBCKSM13S_ALV_STR-XBLNR sy-vline
WA_MBCKSM13S_ALV_STR-TCODE2 sy-vline
WA_MBCKSM13S_ALV_STR-ZEILE sy-vline
WA_MBCKSM13S_ALV_STR-LINE_ID sy-vline
WA_MBCKSM13S_ALV_STR-PARENT_ID sy-vline
WA_MBCKSM13S_ALV_STR-BWART sy-vline
WA_MBCKSM13S_ALV_STR-BUKRS sy-vline
WA_MBCKSM13S_ALV_STR-XAUTO sy-vline
WA_MBCKSM13S_ALV_STR-MATNR sy-vline
WA_MBCKSM13S_ALV_STR-WERKS sy-vline
WA_MBCKSM13S_ALV_STR-LGORT sy-vline
WA_MBCKSM13S_ALV_STR-CHARG sy-vline
WA_MBCKSM13S_ALV_STR-INSMK sy-vline
WA_MBCKSM13S_ALV_STR-SOBKZ sy-vline
WA_MBCKSM13S_ALV_STR-LIFNR sy-vline
WA_MBCKSM13S_ALV_STR-KUNNR sy-vline
WA_MBCKSM13S_ALV_STR-KDAUF sy-vline
WA_MBCKSM13S_ALV_STR-KDPOS sy-vline
WA_MBCKSM13S_ALV_STR-SHKZG sy-vline
WA_MBCKSM13S_ALV_STR-WAERS sy-vline
WA_MBCKSM13S_ALV_STR-DMBTR sy-vline
WA_MBCKSM13S_ALV_STR-BWTAR sy-vline
WA_MBCKSM13S_ALV_STR-MENGE sy-vline
WA_MBCKSM13S_ALV_STR-EBELN sy-vline
WA_MBCKSM13S_ALV_STR-EBELP sy-vline
WA_MBCKSM13S_ALV_STR-SJAHR sy-vline
WA_MBCKSM13S_ALV_STR-SMBLN sy-vline
WA_MBCKSM13S_ALV_STR-ELIKZ sy-vline
WA_MBCKSM13S_ALV_STR-EQUNR sy-vline
WA_MBCKSM13S_ALV_STR-KOSTL sy-vline
WA_MBCKSM13S_ALV_STR-PROJN sy-vline
WA_MBCKSM13S_ALV_STR-AUFNR sy-vline
WA_MBCKSM13S_ALV_STR-UMMAT sy-vline
WA_MBCKSM13S_ALV_STR-UMWRK sy-vline
WA_MBCKSM13S_ALV_STR-UMLGO sy-vline
WA_MBCKSM13S_ALV_STR-UMCHA sy-vline
WA_MBCKSM13S_ALV_STR-UMBAR sy-vline
WA_MBCKSM13S_ALV_STR-UMSOK sy-vline
WA_MBCKSM13S_ALV_STR-KZBEW sy-vline
WA_MBCKSM13S_ALV_STR-KZVBR sy-vline
WA_MBCKSM13S_ALV_STR-KZZUG sy-vline
WA_MBCKSM13S_ALV_STR-WEUNB sy-vline
WA_MBCKSM13S_ALV_STR-PS_PSP_PNR sy-vline
WA_MBCKSM13S_ALV_STR-AUFPL sy-vline
WA_MBCKSM13S_ALV_STR-APLZL sy-vline
WA_MBCKSM13S_ALV_STR-EXBWR sy-vline
WA_MBCKSM13S_ALV_STR-VKWRT sy-vline
WA_MBCKSM13S_ALV_STR-EXVKW sy-vline
WA_MBCKSM13S_ALV_STR-BUSTM sy-vline
WA_MBCKSM13S_ALV_STR-BUSTW sy-vline
WA_MBCKSM13S_ALV_STR-MENGU sy-vline
WA_MBCKSM13S_ALV_STR-WERTU sy-vline
WA_MBCKSM13S_ALV_STR-LBKUM sy-vline
WA_MBCKSM13S_ALV_STR-SALK3 sy-vline
WA_MBCKSM13S_ALV_STR-KZBWS sy-vline
WA_MBCKSM13S_ALV_STR-MAT_KDAUF sy-vline
WA_MBCKSM13S_ALV_STR-MAT_KDPOS sy-vline
WA_MBCKSM13S_ALV_STR-MAT_PSPNR sy-vline
WA_MBCKSM13S_ALV_STR-VFDAT sy-vline
WA_MBCKSM13S_ALV_STR-HSDAT sy-vline
WA_MBCKSM13S_ALV_STR-BELNR sy-vline
WA_MBCKSM13S_ALV_STR-AWKEY sy-vline
WA_MBCKSM13S_ALV_STR-GJAHR sy-vline
WA_MBCKSM13S_ALV_STR-SMBLP sy-vline
WA_MBCKSM13S_ALV_STR-MEINS sy-vline
WA_MBCKSM13S_ALV_STR-ERFMG sy-vline
WA_MBCKSM13S_ALV_STR-ERFME sy-vline
WA_MBCKSM13S_ALV_STR-BPMNG sy-vline
WA_MBCKSM13S_ALV_STR-BPRME sy-vline
WA_MBCKSM13S_ALV_STR-LFBJA sy-vline
WA_MBCKSM13S_ALV_STR-LFBNR sy-vline
WA_MBCKSM13S_ALV_STR-LFPOS sy-vline
WA_MBCKSM13S_ALV_STR-ANLN1 sy-vline
WA_MBCKSM13S_ALV_STR-ANLN2 sy-vline
WA_MBCKSM13S_ALV_STR-RSNUM sy-vline
WA_MBCKSM13S_ALV_STR-RSPOS sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.