ABAP Select data from SAP table MCJHAGVZB 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 MCJHAGVZB 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 MCJHAGVZB. 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 MCJHAGVZB 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_MCJHAGVZB TYPE STANDARD TABLE OF MCJHAGVZB,
      WA_MCJHAGVZB TYPE MCJHAGVZB,
      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: <MCJHAGVZB> TYPE MCJHAGVZB.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM MCJHAGVZB
*  INTO TABLE @DATA(IT_MCJHAGVZB2).
*--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_MCJHAGVZB INDEX 1 INTO DATA(WA_MCJHAGVZB2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_MCJHAGVZB ASSIGNING <MCJHAGVZB>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<MCJHAGVZB>-SUPKZ = 1.
<MCJHAGVZB>-MANDT = 1.
<MCJHAGVZB>-AVM_NR = 1.
<MCJHAGVZB>-POS_NR = 1.
<MCJHAGVZB>-GRUPPABRNR = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_MCJHAGVZB-VNR, sy-vline,
WA_MCJHAGVZB-BEDNR, sy-vline,
WA_MCJHAGVZB-PROVTYP, sy-vline,
WA_MCJHAGVZB-VZUONR, sy-vline,
WA_MCJHAGVZB-VERM, sy-vline,
WA_MCJHAGVZB-XAUTO, sy-vline.
ENDLOOP. *Add any further fields from structure WA_MCJHAGVZB 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_MCJHAGVZB 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_MCJHAGVZB INTO WA_MCJHAGVZB. *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 AVM_NR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MCJHAGVZB-AVM_NR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MCJHAGVZB-AVM_NR.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit VERMK, internal->external for field VERM CALL FUNCTION 'CONVERSION_EXIT_VERMK_OUTPUT' EXPORTING input = WA_MCJHAGVZB-VERM IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MCJHAGVZB-VERM.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field J_BEINH_BA CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MCJHAGVZB-J_BEINH_BA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MCJHAGVZB-J_BEINH_BA.
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_MCJHAGVZB_STR,
SUPKZ TYPE STRING,
MANDT TYPE STRING,
AVM_NR TYPE STRING,
POS_NR TYPE STRING,
GRUPPABRNR TYPE STRING,
VNR TYPE STRING,
BEDNR TYPE STRING,
PROVTYP TYPE STRING,
VZUONR TYPE STRING,
VERM TYPE STRING,
XAUTO TYPE STRING,
XFOUND TYPE STRING,
XMANU TYPE STRING,
XKOPF_ZUO TYPE STRING,
XPOS_ZUO TYPE STRING,
PR_BASIS TYPE STRING,
PR_BETRAG TYPE STRING,
PRA_XMANU TYPE STRING,
PROSL TYPE STRING,
PROV_SATZ TYPE STRING,
PROVM_SATZ TYPE STRING,
PROVM_WAER TYPE STRING,
PROV_PEIN TYPE STRING,
PROV_MEIN TYPE STRING,
BEZKOND TYPE STRING,
VZMANUAEND TYPE STRING,
BEDGR TYPE STRING,
JVERMART TYPE STRING,
VANR TYPE STRING,
REF_VZUONR TYPE STRING,
SUC_VZUONR TYPE STRING,
CORRCAUSE TYPE STRING,
CANCELCC TYPE STRING,
STATUSK TYPE STRING,
XPROV_EE TYPE STRING,
XUVVZ TYPE STRING,
XZUOINACT TYPE STRING,
XDELETED TYPE STRING,
MERKMAL0 TYPE STRING,
MERKMAL1 TYPE STRING,
MERKMAL2 TYPE STRING,
MERKMAL3 TYPE STRING,
MERKMAL4 TYPE STRING,
MERKMAL5 TYPE STRING,
MERKMAL6 TYPE STRING,
MERKMAL7 TYPE STRING,
MERKMAL8 TYPE STRING,
MERKMAL9 TYPE STRING,
VZ_UMBRT TYPE STRING,
VZ_UMNET TYPE STRING,
VZ_KZWI1 TYPE STRING,
VZ_KZWI2 TYPE STRING,
VZ_KZWI3 TYPE STRING,
VZ_KZWI4 TYPE STRING,
VZ_KZWI5 TYPE STRING,
VZ_KZWI6 TYPE STRING,
VZ_GUBRT TYPE STRING,
VZ_GUNET TYPE STRING,
VZ_KZWI1G TYPE STRING,
VZ_KZWI2G TYPE STRING,
VZ_KZWI3G TYPE STRING,
VZ_KZWI4G TYPE STRING,
VZ_KZWI5G TYPE STRING,
VZ_KZWI6G TYPE STRING,
VZA_UMBRT TYPE STRING,
VZA_UMNET TYPE STRING,
VZA_KZWI1 TYPE STRING,
VZA_KZWI2 TYPE STRING,
VZA_KZWI3 TYPE STRING,
VZA_KZWI4 TYPE STRING,
VZA_KZWI5 TYPE STRING,
VZA_KZWI6 TYPE STRING,
VZA_GUBRT TYPE STRING,
VZA_GUNET TYPE STRING,
VZA_KZWI1G TYPE STRING,
VZA_KZWI2G TYPE STRING,
VZA_KZWI3G TYPE STRING,
VZA_KZWI4G TYPE STRING,
VZA_KZWI5G TYPE STRING,
VZA_KZWI6G TYPE STRING,
J_FAKDAT TYPE STRING,
J_KUNAG TYPE STRING,
J_KUNRG TYPE STRING,
J_INSERENT TYPE STRING,
J_VKORG TYPE STRING,
J_VTWEG TYPE STRING,
J_SPART TYPE STRING,
J_BEINH TYPE STRING,
J_BEINH_BA TYPE STRING,
VF_UMBRT TYPE STRING,
VF_UMNET TYPE STRING,
VF_KZWI1 TYPE STRING,
VF_KZWI2 TYPE STRING,
VF_KZWI3 TYPE STRING,
VF_KZWI4 TYPE STRING,
VF_KZWI5 TYPE STRING,
VF_KZWI6 TYPE STRING,
VF_GUBRT TYPE STRING,
VF_GUNET TYPE STRING,
VF_KZWI1G TYPE STRING,
VF_KZWI2G TYPE STRING,
VF_KZWI3G TYPE STRING,
VF_KZWI4G TYPE STRING,
VF_KZWI5G TYPE STRING,
VF_KZWI6G TYPE STRING,
VFA_UMBRT TYPE STRING,
VFA_UMNET TYPE STRING,
VFA_KZWI1 TYPE STRING,
VFA_KZWI2 TYPE STRING,
VFA_KZWI3 TYPE STRING,
VFA_KZWI4 TYPE STRING,
VFA_KZWI5 TYPE STRING,
VFA_KZWI6 TYPE STRING,
VFA_GUBRT TYPE STRING,
VFA_GUNET TYPE STRING,
VFA_KZWI1G TYPE STRING,
VFA_KZWI2G TYPE STRING,
VFA_KZWI3G TYPE STRING,
VFA_KZWI4G TYPE STRING,
VFA_KZWI5G TYPE STRING,
VFA_KZWI6G TYPE STRING,END OF T_EKKO_STR. DATA: WA_MCJHAGVZB_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_MCJHAGVZB_STR-SUPKZ sy-vline
WA_MCJHAGVZB_STR-MANDT sy-vline
WA_MCJHAGVZB_STR-AVM_NR sy-vline
WA_MCJHAGVZB_STR-POS_NR sy-vline
WA_MCJHAGVZB_STR-GRUPPABRNR sy-vline
WA_MCJHAGVZB_STR-VNR sy-vline
WA_MCJHAGVZB_STR-BEDNR sy-vline
WA_MCJHAGVZB_STR-PROVTYP sy-vline
WA_MCJHAGVZB_STR-VZUONR sy-vline
WA_MCJHAGVZB_STR-VERM sy-vline
WA_MCJHAGVZB_STR-XAUTO sy-vline
WA_MCJHAGVZB_STR-XFOUND sy-vline
WA_MCJHAGVZB_STR-XMANU sy-vline
WA_MCJHAGVZB_STR-XKOPF_ZUO sy-vline
WA_MCJHAGVZB_STR-XPOS_ZUO sy-vline
WA_MCJHAGVZB_STR-PR_BASIS sy-vline
WA_MCJHAGVZB_STR-PR_BETRAG sy-vline
WA_MCJHAGVZB_STR-PRA_XMANU sy-vline
WA_MCJHAGVZB_STR-PROSL sy-vline
WA_MCJHAGVZB_STR-PROV_SATZ sy-vline
WA_MCJHAGVZB_STR-PROVM_SATZ sy-vline
WA_MCJHAGVZB_STR-PROVM_WAER sy-vline
WA_MCJHAGVZB_STR-PROV_PEIN sy-vline
WA_MCJHAGVZB_STR-PROV_MEIN sy-vline
WA_MCJHAGVZB_STR-BEZKOND sy-vline
WA_MCJHAGVZB_STR-VZMANUAEND sy-vline
WA_MCJHAGVZB_STR-BEDGR sy-vline
WA_MCJHAGVZB_STR-JVERMART sy-vline
WA_MCJHAGVZB_STR-VANR sy-vline
WA_MCJHAGVZB_STR-REF_VZUONR sy-vline
WA_MCJHAGVZB_STR-SUC_VZUONR sy-vline
WA_MCJHAGVZB_STR-CORRCAUSE sy-vline
WA_MCJHAGVZB_STR-CANCELCC sy-vline
WA_MCJHAGVZB_STR-STATUSK sy-vline
WA_MCJHAGVZB_STR-XPROV_EE sy-vline
WA_MCJHAGVZB_STR-XUVVZ sy-vline
WA_MCJHAGVZB_STR-XZUOINACT sy-vline
WA_MCJHAGVZB_STR-XDELETED sy-vline
WA_MCJHAGVZB_STR-MERKMAL0 sy-vline
WA_MCJHAGVZB_STR-MERKMAL1 sy-vline
WA_MCJHAGVZB_STR-MERKMAL2 sy-vline
WA_MCJHAGVZB_STR-MERKMAL3 sy-vline
WA_MCJHAGVZB_STR-MERKMAL4 sy-vline
WA_MCJHAGVZB_STR-MERKMAL5 sy-vline
WA_MCJHAGVZB_STR-MERKMAL6 sy-vline
WA_MCJHAGVZB_STR-MERKMAL7 sy-vline
WA_MCJHAGVZB_STR-MERKMAL8 sy-vline
WA_MCJHAGVZB_STR-MERKMAL9 sy-vline
WA_MCJHAGVZB_STR-VZ_UMBRT sy-vline
WA_MCJHAGVZB_STR-VZ_UMNET sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI1 sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI2 sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI3 sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI4 sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI5 sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI6 sy-vline
WA_MCJHAGVZB_STR-VZ_GUBRT sy-vline
WA_MCJHAGVZB_STR-VZ_GUNET sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI1G sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI2G sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI3G sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI4G sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI5G sy-vline
WA_MCJHAGVZB_STR-VZ_KZWI6G sy-vline
WA_MCJHAGVZB_STR-VZA_UMBRT sy-vline
WA_MCJHAGVZB_STR-VZA_UMNET sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI1 sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI2 sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI3 sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI4 sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI5 sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI6 sy-vline
WA_MCJHAGVZB_STR-VZA_GUBRT sy-vline
WA_MCJHAGVZB_STR-VZA_GUNET sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI1G sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI2G sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI3G sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI4G sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI5G sy-vline
WA_MCJHAGVZB_STR-VZA_KZWI6G sy-vline
WA_MCJHAGVZB_STR-J_FAKDAT sy-vline
WA_MCJHAGVZB_STR-J_KUNAG sy-vline
WA_MCJHAGVZB_STR-J_KUNRG sy-vline
WA_MCJHAGVZB_STR-J_INSERENT sy-vline
WA_MCJHAGVZB_STR-J_VKORG sy-vline
WA_MCJHAGVZB_STR-J_VTWEG sy-vline
WA_MCJHAGVZB_STR-J_SPART sy-vline
WA_MCJHAGVZB_STR-J_BEINH sy-vline
WA_MCJHAGVZB_STR-J_BEINH_BA sy-vline
WA_MCJHAGVZB_STR-VF_UMBRT sy-vline
WA_MCJHAGVZB_STR-VF_UMNET sy-vline
WA_MCJHAGVZB_STR-VF_KZWI1 sy-vline
WA_MCJHAGVZB_STR-VF_KZWI2 sy-vline
WA_MCJHAGVZB_STR-VF_KZWI3 sy-vline
WA_MCJHAGVZB_STR-VF_KZWI4 sy-vline
WA_MCJHAGVZB_STR-VF_KZWI5 sy-vline
WA_MCJHAGVZB_STR-VF_KZWI6 sy-vline
WA_MCJHAGVZB_STR-VF_GUBRT sy-vline
WA_MCJHAGVZB_STR-VF_GUNET sy-vline
WA_MCJHAGVZB_STR-VF_KZWI1G sy-vline
WA_MCJHAGVZB_STR-VF_KZWI2G sy-vline
WA_MCJHAGVZB_STR-VF_KZWI3G sy-vline
WA_MCJHAGVZB_STR-VF_KZWI4G sy-vline
WA_MCJHAGVZB_STR-VF_KZWI5G sy-vline
WA_MCJHAGVZB_STR-VF_KZWI6G sy-vline
WA_MCJHAGVZB_STR-VFA_UMBRT sy-vline
WA_MCJHAGVZB_STR-VFA_UMNET sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI1 sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI2 sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI3 sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI4 sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI5 sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI6 sy-vline
WA_MCJHAGVZB_STR-VFA_GUBRT sy-vline
WA_MCJHAGVZB_STR-VFA_GUNET sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI1G sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI2G sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI3G sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI4G sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI5G sy-vline
WA_MCJHAGVZB_STR-VFA_KZWI6G sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.