ABAP Select data from SAP table CALLOCSEGMENT 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 CALLOCSEGMENT 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 CALLOCSEGMENT. 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 CALLOCSEGMENT 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_CALLOCSEGMENT TYPE STANDARD TABLE OF CALLOCSEGMENT,
      WA_CALLOCSEGMENT TYPE CALLOCSEGMENT,
      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: <CALLOCSEGMENT> TYPE CALLOCSEGMENT.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM CALLOCSEGMENT
*  INTO TABLE @DATA(IT_CALLOCSEGMENT2).
*--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_CALLOCSEGMENT INDEX 1 INTO DATA(WA_CALLOCSEGMENT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_CALLOCSEGMENT ASSIGNING <CALLOCSEGMENT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<CALLOCSEGMENT>-MANDT = 1.
<CALLOCSEGMENT>-ALLOCATIONTYPE = 1.
<CALLOCSEGMENT>-ALLOCATIONCYCLE = 1.
<CALLOCSEGMENT>-ALLOCATIONCYCLESTARTDATE = 1.
<CALLOCSEGMENT>-ALLOCATIONCYCLESEGMENT = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_CALLOCSEGMENT-SEGMENTNAME, sy-vline,
WA_CALLOCSEGMENT-SEGMENTNAMEDESCRIPTION, sy-vline,
WA_CALLOCSEGMENT-ALLOCATIONSEGMENTPOSITION, sy-vline,
WA_CALLOCSEGMENT-ALLOCATIONSENDERRULE, sy-vline,
WA_CALLOCSEGMENT-ALLOCATIONSENDERRULETEXT, sy-vline,
WA_CALLOCSEGMENT-ALLOCATIONRECEIVERRULE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_CALLOCSEGMENT 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_CALLOCSEGMENT 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_CALLOCSEGMENT INTO WA_CALLOCSEGMENT. *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 ASSESSMENTCOSTELEMENT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_CALLOCSEGMENT-ASSESSMENTCOSTELEMENT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CALLOCSEGMENT-ASSESSMENTCOSTELEMENT.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit CUNIT, internal->external for field UNITOFMEASURE CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_CALLOCSEGMENT-UNITOFMEASURE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CALLOCSEGMENT-UNITOFMEASURE.
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_CALLOCSEGMENT_STR,
MANDT TYPE STRING,
ALLOCATIONTYPE TYPE STRING,
ALLOCATIONCYCLE TYPE STRING,
ALLOCATIONCYCLESTARTDATE TYPE STRING,
ALLOCATIONCYCLESEGMENT TYPE STRING,
SEGMENTNAME TYPE STRING,
SEGMENTNAMEDESCRIPTION TYPE STRING,
ALLOCATIONSEGMENTPOSITION TYPE STRING,
ALLOCATIONSENDERRULE TYPE STRING,
ALLOCATIONSENDERRULETEXT TYPE STRING,
ALLOCATIONRECEIVERRULE TYPE STRING,
ALLOCATIONRECEIVERRULETEXT TYPE STRING,
ALLOCATIONSTATUSDESCRIPTION TYPE STRING,
ALLOCATIONSEGMENTISLOCKED TYPE STRING,
CRITICALITYCODE TYPE STRING,
ALLOCATIONSTATUS TYPE STRING,
ALLOCSCNGTVTRACINGFCTR TYPE STRING,
SENDERTRACINGFIELDGROUP TYPE STRING,
SENDERFIELDGROUP TYPE STRING,
RECEIVERFIELDGROUP TYPE STRING,
RECEIVERTRACINGFIELDGROUP TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD1 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD2 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD3 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD4 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD5 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD6 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD7 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD8 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD9 TYPE STRING,
ALLOCATIONSENDERFIXEDFIELD10 TYPE STRING,
ALLOCATIONCURRENCY TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD1 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD2 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD3 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD4 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD5 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD6 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD7 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD8 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD9 TYPE STRING,
ALLOCATIONRECEIVERFIXEDFIELD10 TYPE STRING,
ALLOCATIONFIELDGROUP TYPE STRING,
ASSESSMENTCOSTELEMENT TYPE STRING,
COSTELEMENTDESCRIPTION TYPE STRING,
ALLOCATIONFIXEDCOSTCENTERCOST TYPE STRING,
ALLOCATIONVARIABLECOSTCTRCOST TYPE STRING,
ALLOCATIONACCOUNTASSIGNMENT TYPE STRING,
ALLOCATIONSEGMENTSORTFIELD TYPE STRING,
ALLOCISSNDREXCLASRCVR TYPE STRING,
MANIPULATIONRULEFORCYCLICMAINT TYPE STRING,
ALLOCATIONSTRUCTURE TYPE STRING,
ALLOCATIONTRANSFERSTRUCTURE TYPE STRING,
ALLOCSNDRFCTRPERCENT TYPE STRING,
ALLOCRCVRFCTRPERCENT TYPE STRING,
ALLOCSEGMENTSHAREINPERCENT TYPE STRING,
ALLOCATIONACTUALPLANVARIANT TYPE STRING,
ALLOCATIONPOSTINGTYPE TYPE STRING,
ALLOCATIONPOSTINGTYPETEXT TYPE STRING,
COMPANYCODE TYPE STRING,
TOPDWNDISTRTMPLNAME TYPE STRING,
CONTROLLINGAREA TYPE STRING,
ALLOCATIONREFERENCELEDGER TYPE STRING,
PLANNINGCATEGORY TYPE STRING,
UNITOFMEASURE TYPE STRING,
ALLOCATIONSENDERVALUEISNEW TYPE STRING,END OF T_EKKO_STR. DATA: WA_CALLOCSEGMENT_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_CALLOCSEGMENT_STR-MANDT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONTYPE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONCYCLE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONCYCLESTARTDATE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONCYCLESEGMENT sy-vline
WA_CALLOCSEGMENT_STR-SEGMENTNAME sy-vline
WA_CALLOCSEGMENT_STR-SEGMENTNAMEDESCRIPTION sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSEGMENTPOSITION sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERRULE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERRULETEXT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERRULE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERRULETEXT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSTATUSDESCRIPTION sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSEGMENTISLOCKED sy-vline
WA_CALLOCSEGMENT_STR-CRITICALITYCODE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSTATUS sy-vline
WA_CALLOCSEGMENT_STR-ALLOCSCNGTVTRACINGFCTR sy-vline
WA_CALLOCSEGMENT_STR-SENDERTRACINGFIELDGROUP sy-vline
WA_CALLOCSEGMENT_STR-SENDERFIELDGROUP sy-vline
WA_CALLOCSEGMENT_STR-RECEIVERFIELDGROUP sy-vline
WA_CALLOCSEGMENT_STR-RECEIVERTRACINGFIELDGROUP sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD1 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD2 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD3 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD4 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD5 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD6 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD7 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD8 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD9 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERFIXEDFIELD10 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONCURRENCY sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD1 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD2 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD3 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD4 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD5 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD6 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD7 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD8 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD9 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONRECEIVERFIXEDFIELD10 sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONFIELDGROUP sy-vline
WA_CALLOCSEGMENT_STR-ASSESSMENTCOSTELEMENT sy-vline
WA_CALLOCSEGMENT_STR-COSTELEMENTDESCRIPTION sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONFIXEDCOSTCENTERCOST sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONVARIABLECOSTCTRCOST sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONACCOUNTASSIGNMENT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSEGMENTSORTFIELD sy-vline
WA_CALLOCSEGMENT_STR-ALLOCISSNDREXCLASRCVR sy-vline
WA_CALLOCSEGMENT_STR-MANIPULATIONRULEFORCYCLICMAINT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSTRUCTURE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONTRANSFERSTRUCTURE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCSNDRFCTRPERCENT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCRCVRFCTRPERCENT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCSEGMENTSHAREINPERCENT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONACTUALPLANVARIANT sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONPOSTINGTYPE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONPOSTINGTYPETEXT sy-vline
WA_CALLOCSEGMENT_STR-COMPANYCODE sy-vline
WA_CALLOCSEGMENT_STR-TOPDWNDISTRTMPLNAME sy-vline
WA_CALLOCSEGMENT_STR-CONTROLLINGAREA sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONREFERENCELEDGER sy-vline
WA_CALLOCSEGMENT_STR-PLANNINGCATEGORY sy-vline
WA_CALLOCSEGMENT_STR-UNITOFMEASURE sy-vline
WA_CALLOCSEGMENT_STR-ALLOCATIONSENDERVALUEISNEW sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.