ABAP Select data from SAP table V_TCMAC_DEF 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 V_TCMAC_DEF 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 V_TCMAC_DEF. 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 V_TCMAC_DEF 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_V_TCMAC_DEF TYPE STANDARD TABLE OF V_TCMAC_DEF,
      WA_V_TCMAC_DEF TYPE V_TCMAC_DEF,
      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: <V_TCMAC_DEF> TYPE V_TCMAC_DEF.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM V_TCMAC_DEF
*  INTO TABLE @DATA(IT_V_TCMAC_DEF2).
*--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_V_TCMAC_DEF INDEX 1 INTO DATA(WA_V_TCMAC_DEF2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_V_TCMAC_DEF ASSIGNING <V_TCMAC_DEF>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<V_TCMAC_DEF>-MANDT = 1.
<V_TCMAC_DEF>-VKTYP = 1.
<V_TCMAC_DEF>-APPLK = 1.
<V_TCMAC_DEF>-TEXT = 1.
<V_TCMAC_DEF>-VKONA = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_V_TCMAC_DEF-VKBEZ, sy-vline,
WA_V_TCMAC_DEF-EXVKO, sy-vline,
WA_V_TCMAC_DEF-OPBUK, sy-vline,
WA_V_TCMAC_DEF-STDBK, sy-vline,
WA_V_TCMAC_DEF-ABWRE, sy-vline,
WA_V_TCMAC_DEF-ABWRA, sy-vline.
ENDLOOP. *Add any further fields from structure WA_V_TCMAC_DEF 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_V_TCMAC_DEF 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_V_TCMAC_DEF INTO WA_V_TCMAC_DEF. *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 ABWRE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_V_TCMAC_DEF-ABWRE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_V_TCMAC_DEF-ABWRE.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field DEF_REC CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_V_TCMAC_DEF-DEF_REC IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_V_TCMAC_DEF-DEF_REC.
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_V_TCMAC_DEF_STR,
MANDT TYPE STRING,
VKTYP TYPE STRING,
APPLK TYPE STRING,
TEXT TYPE STRING,
VKONA TYPE STRING,
VKBEZ TYPE STRING,
EXVKO TYPE STRING,
OPBUK TYPE STRING,
STDBK TYPE STRING,
ABWRE TYPE STRING,
ABWRA TYPE STRING,
ABWMA TYPE STRING,
EBVTY TYPE STRING,
ABVTY TYPE STRING,
EZAWE TYPE STRING,
AZAWE TYPE STRING,
VWNZA TYPE STRING,
LOEVM TYPE STRING,
ABWVK TYPE STRING,
IKEY TYPE STRING,
MAHNV TYPE STRING,
MGRUP TYPE STRING,
FDGRP TYPE STRING,
FDZTG TYPE STRING,
VKPBZ TYPE STRING,
VKONV TYPE STRING,
GPARV TYPE STRING,
ABWRH TYPE STRING,
BEGRU TYPE STRING,
TOGRU TYPE STRING,
TXJCD TYPE STRING,
COPRC TYPE STRING,
VBUND TYPE STRING,
CCARD_ID TYPE STRING,
CCARD_OUT TYPE STRING,
UEBTR TYPE STRING,
DEF_REC TYPE STRING,
DEF_REC_IND TYPE STRING,
QSSKZ_A TYPE STRING,
QSSKZ_E TYPE STRING,
QSZNR TYPE STRING,
QSZDT TYPE STRING,
CORR_MAHNV TYPE STRING,
FITYP TYPE STRING,
PROVINCE TYPE STRING,
COUNTY TYPE STRING,
VERTYP TYPE STRING,
AUGRS_DEF TYPE STRING,
LANDL TYPE STRING,
BUPLA TYPE STRING,
BPTAXTYPE1 TYPE STRING,
BPTAXTYPE2 TYPE STRING,
INVTP TYPE STRING,
FCALID TYPE STRING,
XFIX_EZAWE TYPE STRING,
XFIX_AZAWE TYPE STRING,
XFIX_OP_LOCK TYPE STRING,
XFIX_IKEY TYPE STRING,
XFIX_TOGRU TYPE STRING,
XFIX_VERTYP TYPE STRING,
XFIX_MAHNV TYPE STRING,
XFIX_COPRC TYPE STRING,
XFIX_CORR_MAHNV TYPE STRING,
XNO_AUTO_UPDATE TYPE STRING,END OF T_EKKO_STR. DATA: WA_V_TCMAC_DEF_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_V_TCMAC_DEF_STR-MANDT sy-vline
WA_V_TCMAC_DEF_STR-VKTYP sy-vline
WA_V_TCMAC_DEF_STR-APPLK sy-vline
WA_V_TCMAC_DEF_STR-TEXT sy-vline
WA_V_TCMAC_DEF_STR-VKONA sy-vline
WA_V_TCMAC_DEF_STR-VKBEZ sy-vline
WA_V_TCMAC_DEF_STR-EXVKO sy-vline
WA_V_TCMAC_DEF_STR-OPBUK sy-vline
WA_V_TCMAC_DEF_STR-STDBK sy-vline
WA_V_TCMAC_DEF_STR-ABWRE sy-vline
WA_V_TCMAC_DEF_STR-ABWRA sy-vline
WA_V_TCMAC_DEF_STR-ABWMA sy-vline
WA_V_TCMAC_DEF_STR-EBVTY sy-vline
WA_V_TCMAC_DEF_STR-ABVTY sy-vline
WA_V_TCMAC_DEF_STR-EZAWE sy-vline
WA_V_TCMAC_DEF_STR-AZAWE sy-vline
WA_V_TCMAC_DEF_STR-VWNZA sy-vline
WA_V_TCMAC_DEF_STR-LOEVM sy-vline
WA_V_TCMAC_DEF_STR-ABWVK sy-vline
WA_V_TCMAC_DEF_STR-IKEY sy-vline
WA_V_TCMAC_DEF_STR-MAHNV sy-vline
WA_V_TCMAC_DEF_STR-MGRUP sy-vline
WA_V_TCMAC_DEF_STR-FDGRP sy-vline
WA_V_TCMAC_DEF_STR-FDZTG sy-vline
WA_V_TCMAC_DEF_STR-VKPBZ sy-vline
WA_V_TCMAC_DEF_STR-VKONV sy-vline
WA_V_TCMAC_DEF_STR-GPARV sy-vline
WA_V_TCMAC_DEF_STR-ABWRH sy-vline
WA_V_TCMAC_DEF_STR-BEGRU sy-vline
WA_V_TCMAC_DEF_STR-TOGRU sy-vline
WA_V_TCMAC_DEF_STR-TXJCD sy-vline
WA_V_TCMAC_DEF_STR-COPRC sy-vline
WA_V_TCMAC_DEF_STR-VBUND sy-vline
WA_V_TCMAC_DEF_STR-CCARD_ID sy-vline
WA_V_TCMAC_DEF_STR-CCARD_OUT sy-vline
WA_V_TCMAC_DEF_STR-UEBTR sy-vline
WA_V_TCMAC_DEF_STR-DEF_REC sy-vline
WA_V_TCMAC_DEF_STR-DEF_REC_IND sy-vline
WA_V_TCMAC_DEF_STR-QSSKZ_A sy-vline
WA_V_TCMAC_DEF_STR-QSSKZ_E sy-vline
WA_V_TCMAC_DEF_STR-QSZNR sy-vline
WA_V_TCMAC_DEF_STR-QSZDT sy-vline
WA_V_TCMAC_DEF_STR-CORR_MAHNV sy-vline
WA_V_TCMAC_DEF_STR-FITYP sy-vline
WA_V_TCMAC_DEF_STR-PROVINCE sy-vline
WA_V_TCMAC_DEF_STR-COUNTY sy-vline
WA_V_TCMAC_DEF_STR-VERTYP sy-vline
WA_V_TCMAC_DEF_STR-AUGRS_DEF sy-vline
WA_V_TCMAC_DEF_STR-LANDL sy-vline
WA_V_TCMAC_DEF_STR-BUPLA sy-vline
WA_V_TCMAC_DEF_STR-BPTAXTYPE1 sy-vline
WA_V_TCMAC_DEF_STR-BPTAXTYPE2 sy-vline
WA_V_TCMAC_DEF_STR-INVTP sy-vline
WA_V_TCMAC_DEF_STR-FCALID sy-vline
WA_V_TCMAC_DEF_STR-XFIX_EZAWE sy-vline
WA_V_TCMAC_DEF_STR-XFIX_AZAWE sy-vline
WA_V_TCMAC_DEF_STR-XFIX_OP_LOCK sy-vline
WA_V_TCMAC_DEF_STR-XFIX_IKEY sy-vline
WA_V_TCMAC_DEF_STR-XFIX_TOGRU sy-vline
WA_V_TCMAC_DEF_STR-XFIX_VERTYP sy-vline
WA_V_TCMAC_DEF_STR-XFIX_MAHNV sy-vline
WA_V_TCMAC_DEF_STR-XFIX_COPRC sy-vline
WA_V_TCMAC_DEF_STR-XFIX_CORR_MAHNV sy-vline
WA_V_TCMAC_DEF_STR-XNO_AUTO_UPDATE sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.