ABAP Select data from SAP table KCMDTBP_ALL 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 KCMDTBP_ALL 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 KCMDTBP_ALL. 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 KCMDTBP_ALL 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_KCMDTBP_ALL TYPE STANDARD TABLE OF KCMDTBP_ALL,
      WA_KCMDTBP_ALL TYPE KCMDTBP_ALL,
      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: <KCMDTBP_ALL> TYPE KCMDTBP_ALL.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM KCMDTBP_ALL
*  INTO TABLE @DATA(IT_KCMDTBP_ALL2).
*--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_KCMDTBP_ALL INDEX 1 INTO DATA(WA_KCMDTBP_ALL2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_KCMDTBP_ALL ASSIGNING <KCMDTBP_ALL>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<KCMDTBP_ALL>-MANDT = 1.
<KCMDTBP_ALL>-OBJNR = 1.
<KCMDTBP_ALL>-DATBI = 1.
<KCMDTBP_ALL>-STATUS = 1.
<KCMDTBP_ALL>-DATAB = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_KCMDTBP_ALL-LOGSYSTEM, sy-vline,
WA_KCMDTBP_ALL-PHINR, sy-vline,
WA_KCMDTBP_ALL-VERAP, sy-vline,
WA_KCMDTBP_ALL-VERAP_USER, sy-vline,
WA_KCMDTBP_ALL-OWAER, sy-vline,
WA_KCMDTBP_ALL-ERSDA, sy-vline.
ENDLOOP. *Add any further fields from structure WA_KCMDTBP_ALL 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_KCMDTBP_ALL 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_KCMDTBP_ALL INTO WA_KCMDTBP_ALL. *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 LOGSYSTEM CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_KCMDTBP_ALL-LOGSYSTEM IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCMDTBP_ALL-LOGSYSTEM.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field TARKZ_I CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_KCMDTBP_ALL-TARKZ_I IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCMDTBP_ALL-TARKZ_I.
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_KCMDTBP_ALL_STR,
MANDT TYPE STRING,
OBJNR TYPE STRING,
DATBI TYPE STRING,
STATUS TYPE STRING,
DATAB TYPE STRING,
LOGSYSTEM TYPE STRING,
PHINR TYPE STRING,
VERAP TYPE STRING,
VERAP_USER TYPE STRING,
OWAER TYPE STRING,
ERSDA TYPE STRING,
USNAM TYPE STRING,
KTEXT TYPE STRING,
LTEXT TYPE STRING,
CSTDRV TYPE STRING,
KOKRS TYPE STRING,
PRZNR TYPE STRING,
BUKRS TYPE STRING,
GSBER TYPE STRING,
PRCTR TYPE STRING,
WERKS TYPE STRING,
KOSTL TYPE STRING,
KHINR TYPE STRING,
VKORG TYPE STRING,
VTWEG TYPE STRING,
SPART TYPE STRING,
PRART TYPE STRING,
PRTYP TYPE STRING,
PD_TASK_TP TYPE STRING,
PD_TASK_ID TYPE STRING,
WFD_ID TYPE STRING,
VERSION TYPE STRING,
EXETYP TYPE STRING,
CBRKE TYPE STRING,
CBRKI TYPE STRING,
CBART TYPE STRING,
DRVTP TYPE STRING,
CBAT TYPE STRING,
ATTRI TYPE STRING,
ATTR1 TYPE STRING,
ATTR2 TYPE STRING,
BP_STAT TYPE STRING,
TEMPLCLASS TYPE STRING,
TEMPLATE TYPE STRING,
TEMPL_BPP TYPE STRING,
KALSM TYPE STRING,
RNERFDT TYPE STRING,
ERFNM TYPE STRING,
AENDT TYPE STRING,
AENNM TYPE STRING,
LEINH TYPE STRING,
AUSEH TYPE STRING,
AUSFK TYPE STRING,
VKSTA TYPE STRING,
TARKZ TYPE STRING,
TARKZ_I TYPE STRING,
YRATE TYPE STRING,
LATYP TYPE STRING,
LATYPI TYPE STRING,
MANIST TYPE STRING,
MANPLAN TYPE STRING,
FIXVO TYPE STRING,
KZ TYPE STRING,END OF T_EKKO_STR. DATA: WA_KCMDTBP_ALL_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_KCMDTBP_ALL_STR-MANDT sy-vline
WA_KCMDTBP_ALL_STR-OBJNR sy-vline
WA_KCMDTBP_ALL_STR-DATBI sy-vline
WA_KCMDTBP_ALL_STR-STATUS sy-vline
WA_KCMDTBP_ALL_STR-DATAB sy-vline
WA_KCMDTBP_ALL_STR-LOGSYSTEM sy-vline
WA_KCMDTBP_ALL_STR-PHINR sy-vline
WA_KCMDTBP_ALL_STR-VERAP sy-vline
WA_KCMDTBP_ALL_STR-VERAP_USER sy-vline
WA_KCMDTBP_ALL_STR-OWAER sy-vline
WA_KCMDTBP_ALL_STR-ERSDA sy-vline
WA_KCMDTBP_ALL_STR-USNAM sy-vline
WA_KCMDTBP_ALL_STR-KTEXT sy-vline
WA_KCMDTBP_ALL_STR-LTEXT sy-vline
WA_KCMDTBP_ALL_STR-CSTDRV sy-vline
WA_KCMDTBP_ALL_STR-KOKRS sy-vline
WA_KCMDTBP_ALL_STR-PRZNR sy-vline
WA_KCMDTBP_ALL_STR-BUKRS sy-vline
WA_KCMDTBP_ALL_STR-GSBER sy-vline
WA_KCMDTBP_ALL_STR-PRCTR sy-vline
WA_KCMDTBP_ALL_STR-WERKS sy-vline
WA_KCMDTBP_ALL_STR-KOSTL sy-vline
WA_KCMDTBP_ALL_STR-KHINR sy-vline
WA_KCMDTBP_ALL_STR-VKORG sy-vline
WA_KCMDTBP_ALL_STR-VTWEG sy-vline
WA_KCMDTBP_ALL_STR-SPART sy-vline
WA_KCMDTBP_ALL_STR-PRART sy-vline
WA_KCMDTBP_ALL_STR-PRTYP sy-vline
WA_KCMDTBP_ALL_STR-PD_TASK_TP sy-vline
WA_KCMDTBP_ALL_STR-PD_TASK_ID sy-vline
WA_KCMDTBP_ALL_STR-WFD_ID sy-vline
WA_KCMDTBP_ALL_STR-VERSION sy-vline
WA_KCMDTBP_ALL_STR-EXETYP sy-vline
WA_KCMDTBP_ALL_STR-CBRKE sy-vline
WA_KCMDTBP_ALL_STR-CBRKI sy-vline
WA_KCMDTBP_ALL_STR-CBART sy-vline
WA_KCMDTBP_ALL_STR-DRVTP sy-vline
WA_KCMDTBP_ALL_STR-CBAT sy-vline
WA_KCMDTBP_ALL_STR-ATTRI sy-vline
WA_KCMDTBP_ALL_STR-ATTR1 sy-vline
WA_KCMDTBP_ALL_STR-ATTR2 sy-vline
WA_KCMDTBP_ALL_STR-BP_STAT sy-vline
WA_KCMDTBP_ALL_STR-TEMPLCLASS sy-vline
WA_KCMDTBP_ALL_STR-TEMPLATE sy-vline
WA_KCMDTBP_ALL_STR-TEMPL_BPP sy-vline
WA_KCMDTBP_ALL_STR-KALSM sy-vline
WA_KCMDTBP_ALL_STR-RNERFDT sy-vline
WA_KCMDTBP_ALL_STR-ERFNM sy-vline
WA_KCMDTBP_ALL_STR-AENDT sy-vline
WA_KCMDTBP_ALL_STR-AENNM sy-vline
WA_KCMDTBP_ALL_STR-LEINH sy-vline
WA_KCMDTBP_ALL_STR-AUSEH sy-vline
WA_KCMDTBP_ALL_STR-AUSFK sy-vline
WA_KCMDTBP_ALL_STR-VKSTA sy-vline
WA_KCMDTBP_ALL_STR-TARKZ sy-vline
WA_KCMDTBP_ALL_STR-TARKZ_I sy-vline
WA_KCMDTBP_ALL_STR-YRATE sy-vline
WA_KCMDTBP_ALL_STR-LATYP sy-vline
WA_KCMDTBP_ALL_STR-LATYPI sy-vline
WA_KCMDTBP_ALL_STR-MANIST sy-vline
WA_KCMDTBP_ALL_STR-MANPLAN sy-vline
WA_KCMDTBP_ALL_STR-FIXVO sy-vline
WA_KCMDTBP_ALL_STR-KZ sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.