ABAP Select data from SAP table /SCMTMS/S_TSPOPT_PROF_K 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 /SCMTMS/S_TSPOPT_PROF_K 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 /SCMTMS/S_TSPOPT_PROF_K. 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 /SCMTMS/S_TSPOPT_PROF_K 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_/SCMTMS/S_TSPOPT_PROF_K TYPE STANDARD TABLE OF /SCMTMS/S_TSPOPT_PROF_K,
      WA_/SCMTMS/S_TSPOPT_PROF_K TYPE /SCMTMS/S_TSPOPT_PROF_K,
      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: </SCMTMS/S_TSPOPT_PROF_K> TYPE /SCMTMS/S_TSPOPT_PROF_K.

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

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM /SCMTMS/S_TSPOPT_PROF_K
  INTO TABLE IT_/SCMTMS/S_TSPOPT_PROF_K.

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM /SCMTMS/S_TSPOPT_PROF_K
*  INTO TABLE @DATA(IT_/SCMTMS/S_TSPOPT_PROF_K2).
*--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_/SCMTMS/S_TSPOPT_PROF_K INDEX 1 INTO DATA(WA_/SCMTMS/S_TSPOPT_PROF_K2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_/SCMTMS/S_TSPOPT_PROF_K ASSIGNING </SCMTMS/S_TSPOPT_PROF_K>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
</SCMTMS/S_TSPOPT_PROF_K>-KEY = 1.
</SCMTMS/S_TSPOPT_PROF_K>-PARENT_KEY = 1.
</SCMTMS/S_TSPOPT_PROF_K>-ROOT_KEY = 1.
</SCMTMS/S_TSPOPT_PROF_K>-PROFILE_ID = 1.
</SCMTMS/S_TSPOPT_PROF_K>-PROFILE_DESCR = 1.
ENDLOOP.

LOOP AT IT_/SCMTMS/S_TSPOPT_PROF_K INTO WA_/SCMTMS/S_TSPOPT_PROF_K.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_/SCMTMS/S_TSPOPT_PROF_K-PROF_TYPE, sy-vline,
WA_/SCMTMS/S_TSPOPT_PROF_K-PLANN_STRAT, sy-vline,
WA_/SCMTMS/S_TSPOPT_PROF_K-PARA_PROFILE, sy-vline,
WA_/SCMTMS/S_TSPOPT_PROF_K-MAX_OPT_TIME, sy-vline,
WA_/SCMTMS/S_TSPOPT_PROF_K-CHK_INCOMP, sy-vline,
WA_/SCMTMS/S_TSPOPT_PROF_K-INC_SET_UUID, sy-vline.
ENDLOOP. *Add any further fields from structure WA_/SCMTMS/S_TSPOPT_PROF_K 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_/SCMTMS/S_TSPOPT_PROF_K 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_/SCMTMS/S_TSPOPT_PROF_K INTO WA_/SCMTMS/S_TSPOPT_PROF_K. *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 PROFILE_ID CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_/SCMTMS/S_TSPOPT_PROF_K-PROFILE_ID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SCMTMS/S_TSPOPT_PROF_K-PROFILE_ID.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit INCST, internal->external for field INC_SET_UUID CALL FUNCTION 'CONVERSION_EXIT_INCST_OUTPUT' EXPORTING input = WA_/SCMTMS/S_TSPOPT_PROF_K-INC_SET_UUID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SCMTMS/S_TSPOPT_PROF_K-INC_SET_UUID.
WRITE:/ 'New Value:', ld_input.

*Conversion exit INCST, internal->external for field INC_SET_UUID_DSO CALL FUNCTION 'CONVERSION_EXIT_INCST_OUTPUT' EXPORTING input = WA_/SCMTMS/S_TSPOPT_PROF_K-INC_SET_UUID_DSO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SCMTMS/S_TSPOPT_PROF_K-INC_SET_UUID_DSO.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit TSTLC, internal->external for field CREATED_ON CALL FUNCTION 'CONVERSION_EXIT_TSTLC_OUTPUT' EXPORTING input = WA_/SCMTMS/S_TSPOPT_PROF_K-CREATED_ON IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SCMTMS/S_TSPOPT_PROF_K-CREATED_ON.
WRITE:/ 'New Value:', ld_input.

*Conversion exit TSTLC, internal->external for field CHANGED_ON CALL FUNCTION 'CONVERSION_EXIT_TSTLC_OUTPUT' EXPORTING input = WA_/SCMTMS/S_TSPOPT_PROF_K-CHANGED_ON IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SCMTMS/S_TSPOPT_PROF_K-CHANGED_ON.
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_/SCMTMS/S_TSPOPT_PROF_K_STR,
KEY TYPE STRING,
PARENT_KEY TYPE STRING,
ROOT_KEY TYPE STRING,
PROFILE_ID TYPE STRING,
PROFILE_DESCR TYPE STRING,
PROF_TYPE TYPE STRING,
PLANN_STRAT TYPE STRING,
PARA_PROFILE TYPE STRING,
MAX_OPT_TIME TYPE STRING,
CHK_INCOMP TYPE STRING,
INC_SET_UUID TYPE STRING,
INC_SET_UUID_DSO TYPE STRING,
COST_ORIGIN TYPE STRING,
ALLOCATION_USE TYPE STRING,
BS_USE TYPE STRING,
STRATEGY_USE TYPE STRING,
CM_CHECK_INFO TYPE STRING,
CM_DEL_ACT TYPE STRING,
CM_MTR_CHK TYPE STRING,
CM_DIST_DURA_CHK TYPE STRING,
CM_TYP_CHK TYPE STRING,
CM_COST_REC TYPE STRING,
POST_ACTION TYPE STRING,
TEND_MANAGER TYPE STRING,
TENDER_ALWAYS TYPE STRING,
TENDERED_OBJECTS TYPE STRING,
TEND_TEMPL TYPE STRING,
ZERO_COSTS TYPE STRING,
OVERALL_TSP_A TYPE STRING,
MAN_ASGNMNT_FIX TYPE STRING,
MAN_RANKS TYPE STRING,
COMMON_CURR TYPE STRING,
OPT_PREF_UNIT TYPE STRING,
HIERARCHA_HANDLING TYPE STRING,
SKIP_RESULT_SCREEN TYPE STRING,
CREATED_BY TYPE STRING,
CREATED_ON TYPE STRING,
CHANGED_BY TYPE STRING,
CHANGED_ON TYPE STRING,
EEW_TSP_PROF_ROOT TYPE STRING,END OF T_EKKO_STR. DATA: WA_/SCMTMS/S_TSPOPT_PROF_K_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_/SCMTMS/S_TSPOPT_PROF_K_STR-KEY sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-PARENT_KEY sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-ROOT_KEY sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-PROFILE_ID sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-PROFILE_DESCR sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-PROF_TYPE sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-PLANN_STRAT sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-PARA_PROFILE sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-MAX_OPT_TIME sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CHK_INCOMP sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-INC_SET_UUID sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-INC_SET_UUID_DSO sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-COST_ORIGIN sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-ALLOCATION_USE sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-BS_USE sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-STRATEGY_USE sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CM_CHECK_INFO sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CM_DEL_ACT sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CM_MTR_CHK sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CM_DIST_DURA_CHK sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CM_TYP_CHK sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CM_COST_REC sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-POST_ACTION sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-TEND_MANAGER sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-TENDER_ALWAYS sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-TENDERED_OBJECTS sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-TEND_TEMPL sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-ZERO_COSTS sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-OVERALL_TSP_A sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-MAN_ASGNMNT_FIX sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-MAN_RANKS sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-COMMON_CURR sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-OPT_PREF_UNIT sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-HIERARCHA_HANDLING sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-SKIP_RESULT_SCREEN sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CREATED_BY sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CREATED_ON sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CHANGED_BY sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-CHANGED_ON sy-vline
WA_/SCMTMS/S_TSPOPT_PROF_K_STR-EEW_TSP_PROF_ROOT sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.