ABAP Select data from SAP table CEPFCSTQUERY2 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 CEPFCSTQUERY2 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 CEPFCSTQUERY2. 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 CEPFCSTQUERY2 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_CEPFCSTQUERY2 TYPE STANDARD TABLE OF CEPFCSTQUERY2,
      WA_CEPFCSTQUERY2 TYPE CEPFCSTQUERY2,
      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: <CEPFCSTQUERY2> TYPE CEPFCSTQUERY2.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM CEPFCSTQUERY2
*  INTO TABLE @DATA(IT_CEPFCSTQUERY22).
*--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_CEPFCSTQUERY2 INDEX 1 INTO DATA(WA_CEPFCSTQUERY22).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_CEPFCSTQUERY2 ASSIGNING <CEPFCSTQUERY2>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<CEPFCSTQUERY2>-MANDT = 1.
<CEPFCSTQUERY2>-ENGAGEMENTPROJECT = 1.
<CEPFCSTQUERY2>-ENGAGEMENTPROJECTNAME = 1.
<CEPFCSTQUERY2>-PROJECTVISIBILITY = 1.
<CEPFCSTQUERY2>-ISMYPROJECT = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_CEPFCSTQUERY2-ENGAGEMENTPROJECTSERVICEORG, sy-vline,
WA_CEPFCSTQUERY2-ENGMTPROJECTSERVICEORGNAME, sy-vline,
WA_CEPFCSTQUERY2-WORKPACKAGE, sy-vline,
WA_CEPFCSTQUERY2-WORKPACKAGENAME, sy-vline,
WA_CEPFCSTQUERY2-WORKPACKAGETYPE, sy-vline,
WA_CEPFCSTQUERY2-WORKPACKAGESTARTDATE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_CEPFCSTQUERY2 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_CEPFCSTQUERY2 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_CEPFCSTQUERY2 INTO WA_CEPFCSTQUERY2. *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 CUNIT, internal->external for field UNITOFMEASURE CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_CEPFCSTQUERY2-UNITOFMEASURE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CEPFCSTQUERY2-UNITOFMEASURE.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ALPHA, internal->external for field SALESORDER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_CEPFCSTQUERY2-SALESORDER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CEPFCSTQUERY2-SALESORDER.
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_CEPFCSTQUERY2_STR,
MANDT TYPE STRING,
ENGAGEMENTPROJECT TYPE STRING,
ENGAGEMENTPROJECTNAME TYPE STRING,
PROJECTVISIBILITY TYPE STRING,
ISMYPROJECT TYPE STRING,
ENGAGEMENTPROJECTSERVICEORG TYPE STRING,
ENGMTPROJECTSERVICEORGNAME TYPE STRING,
WORKPACKAGE TYPE STRING,
WORKPACKAGENAME TYPE STRING,
WORKPACKAGETYPE TYPE STRING,
WORKPACKAGESTARTDATE TYPE STRING,
WORKPACKAGEENDDATE TYPE STRING,
ENGAGEMENTPROJECTRESOURCETYPE TYPE STRING,
ENGAGEMENTPROJECTRESOURCE TYPE STRING,
ENGAGEMENTPROJRESOURCETEXT TYPE STRING,
WORKITEM TYPE STRING,
WORKITEMNAME TYPE STRING,
ENGMNTPROJECTDELIVERYORG TYPE STRING,
ENGAGEMENTPROJDELIVERYORGNAME TYPE STRING,
UNITOFMEASURE TYPE STRING,
WORKPACKAGEUNITUOM TYPE STRING,
CURRENCY TYPE STRING,
FISCALPERIOD TYPE STRING,
FISCALYEAR TYPE STRING,
FISCALYEARPERIOD TYPE STRING,
ENGAGEMENTPROJECTFORECASTYEAR TYPE STRING,
ENGAGEMENTPROJECTFORECASTMONTH TYPE STRING,
ENGAGEMENTPROJECTREVIEWSTATUS TYPE STRING,
ENGAGEMENTPROJREVIEWSTATUSTEXT TYPE STRING,
SALESORDER TYPE STRING,
SALESORDERITEM TYPE STRING,
SALESORDERITEMTEXT TYPE STRING,
WBSELEMENTEXTERNALID TYPE STRING,
ENGAGEMENTPROJECTSTAGE TYPE STRING,
PLNDCOSTAMT TYPE STRING,
PLANNEDEFFORTCOSTAMT TYPE STRING,
ADDLRESOURCEPLANNEDCOSTAMOUNT TYPE STRING,
PLNDEFFORTQTY TYPE STRING,
ADDLRESOURCEPLANNEDQUANTITY TYPE STRING,
PLANUNITQUANTITY TYPE STRING,
SUPPLYEFFORTQTY TYPE STRING,
ACTUALPLANDEVIATIONCOST TYPE STRING,
ACTUALPLANDEVIATIONEFFORT TYPE STRING,
ADDLRSCEACTLPLANDEVIATIONQTY TYPE STRING,
ACTUALCOSTUPTOFSCLYRPERD TYPE STRING,
ACTUALEFFORTUPTOFSCLYRPERD TYPE STRING,
ADDLRESOURCEACTUALQUANTITY TYPE STRING,
ACTUALUNITQTY TYPE STRING,
PLANNEDEXPENSECOST TYPE STRING,
ACTLEXPENSECOSTUPTOFSCLYRPERD TYPE STRING,
ACTUALPLANDEVIATIONEXPNCOST TYPE STRING,
PLANNEDCOSTRATE TYPE STRING,
ADDLRESOURCEPLANNEDCOSTRATE TYPE STRING,
COSTVARIANCE TYPE STRING,
EFFORTVARIANCE TYPE STRING,
ADDLRSCEVARCOFQTY TYPE STRING,
CALCULATEDADJUSTEDRMNGCOST TYPE STRING,
CALCULATEDADJUSTEDRMNGEFFORT TYPE STRING,
ADDLRSCECALCDESTTOCMPLTQTY TYPE STRING,
CALCDADJUSTEDRMNGEXPNCOST TYPE STRING,
ESTIMATETOCOMPLETEEXPENSECOST TYPE STRING,
ESTIMATETOCOMPLETEEFFORT TYPE STRING,
ADDLRSCEESTTOCMPLTQTY TYPE STRING,
ESTIMATETOCOMPLETEEFFORTCOST TYPE STRING,
ADDLRSCEESTTOCMPLTCOSTAMOUNT TYPE STRING,
ESTIMATETOCOMPLETECOSTAMOUNT TYPE STRING,
ESTIMATEATCOMPLETIONEFFORT TYPE STRING,
ADDLRSCEESTATCOMPLTNQTY TYPE STRING,
ESTIMATEATCOMPLETIONCOST TYPE STRING,
PERCENTAGEOFCOMPLETIONEFFORT TYPE STRING,
UNITCOMPLTNRATEINPERCENT TYPE STRING,
DERIVEDESTATCOMPLTNCOST TYPE STRING,
PERCENTAGEOFCOMPLETIONCOST TYPE STRING,
REVENUEASSOLDAMOUNT TYPE STRING,END OF T_EKKO_STR. DATA: WA_CEPFCSTQUERY2_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_CEPFCSTQUERY2_STR-MANDT sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECT sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTNAME sy-vline
WA_CEPFCSTQUERY2_STR-PROJECTVISIBILITY sy-vline
WA_CEPFCSTQUERY2_STR-ISMYPROJECT sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTSERVICEORG sy-vline
WA_CEPFCSTQUERY2_STR-ENGMTPROJECTSERVICEORGNAME sy-vline
WA_CEPFCSTQUERY2_STR-WORKPACKAGE sy-vline
WA_CEPFCSTQUERY2_STR-WORKPACKAGENAME sy-vline
WA_CEPFCSTQUERY2_STR-WORKPACKAGETYPE sy-vline
WA_CEPFCSTQUERY2_STR-WORKPACKAGESTARTDATE sy-vline
WA_CEPFCSTQUERY2_STR-WORKPACKAGEENDDATE sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTRESOURCETYPE sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTRESOURCE sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJRESOURCETEXT sy-vline
WA_CEPFCSTQUERY2_STR-WORKITEM sy-vline
WA_CEPFCSTQUERY2_STR-WORKITEMNAME sy-vline
WA_CEPFCSTQUERY2_STR-ENGMNTPROJECTDELIVERYORG sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJDELIVERYORGNAME sy-vline
WA_CEPFCSTQUERY2_STR-UNITOFMEASURE sy-vline
WA_CEPFCSTQUERY2_STR-WORKPACKAGEUNITUOM sy-vline
WA_CEPFCSTQUERY2_STR-CURRENCY sy-vline
WA_CEPFCSTQUERY2_STR-FISCALPERIOD sy-vline
WA_CEPFCSTQUERY2_STR-FISCALYEAR sy-vline
WA_CEPFCSTQUERY2_STR-FISCALYEARPERIOD sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTFORECASTYEAR sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTFORECASTMONTH sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTREVIEWSTATUS sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJREVIEWSTATUSTEXT sy-vline
WA_CEPFCSTQUERY2_STR-SALESORDER sy-vline
WA_CEPFCSTQUERY2_STR-SALESORDERITEM sy-vline
WA_CEPFCSTQUERY2_STR-SALESORDERITEMTEXT sy-vline
WA_CEPFCSTQUERY2_STR-WBSELEMENTEXTERNALID sy-vline
WA_CEPFCSTQUERY2_STR-ENGAGEMENTPROJECTSTAGE sy-vline
WA_CEPFCSTQUERY2_STR-PLNDCOSTAMT sy-vline
WA_CEPFCSTQUERY2_STR-PLANNEDEFFORTCOSTAMT sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRESOURCEPLANNEDCOSTAMOUNT sy-vline
WA_CEPFCSTQUERY2_STR-PLNDEFFORTQTY sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRESOURCEPLANNEDQUANTITY sy-vline
WA_CEPFCSTQUERY2_STR-PLANUNITQUANTITY sy-vline
WA_CEPFCSTQUERY2_STR-SUPPLYEFFORTQTY sy-vline
WA_CEPFCSTQUERY2_STR-ACTUALPLANDEVIATIONCOST sy-vline
WA_CEPFCSTQUERY2_STR-ACTUALPLANDEVIATIONEFFORT sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRSCEACTLPLANDEVIATIONQTY sy-vline
WA_CEPFCSTQUERY2_STR-ACTUALCOSTUPTOFSCLYRPERD sy-vline
WA_CEPFCSTQUERY2_STR-ACTUALEFFORTUPTOFSCLYRPERD sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRESOURCEACTUALQUANTITY sy-vline
WA_CEPFCSTQUERY2_STR-ACTUALUNITQTY sy-vline
WA_CEPFCSTQUERY2_STR-PLANNEDEXPENSECOST sy-vline
WA_CEPFCSTQUERY2_STR-ACTLEXPENSECOSTUPTOFSCLYRPERD sy-vline
WA_CEPFCSTQUERY2_STR-ACTUALPLANDEVIATIONEXPNCOST sy-vline
WA_CEPFCSTQUERY2_STR-PLANNEDCOSTRATE sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRESOURCEPLANNEDCOSTRATE sy-vline
WA_CEPFCSTQUERY2_STR-COSTVARIANCE sy-vline
WA_CEPFCSTQUERY2_STR-EFFORTVARIANCE sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRSCEVARCOFQTY sy-vline
WA_CEPFCSTQUERY2_STR-CALCULATEDADJUSTEDRMNGCOST sy-vline
WA_CEPFCSTQUERY2_STR-CALCULATEDADJUSTEDRMNGEFFORT sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRSCECALCDESTTOCMPLTQTY sy-vline
WA_CEPFCSTQUERY2_STR-CALCDADJUSTEDRMNGEXPNCOST sy-vline
WA_CEPFCSTQUERY2_STR-ESTIMATETOCOMPLETEEXPENSECOST sy-vline
WA_CEPFCSTQUERY2_STR-ESTIMATETOCOMPLETEEFFORT sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRSCEESTTOCMPLTQTY sy-vline
WA_CEPFCSTQUERY2_STR-ESTIMATETOCOMPLETEEFFORTCOST sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRSCEESTTOCMPLTCOSTAMOUNT sy-vline
WA_CEPFCSTQUERY2_STR-ESTIMATETOCOMPLETECOSTAMOUNT sy-vline
WA_CEPFCSTQUERY2_STR-ESTIMATEATCOMPLETIONEFFORT sy-vline
WA_CEPFCSTQUERY2_STR-ADDLRSCEESTATCOMPLTNQTY sy-vline
WA_CEPFCSTQUERY2_STR-ESTIMATEATCOMPLETIONCOST sy-vline
WA_CEPFCSTQUERY2_STR-PERCENTAGEOFCOMPLETIONEFFORT sy-vline
WA_CEPFCSTQUERY2_STR-UNITCOMPLTNRATEINPERCENT sy-vline
WA_CEPFCSTQUERY2_STR-DERIVEDESTATCOMPLTNCOST sy-vline
WA_CEPFCSTQUERY2_STR-PERCENTAGEOFCOMPLETIONCOST sy-vline
WA_CEPFCSTQUERY2_STR-REVENUEASSOLDAMOUNT sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.