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

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

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

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


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_/SCWM/S_TW_COND ASSIGNING </SCWM/S_TW_COND>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
</SCWM/S_TW_COND>-TWP_LGNUM = 1.
</SCWM/S_TW_COND>-TWP_CLOSED_PACK = 1.
</SCWM/S_TW_COND>-TWP_HUTYP = 1.
</SCWM/S_TW_COND>-TWP_PMTYP = 1.
</SCWM/S_TW_COND>-TWP_PACKGR = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_/SCWM/S_TW_COND-TWP_PMAT, sy-vline,
WA_/SCWM/S_TW_COND-TWP_PROCTY, sy-vline,
WA_/SCWM/S_TW_COND-TWP_HUTYPGRP, sy-vline,
WA_/SCWM/S_TW_COND-TWP_COEE_POCO, sy-vline,
WA_/SCWM/S_TW_COND-TWP_LTW_POCO, sy-vline,
WA_/SCWM/S_TW_COND-TWP_NTW_POCO, sy-vline.
ENDLOOP. *Add any further fields from structure WA_/SCWM/S_TW_COND 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_/SCWM/S_TW_COND 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_/SCWM/S_TW_COND INTO WA_/SCWM/S_TW_COND. *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 MATN1, internal->external for field TWP_PMAT CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_/SCWM/S_TW_COND-TWP_PMAT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SCWM/S_TW_COND-TWP_PMAT.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

*Conversion exit CUNIT, internal->external for field UNIT_MAX_LWH CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_/SCWM/S_TW_COND-UNIT_MAX_LWH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SCWM/S_TW_COND-UNIT_MAX_LWH.
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_/SCWM/S_TW_COND_STR,
TWP_LGNUM TYPE STRING,
TWP_CLOSED_PACK TYPE STRING,
TWP_HUTYP TYPE STRING,
TWP_PMTYP TYPE STRING,
TWP_PACKGR TYPE STRING,
TWP_PMAT TYPE STRING,
TWP_PROCTY TYPE STRING,
TWP_HUTYPGRP TYPE STRING,
TWP_COEE_POCO TYPE STRING,
TWP_LTW_POCO TYPE STRING,
TWP_NTW_POCO TYPE STRING,
TWP_SHIPPER_POCO TYPE STRING,
TWP_PUT_ACS TYPE STRING,
TWP_ACS_CURRENT TYPE STRING,
TWP_ACS_TARGET TYPE STRING,
TWP_ACS_CSS TYPE STRING,
TWP_ACS_CSS_CTRY TYPE STRING,
G_WEIGHT TYPE STRING,
N_WEIGHT TYPE STRING,
UNIT_GW TYPE STRING,
T_WEIGHT TYPE STRING,
UNIT_TW TYPE STRING,
G_VOLUME TYPE STRING,
N_VOLUME TYPE STRING,
UNIT_GV TYPE STRING,
T_VOLUME TYPE STRING,
UNIT_TV TYPE STRING,
G_CAPA TYPE STRING,
N_CAPA TYPE STRING,
T_CAPA TYPE STRING,
LENGTH TYPE STRING,
WIDTH TYPE STRING,
HEIGHT TYPE STRING,
UNIT_LWH TYPE STRING,
MAX_WEIGHT TYPE STRING,
TOLW TYPE STRING,
TARE_VAR TYPE STRING,
MAX_VOLUME TYPE STRING,
TOLV TYPE STRING,
MAX_CAPA TYPE STRING,
TOLC TYPE STRING,
MAX_LENGTH TYPE STRING,
MAX_WIDTH TYPE STRING,
MAX_HEIGHT TYPE STRING,
UNIT_MAX_LWH TYPE STRING,END OF T_EKKO_STR. DATA: WA_/SCWM/S_TW_COND_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_/SCWM/S_TW_COND_STR-TWP_LGNUM sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_CLOSED_PACK sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_HUTYP sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_PMTYP sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_PACKGR sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_PMAT sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_PROCTY sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_HUTYPGRP sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_COEE_POCO sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_LTW_POCO sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_NTW_POCO sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_SHIPPER_POCO sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_PUT_ACS sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_ACS_CURRENT sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_ACS_TARGET sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_ACS_CSS sy-vline
WA_/SCWM/S_TW_COND_STR-TWP_ACS_CSS_CTRY sy-vline
WA_/SCWM/S_TW_COND_STR-G_WEIGHT sy-vline
WA_/SCWM/S_TW_COND_STR-N_WEIGHT sy-vline
WA_/SCWM/S_TW_COND_STR-UNIT_GW sy-vline
WA_/SCWM/S_TW_COND_STR-T_WEIGHT sy-vline
WA_/SCWM/S_TW_COND_STR-UNIT_TW sy-vline
WA_/SCWM/S_TW_COND_STR-G_VOLUME sy-vline
WA_/SCWM/S_TW_COND_STR-N_VOLUME sy-vline
WA_/SCWM/S_TW_COND_STR-UNIT_GV sy-vline
WA_/SCWM/S_TW_COND_STR-T_VOLUME sy-vline
WA_/SCWM/S_TW_COND_STR-UNIT_TV sy-vline
WA_/SCWM/S_TW_COND_STR-G_CAPA sy-vline
WA_/SCWM/S_TW_COND_STR-N_CAPA sy-vline
WA_/SCWM/S_TW_COND_STR-T_CAPA sy-vline
WA_/SCWM/S_TW_COND_STR-LENGTH sy-vline
WA_/SCWM/S_TW_COND_STR-WIDTH sy-vline
WA_/SCWM/S_TW_COND_STR-HEIGHT sy-vline
WA_/SCWM/S_TW_COND_STR-UNIT_LWH sy-vline
WA_/SCWM/S_TW_COND_STR-MAX_WEIGHT sy-vline
WA_/SCWM/S_TW_COND_STR-TOLW sy-vline
WA_/SCWM/S_TW_COND_STR-TARE_VAR sy-vline
WA_/SCWM/S_TW_COND_STR-MAX_VOLUME sy-vline
WA_/SCWM/S_TW_COND_STR-TOLV sy-vline
WA_/SCWM/S_TW_COND_STR-MAX_CAPA sy-vline
WA_/SCWM/S_TW_COND_STR-TOLC sy-vline
WA_/SCWM/S_TW_COND_STR-MAX_LENGTH sy-vline
WA_/SCWM/S_TW_COND_STR-MAX_WIDTH sy-vline
WA_/SCWM/S_TW_COND_STR-MAX_HEIGHT sy-vline
WA_/SCWM/S_TW_COND_STR-UNIT_MAX_LWH sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.