ABAP Select data from SAP table V_LAECRM_ITEMS 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_LAECRM_ITEMS 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_LAECRM_ITEMS. 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_LAECRM_ITEMS 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_LAECRM_ITEMS TYPE STANDARD TABLE OF V_LAECRM_ITEMS,
      WA_V_LAECRM_ITEMS TYPE V_LAECRM_ITEMS,
      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_LAECRM_ITEMS> TYPE V_LAECRM_ITEMS.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM V_LAECRM_ITEMS
*  INTO TABLE @DATA(IT_V_LAECRM_ITEMS2).
*--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_LAECRM_ITEMS INDEX 1 INTO DATA(WA_V_LAECRM_ITEMS2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_V_LAECRM_ITEMS ASSIGNING <V_LAECRM_ITEMS>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<V_LAECRM_ITEMS>-MANDT = 1.
<V_LAECRM_ITEMS>-BUKRS = 1.
<V_LAECRM_ITEMS>-OBJID = 1.
<V_LAECRM_ITEMS>-REF_SUBKEY = 1.
<V_LAECRM_ITEMS>-TMSTAMP = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_V_LAECRM_ITEMS-EVENT, sy-vline,
WA_V_LAECRM_ITEMS-STATUS, sy-vline,
WA_V_LAECRM_ITEMS-ITEMNO, sy-vline,
WA_V_LAECRM_ITEMS-ITEMNO_EXT, sy-vline,
WA_V_LAECRM_ITEMS-ITEMTXT, sy-vline,
WA_V_LAECRM_ITEMS-CURRENCY, sy-vline.
ENDLOOP. *Add any further fields from structure WA_V_LAECRM_ITEMS 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_LAECRM_ITEMS 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_LAECRM_ITEMS INTO WA_V_LAECRM_ITEMS. *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 EXCRT, internal->external for field EXCHG_RATE CALL FUNCTION 'CONVERSION_EXIT_EXCRT_OUTPUT' EXPORTING input = WA_V_LAECRM_ITEMS-EXCHG_RATE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_V_LAECRM_ITEMS-EXCHG_RATE.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field LESSOR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_V_LAECRM_ITEMS-LESSOR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_V_LAECRM_ITEMS-LESSOR.
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_LAECRM_ITEMS_STR,
MANDT TYPE STRING,
BUKRS TYPE STRING,
OBJID TYPE STRING,
REF_SUBKEY TYPE STRING,
TMSTAMP TYPE STRING,
EVENT TYPE STRING,
STATUS TYPE STRING,
ITEMNO TYPE STRING,
ITEMNO_EXT TYPE STRING,
ITEMTXT TYPE STRING,
CURRENCY TYPE STRING,
EXCHG_RATE TYPE STRING,
QUANTITY TYPE STRING,
UNIT TYPE STRING,
BUKRS_REF TYPE STRING,
OBJID_REF TYPE STRING,
SUBID_REF TYPE STRING,
SUBID_STG TYPE STRING,
UIID TYPE STRING,
EXT_POS_REF TYPE STRING,
FREE_POS_REF TYPE STRING,
CONTSTARTDT TYPE STRING,
CONTENDDT TYPE STRING,
LEASTARTDT TYPE STRING,
LEASOPTEXEDT TYPE STRING,
TERM TYPE STRING,
REM_USEFUL_LIFE TYPE STRING,
PRODUCT TYPE STRING,
USEDPRODUCT TYPE STRING,
CUSTGROUP TYPE STRING,
SALESORG TYPE STRING,
DIS_CHANNEL TYPE STRING,
SOLDTO TYPE STRING,
INSTAT TYPE STRING,
FINPROD TYPE STRING,
FINCLAS TYPE STRING,
BP_USAGE TYPE STRING,
DEVICECLASS TYPE STRING,
OPTIONID TYPE STRING,
CLASSIF_LOCAL TYPE STRING,
CLASSIF_GROUP TYPE STRING,
CLASSIF_LO_FLAG TYPE STRING,
CLASSIF_GR_FLAG TYPE STRING,
CLAS_CUSTOMER TYPE STRING,
BUS_AREA TYPE STRING,
CLASSIF_LOCALBK TYPE STRING,
CLASSIF_GROUPBK TYPE STRING,
CLASSIF_LOBK_FL TYPE STRING,
CLASSIF_GRBK_FL TYPE STRING,
INTRATE TYPE STRING,
PROC_INDICATOR TYPE STRING,
BU_ID_NUMBER TYPE STRING,
BU_ID_TYPE TYPE STRING,
BU_PARTNER TYPE STRING,
BEMOT TYPE STRING,
BUAG_ID TYPE STRING,
TMSTAMPADJ TYPE STRING,
PROCMODE TYPE STRING,
LOGHANDLE TYPE STRING,
TMSTAMPREV TYPE STRING,
ALLOCATION_ID TYPE STRING,
TMSTAMPESP TYPE STRING,
RUNID TYPE STRING,
BUS_SCENARIO_ID TYPE STRING,
FIELD01 TYPE STRING,
FIELD02 TYPE STRING,
FIELD03 TYPE STRING,
LESSOR TYPE STRING,END OF T_EKKO_STR. DATA: WA_V_LAECRM_ITEMS_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_LAECRM_ITEMS_STR-MANDT sy-vline
WA_V_LAECRM_ITEMS_STR-BUKRS sy-vline
WA_V_LAECRM_ITEMS_STR-OBJID sy-vline
WA_V_LAECRM_ITEMS_STR-REF_SUBKEY sy-vline
WA_V_LAECRM_ITEMS_STR-TMSTAMP sy-vline
WA_V_LAECRM_ITEMS_STR-EVENT sy-vline
WA_V_LAECRM_ITEMS_STR-STATUS sy-vline
WA_V_LAECRM_ITEMS_STR-ITEMNO sy-vline
WA_V_LAECRM_ITEMS_STR-ITEMNO_EXT sy-vline
WA_V_LAECRM_ITEMS_STR-ITEMTXT sy-vline
WA_V_LAECRM_ITEMS_STR-CURRENCY sy-vline
WA_V_LAECRM_ITEMS_STR-EXCHG_RATE sy-vline
WA_V_LAECRM_ITEMS_STR-QUANTITY sy-vline
WA_V_LAECRM_ITEMS_STR-UNIT sy-vline
WA_V_LAECRM_ITEMS_STR-BUKRS_REF sy-vline
WA_V_LAECRM_ITEMS_STR-OBJID_REF sy-vline
WA_V_LAECRM_ITEMS_STR-SUBID_REF sy-vline
WA_V_LAECRM_ITEMS_STR-SUBID_STG sy-vline
WA_V_LAECRM_ITEMS_STR-UIID sy-vline
WA_V_LAECRM_ITEMS_STR-EXT_POS_REF sy-vline
WA_V_LAECRM_ITEMS_STR-FREE_POS_REF sy-vline
WA_V_LAECRM_ITEMS_STR-CONTSTARTDT sy-vline
WA_V_LAECRM_ITEMS_STR-CONTENDDT sy-vline
WA_V_LAECRM_ITEMS_STR-LEASTARTDT sy-vline
WA_V_LAECRM_ITEMS_STR-LEASOPTEXEDT sy-vline
WA_V_LAECRM_ITEMS_STR-TERM sy-vline
WA_V_LAECRM_ITEMS_STR-REM_USEFUL_LIFE sy-vline
WA_V_LAECRM_ITEMS_STR-PRODUCT sy-vline
WA_V_LAECRM_ITEMS_STR-USEDPRODUCT sy-vline
WA_V_LAECRM_ITEMS_STR-CUSTGROUP sy-vline
WA_V_LAECRM_ITEMS_STR-SALESORG sy-vline
WA_V_LAECRM_ITEMS_STR-DIS_CHANNEL sy-vline
WA_V_LAECRM_ITEMS_STR-SOLDTO sy-vline
WA_V_LAECRM_ITEMS_STR-INSTAT sy-vline
WA_V_LAECRM_ITEMS_STR-FINPROD sy-vline
WA_V_LAECRM_ITEMS_STR-FINCLAS sy-vline
WA_V_LAECRM_ITEMS_STR-BP_USAGE sy-vline
WA_V_LAECRM_ITEMS_STR-DEVICECLASS sy-vline
WA_V_LAECRM_ITEMS_STR-OPTIONID sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_LOCAL sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_GROUP sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_LO_FLAG sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_GR_FLAG sy-vline
WA_V_LAECRM_ITEMS_STR-CLAS_CUSTOMER sy-vline
WA_V_LAECRM_ITEMS_STR-BUS_AREA sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_LOCALBK sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_GROUPBK sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_LOBK_FL sy-vline
WA_V_LAECRM_ITEMS_STR-CLASSIF_GRBK_FL sy-vline
WA_V_LAECRM_ITEMS_STR-INTRATE sy-vline
WA_V_LAECRM_ITEMS_STR-PROC_INDICATOR sy-vline
WA_V_LAECRM_ITEMS_STR-BU_ID_NUMBER sy-vline
WA_V_LAECRM_ITEMS_STR-BU_ID_TYPE sy-vline
WA_V_LAECRM_ITEMS_STR-BU_PARTNER sy-vline
WA_V_LAECRM_ITEMS_STR-BEMOT sy-vline
WA_V_LAECRM_ITEMS_STR-BUAG_ID sy-vline
WA_V_LAECRM_ITEMS_STR-TMSTAMPADJ sy-vline
WA_V_LAECRM_ITEMS_STR-PROCMODE sy-vline
WA_V_LAECRM_ITEMS_STR-LOGHANDLE sy-vline
WA_V_LAECRM_ITEMS_STR-TMSTAMPREV sy-vline
WA_V_LAECRM_ITEMS_STR-ALLOCATION_ID sy-vline
WA_V_LAECRM_ITEMS_STR-TMSTAMPESP sy-vline
WA_V_LAECRM_ITEMS_STR-RUNID sy-vline
WA_V_LAECRM_ITEMS_STR-BUS_SCENARIO_ID sy-vline
WA_V_LAECRM_ITEMS_STR-FIELD01 sy-vline
WA_V_LAECRM_ITEMS_STR-FIELD02 sy-vline
WA_V_LAECRM_ITEMS_STR-FIELD03 sy-vline
WA_V_LAECRM_ITEMS_STR-LESSOR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.