ABAP Select data from SAP table WCUS_TERM 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 WCUS_TERM 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 WCUS_TERM. 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 WCUS_TERM 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_WCUS_TERM TYPE STANDARD TABLE OF WCUS_TERM,
      WA_WCUS_TERM TYPE WCUS_TERM,
      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: <WCUS_TERM> TYPE WCUS_TERM.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM WCUS_TERM
*  INTO TABLE @DATA(IT_WCUS_TERM2).
*--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_WCUS_TERM INDEX 1 INTO DATA(WA_WCUS_TERM2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_WCUS_TERM ASSIGNING <WCUS_TERM>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<WCUS_TERM>-SCHLU = 1.
<WCUS_TERM>-GRUPP = 1.
<WCUS_TERM>-GUELT = 1.
<WCUS_TERM>-SONST = 1.
<WCUS_TERM>-FUNKT = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_WCUS_TERM-ABLAF, sy-vline,
WA_WCUS_TERM-STEUG, sy-vline,
WA_WCUS_TERM-PARAM, sy-vline,
WA_WCUS_TERM-ORGEB, sy-vline,
WA_WCUS_TERM-BEREC, sy-vline,
WA_WCUS_TERM-REGEL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_WCUS_TERM 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_WCUS_TERM 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_WCUS_TERM INTO WA_WCUS_TERM. *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.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_WCUS_TERM_STR,
SCHLU TYPE STRING,
GRUPP TYPE STRING,
GUELT TYPE STRING,
SONST TYPE STRING,
FUNKT TYPE STRING,
ABLAF TYPE STRING,
STEUG TYPE STRING,
PARAM TYPE STRING,
ORGEB TYPE STRING,
BEREC TYPE STRING,
REGEL TYPE STRING,
AUTAK TYPE STRING,
PDIFF TYPE STRING,
NDIFF TYPE STRING,
AUTRK TYPE STRING,
KAUFM TYPE STRING,
TRANS TYPE STRING,
ZUORD TYPE STRING,
VORL TYPE STRING,
GESVB TYPE STRING,
ANGVB TYPE STRING,
POSDL TYPE STRING,
FIL TYPE STRING,
VZ TYPE STRING,
OBJ TYPE STRING,
COND TYPE STRING,
NUKR TYPE STRING,
KLAS TYPE STRING,
LSTEU TYPE STRING,
KOMM TYPE STRING,
TSTEU TYPE STRING,
PTYP TYPE STRING,
SSCHN TYPE STRING,
AUFBER TYPE STRING,
NACHRST TYPE STRING,
REFVKPF TYPE STRING,
PROZ TYPE STRING,
BELST TYPE STRING,
BEZEI TYPE STRING,
TRANSP TYPE STRING,
ABSP1 TYPE STRING,
KAPV TYPE STRING,
ZSCH TYPE STRING,
ZSCHNR TYPE STRING,
WELLR TYPE STRING,
ZIEL TYPE STRING,
DEFAU TYPE STRING,
ZV TYPE STRING,
ZS1 TYPE STRING,
ZS2 TYPE STRING,
BEZ1 TYPE STRING,
TS TYPE STRING,
GV TYPE STRING,
FBS TYPE STRING,
FK TYPE STRING,
RR TYPE STRING,
PR TYPE STRING,
AVM TYPE STRING,
EF TYPE STRING,
SU TYPE STRING,
TSF TYPE STRING,
RF TYPE STRING,
MF TYPE STRING,
SR TYPE STRING,
NF TYPE STRING,
DU TYPE STRING,
KW TYPE STRING,
KA TYPE STRING,
ZA TYPE STRING,
RVKPF TYPE STRING,
BEZ2 TYPE STRING,
BEZ3 TYPE STRING,
EINKAUF TYPE STRING,
WARENBEWEGUNG TYPE STRING,
LIEFERUNG TYPE STRING,
BSM TYPE STRING,
BEZ4 TYPE STRING,
PA TYPE STRING,
KAG TYPE STRING,
ASE TYPE STRING,
DSE TYPE STRING,
VSW TYPE STRING,
SMV TYPE STRING,
VA TYPE STRING,
MBV TYPE STRING,
DAG TYPE STRING,
KRL TYPE STRING,
SAB TYPE STRING,
LSCH TYPE STRING,
BEST TYPE STRING,
RIS TYPE STRING,
AVD TYPE STRING,
SVB TYPE STRING,
SWUD TYPE STRING,
SWBD TYPE STRING,
FBDE TYPE STRING,
PROF TYPE STRING,
MPROZ TYPE STRING,
MPROZ1 TYPE STRING,
ALLGVB TYPE STRING,
BEWART TYPE STRING,
STWE TYPE STRING,
ZIS TYPE STRING,
AART TYPE STRING,
DART TYPE STRING,
ZART TYPE STRING,
FPG TYPE STRING,
ABVD TYPE STRING,
AST TYPE STRING,
WLD TYPE STRING,
KT1 TYPE STRING,
KT2 TYPE STRING,
KT3 TYPE STRING,
LJEGRUP TYPE STRING,
KM TYPE STRING,
BNV TYPE STRING,
KK TYPE STRING,
AKT TYPE STRING,
KSTT TYPE STRING,
SART TYPE STRING,
NKRA TYPE STRING,
KANL TYPE STRING,
WAERS TYPE STRING,
WFUNK TYPE STRING,
NACHR TYPE STRING,
BWARTSE TYPE STRING,
FUMF TYPE STRING,
VKRAB TYPE STRING,
DATUM TYPE STRING,
ZUSEINST TYPE STRING,
PERF TYPE STRING,
EMPFIN TYPE STRING,
GUELT1 TYPE STRING,
DFARTST TYPE STRING,
VKPVTL TYPE STRING,
DFBELM TYPE STRING,
SCHEMA TYPE STRING,
STEU_AH TYPE STRING,END OF T_EKKO_STR. DATA: WA_WCUS_TERM_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_WCUS_TERM_STR-SCHLU sy-vline
WA_WCUS_TERM_STR-GRUPP sy-vline
WA_WCUS_TERM_STR-GUELT sy-vline
WA_WCUS_TERM_STR-SONST sy-vline
WA_WCUS_TERM_STR-FUNKT sy-vline
WA_WCUS_TERM_STR-ABLAF sy-vline
WA_WCUS_TERM_STR-STEUG sy-vline
WA_WCUS_TERM_STR-PARAM sy-vline
WA_WCUS_TERM_STR-ORGEB sy-vline
WA_WCUS_TERM_STR-BEREC sy-vline
WA_WCUS_TERM_STR-REGEL sy-vline
WA_WCUS_TERM_STR-AUTAK sy-vline
WA_WCUS_TERM_STR-PDIFF sy-vline
WA_WCUS_TERM_STR-NDIFF sy-vline
WA_WCUS_TERM_STR-AUTRK sy-vline
WA_WCUS_TERM_STR-KAUFM sy-vline
WA_WCUS_TERM_STR-TRANS sy-vline
WA_WCUS_TERM_STR-ZUORD sy-vline
WA_WCUS_TERM_STR-VORL sy-vline
WA_WCUS_TERM_STR-GESVB sy-vline
WA_WCUS_TERM_STR-ANGVB sy-vline
WA_WCUS_TERM_STR-POSDL sy-vline
WA_WCUS_TERM_STR-FIL sy-vline
WA_WCUS_TERM_STR-VZ sy-vline
WA_WCUS_TERM_STR-OBJ sy-vline
WA_WCUS_TERM_STR-COND sy-vline
WA_WCUS_TERM_STR-NUKR sy-vline
WA_WCUS_TERM_STR-KLAS sy-vline
WA_WCUS_TERM_STR-LSTEU sy-vline
WA_WCUS_TERM_STR-KOMM sy-vline
WA_WCUS_TERM_STR-TSTEU sy-vline
WA_WCUS_TERM_STR-PTYP sy-vline
WA_WCUS_TERM_STR-SSCHN sy-vline
WA_WCUS_TERM_STR-AUFBER sy-vline
WA_WCUS_TERM_STR-NACHRST sy-vline
WA_WCUS_TERM_STR-REFVKPF sy-vline
WA_WCUS_TERM_STR-PROZ sy-vline
WA_WCUS_TERM_STR-BELST sy-vline
WA_WCUS_TERM_STR-BEZEI sy-vline
WA_WCUS_TERM_STR-TRANSP sy-vline
WA_WCUS_TERM_STR-ABSP1 sy-vline
WA_WCUS_TERM_STR-KAPV sy-vline
WA_WCUS_TERM_STR-ZSCH sy-vline
WA_WCUS_TERM_STR-ZSCHNR sy-vline
WA_WCUS_TERM_STR-WELLR sy-vline
WA_WCUS_TERM_STR-ZIEL sy-vline
WA_WCUS_TERM_STR-DEFAU sy-vline
WA_WCUS_TERM_STR-ZV sy-vline
WA_WCUS_TERM_STR-ZS1 sy-vline
WA_WCUS_TERM_STR-ZS2 sy-vline
WA_WCUS_TERM_STR-BEZ1 sy-vline
WA_WCUS_TERM_STR-TS sy-vline
WA_WCUS_TERM_STR-GV sy-vline
WA_WCUS_TERM_STR-FBS sy-vline
WA_WCUS_TERM_STR-FK sy-vline
WA_WCUS_TERM_STR-RR sy-vline
WA_WCUS_TERM_STR-PR sy-vline
WA_WCUS_TERM_STR-AVM sy-vline
WA_WCUS_TERM_STR-EF sy-vline
WA_WCUS_TERM_STR-SU sy-vline
WA_WCUS_TERM_STR-TSF sy-vline
WA_WCUS_TERM_STR-RF sy-vline
WA_WCUS_TERM_STR-MF sy-vline
WA_WCUS_TERM_STR-SR sy-vline
WA_WCUS_TERM_STR-NF sy-vline
WA_WCUS_TERM_STR-DU sy-vline
WA_WCUS_TERM_STR-KW sy-vline
WA_WCUS_TERM_STR-KA sy-vline
WA_WCUS_TERM_STR-ZA sy-vline
WA_WCUS_TERM_STR-RVKPF sy-vline
WA_WCUS_TERM_STR-BEZ2 sy-vline
WA_WCUS_TERM_STR-BEZ3 sy-vline
WA_WCUS_TERM_STR-EINKAUF sy-vline
WA_WCUS_TERM_STR-WARENBEWEGUNG sy-vline
WA_WCUS_TERM_STR-LIEFERUNG sy-vline
WA_WCUS_TERM_STR-BSM sy-vline
WA_WCUS_TERM_STR-BEZ4 sy-vline
WA_WCUS_TERM_STR-PA sy-vline
WA_WCUS_TERM_STR-KAG sy-vline
WA_WCUS_TERM_STR-ASE sy-vline
WA_WCUS_TERM_STR-DSE sy-vline
WA_WCUS_TERM_STR-VSW sy-vline
WA_WCUS_TERM_STR-SMV sy-vline
WA_WCUS_TERM_STR-VA sy-vline
WA_WCUS_TERM_STR-MBV sy-vline
WA_WCUS_TERM_STR-DAG sy-vline
WA_WCUS_TERM_STR-KRL sy-vline
WA_WCUS_TERM_STR-SAB sy-vline
WA_WCUS_TERM_STR-LSCH sy-vline
WA_WCUS_TERM_STR-BEST sy-vline
WA_WCUS_TERM_STR-RIS sy-vline
WA_WCUS_TERM_STR-AVD sy-vline
WA_WCUS_TERM_STR-SVB sy-vline
WA_WCUS_TERM_STR-SWUD sy-vline
WA_WCUS_TERM_STR-SWBD sy-vline
WA_WCUS_TERM_STR-FBDE sy-vline
WA_WCUS_TERM_STR-PROF sy-vline
WA_WCUS_TERM_STR-MPROZ sy-vline
WA_WCUS_TERM_STR-MPROZ1 sy-vline
WA_WCUS_TERM_STR-ALLGVB sy-vline
WA_WCUS_TERM_STR-BEWART sy-vline
WA_WCUS_TERM_STR-STWE sy-vline
WA_WCUS_TERM_STR-ZIS sy-vline
WA_WCUS_TERM_STR-AART sy-vline
WA_WCUS_TERM_STR-DART sy-vline
WA_WCUS_TERM_STR-ZART sy-vline
WA_WCUS_TERM_STR-FPG sy-vline
WA_WCUS_TERM_STR-ABVD sy-vline
WA_WCUS_TERM_STR-AST sy-vline
WA_WCUS_TERM_STR-WLD sy-vline
WA_WCUS_TERM_STR-KT1 sy-vline
WA_WCUS_TERM_STR-KT2 sy-vline
WA_WCUS_TERM_STR-KT3 sy-vline
WA_WCUS_TERM_STR-LJEGRUP sy-vline
WA_WCUS_TERM_STR-KM sy-vline
WA_WCUS_TERM_STR-BNV sy-vline
WA_WCUS_TERM_STR-KK sy-vline
WA_WCUS_TERM_STR-AKT sy-vline
WA_WCUS_TERM_STR-KSTT sy-vline
WA_WCUS_TERM_STR-SART sy-vline
WA_WCUS_TERM_STR-NKRA sy-vline
WA_WCUS_TERM_STR-KANL sy-vline
WA_WCUS_TERM_STR-WAERS sy-vline
WA_WCUS_TERM_STR-WFUNK sy-vline
WA_WCUS_TERM_STR-NACHR sy-vline
WA_WCUS_TERM_STR-BWARTSE sy-vline
WA_WCUS_TERM_STR-FUMF sy-vline
WA_WCUS_TERM_STR-VKRAB sy-vline
WA_WCUS_TERM_STR-DATUM sy-vline
WA_WCUS_TERM_STR-ZUSEINST sy-vline
WA_WCUS_TERM_STR-PERF sy-vline
WA_WCUS_TERM_STR-EMPFIN sy-vline
WA_WCUS_TERM_STR-GUELT1 sy-vline
WA_WCUS_TERM_STR-DFARTST sy-vline
WA_WCUS_TERM_STR-VKPVTL sy-vline
WA_WCUS_TERM_STR-DFBELM sy-vline
WA_WCUS_TERM_STR-SCHEMA sy-vline
WA_WCUS_TERM_STR-STEU_AH sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.