ABAP Select data from SAP table /SAPAPO/CD_TR_CD_TP 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 /SAPAPO/CD_TR_CD_TP 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 /SAPAPO/CD_TR_CD_TP. 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 /SAPAPO/CD_TR_CD_TP 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_/SAPAPO/CD_TR_CD_TP TYPE STANDARD TABLE OF /SAPAPO/CD_TR_CD_TP, WA_/SAPAPO/CD_TR_CD_TP TYPE /SAPAPO/CD_TR_CD_TP, 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: </SAPAPO/CD_TR_CD_TP> TYPE /SAPAPO/CD_TR_CD_TP. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_/SAPAPO/CD_TR_CD_TP. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM /SAPAPO/CD_TR_CD_TP INTO TABLE IT_/SAPAPO/CD_TR_CD_TP. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM /SAPAPO/CD_TR_CD_TP * INTO TABLE @DATA(IT_/SAPAPO/CD_TR_CD_TP2). *--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_/SAPAPO/CD_TR_CD_TP INDEX 1 INTO DATA(WA_/SAPAPO/CD_TR_CD_TP2). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_/SAPAPO/CD_TR_CD_TP ASSIGNING </SAPAPO/CD_TR_CD_TP>.*To update a field value using a field symbol simply change the value via the field symbol pointer
</SAPAPO/CD_TR_CD_TP>-MANDT = 1.
</SAPAPO/CD_TR_CD_TP>-TRNAME = 1.
</SAPAPO/CD_TR_CD_TP>-LOCNOFR = 1.
</SAPAPO/CD_TR_CD_TP>-LOCTYPEFR = 1.
</SAPAPO/CD_TR_CD_TP>-LOCNOTO = 1.
ENDLOOP. LOOP AT IT_/SAPAPO/CD_TR_CD_TP INTO WA_/SAPAPO/CD_TR_CD_TP. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_/SAPAPO/CD_TR_CD_TP-LOCTYPETO, sy-vline,
WA_/SAPAPO/CD_TR_CD_TP-MATNR, sy-vline,
WA_/SAPAPO/CD_TR_CD_TP-VALFR, sy-vline,
WA_/SAPAPO/CD_TR_CD_TP-VALTO, sy-vline,
WA_/SAPAPO/CD_TR_CD_TP-TDSRC, sy-vline,
WA_/SAPAPO/CD_TR_CD_TP-EBELN, sy-vline.
ENDLOOP. *Add any further fields from structure WA_/SAPAPO/CD_TR_CD_TP 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_/SAPAPO/CD_TR_CD_TP 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_/SAPAPO/CD_TR_CD_TP INTO WA_/SAPAPO/CD_TR_CD_TP. *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 MATNR CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-MATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-MATNR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit TSTRG, internal->external for field TGRPRT CALL FUNCTION 'CONVERSION_EXIT_TSTRG_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-TGRPRT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-TGRPRT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit TSTRG, internal->external for field TGIPRT CALL FUNCTION 'CONVERSION_EXIT_TSTRG_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-TGIPRT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-TGIPRT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit TSTRN, internal->external for field MANF_DURA CALL FUNCTION 'CONVERSION_EXIT_TSTRN_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-MANF_DURA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-MANF_DURA.
WRITE:/ 'New Value:', ld_input.
*Conversion exit TSTRN, internal->external for field TDD_LT_PULL CALL FUNCTION 'CONVERSION_EXIT_TSTRN_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-TDD_LT_PULL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-TDD_LT_PULL.
WRITE:/ 'New Value:', ld_input.
*Conversion exit TSTRN, internal->external for field TDD_LT_PUSH CALL FUNCTION 'CONVERSION_EXIT_TSTRN_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-TDD_LT_PUSH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-TDD_LT_PUSH.
WRITE:/ 'New Value:', ld_input.
*Conversion exit TSTRN, internal->external for field TSS_LT_PULL CALL FUNCTION 'CONVERSION_EXIT_TSTRN_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-TSS_LT_PULL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-TSS_LT_PULL.
WRITE:/ 'New Value:', ld_input.
*Conversion exit TSTRN, internal->external for field TSS_LT_PUSH CALL FUNCTION 'CONVERSION_EXIT_TSTRN_OUTPUT' EXPORTING input = WA_/SAPAPO/CD_TR_CD_TP-TSS_LT_PUSH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SAPAPO/CD_TR_CD_TP-TSS_LT_PUSH.
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_/SAPAPO/CD_TR_CD_TP_STR,
MANDT TYPE STRING,
TRNAME TYPE STRING,
LOCNOFR TYPE STRING,
LOCTYPEFR TYPE STRING,
LOCNOTO TYPE STRING,
LOCTYPETO TYPE STRING,
MATNR TYPE STRING,
VALFR TYPE STRING,
VALTO TYPE STRING,
TDSRC TYPE STRING,
EBELN TYPE STRING,
EBELP TYPE STRING,
EKORG TYPE STRING,
SUBCO TYPE STRING,
CONSI TYPE STRING,
TDSCO TYPE STRING,
VBELN TYPE STRING,
VBELP TYPE STRING,
VKORG TYPE STRING,
MATGRPTYP TYPE STRING,
MATGRPVAL TYPE STRING,
LOCFR TYPE STRING,
LOCTO TYPE STRING,
MATID TYPE STRING,
BZQID TYPE STRING,
SCOID TYPE STRING,
NAME TYPE STRING,
PPEGUID TYPE STRING,
PLANID TYPE STRING,
AMATID TYPE STRING,
BPRFID TYPE STRING,
TRPTYPE TYPE STRING,
BESKZ TYPE STRING,
PFLAG TYPE STRING,
ESTKZ TYPE STRING,
MINLS TYPE STRING,
MAXLS TYPE STRING,
PCOST TYPE STRING,
PCOSTID TYPE STRING,
SPRKZ TYPE STRING,
SPRKZ_CTRL TYPE STRING,
SPRIO TYPE STRING,
TGRFLG TYPE STRING,
TGRPRT TYPE STRING,
PROC_CAT TYPE STRING,
SRCFCS_R TYPE STRING,
DSFLG TYPE STRING,
DPRIO TYPE STRING,
RTOFLG TYPE STRING,
PDISC TYPE STRING,
PPMRNDPR TYPE STRING,
PREQ_COVER_TYPE TYPE STRING,
TGIPRT TYPE STRING,
SLSNR TYPE STRING,
BZQ_INT TYPE STRING,
PROCESSING_METH TYPE STRING,
PLN_HRZN TYPE STRING,
CTP_HRZN TYPE STRING,
FDR_HRZN TYPE STRING,
FOH_HRZN TYPE STRING,
MANF_DURA TYPE STRING,
TDD_CMP_LT_TRG TYPE STRING,
TDD_LT_PULL TYPE STRING,
TDD_LT_PUSH TYPE STRING,
TSS_LT_PULL TYPE STRING,
TSS_LT_PUSH TYPE STRING,
SSD_PRF_ID_I TYPE STRING,
SYNC_CAL_GR TYPE STRING,
DYN_FREEZE_HOR TYPE STRING,
ORDTYP_UNSPROD TYPE STRING,
PCOSTEX TYPE STRING,
KZ TYPE STRING,END OF T_EKKO_STR. DATA: WA_/SAPAPO/CD_TR_CD_TP_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_/SAPAPO/CD_TR_CD_TP_STR-MANDT sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TRNAME sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-LOCNOFR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-LOCTYPEFR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-LOCNOTO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-LOCTYPETO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-MATNR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-VALFR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-VALTO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TDSRC sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-EBELN sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-EBELP sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-EKORG sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SUBCO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-CONSI sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TDSCO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-VBELN sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-VBELP sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-VKORG sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-MATGRPTYP sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-MATGRPVAL sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-LOCFR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-LOCTO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-MATID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-BZQID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SCOID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-NAME sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PPEGUID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PLANID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-AMATID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-BPRFID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TRPTYPE sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-BESKZ sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PFLAG sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-ESTKZ sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-MINLS sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-MAXLS sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PCOST sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PCOSTID sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SPRKZ sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SPRKZ_CTRL sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SPRIO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TGRFLG sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TGRPRT sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PROC_CAT sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SRCFCS_R sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-DSFLG sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-DPRIO sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-RTOFLG sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PDISC sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PPMRNDPR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PREQ_COVER_TYPE sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TGIPRT sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SLSNR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-BZQ_INT sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PROCESSING_METH sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PLN_HRZN sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-CTP_HRZN sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-FDR_HRZN sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-FOH_HRZN sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-MANF_DURA sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TDD_CMP_LT_TRG sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TDD_LT_PULL sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TDD_LT_PUSH sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TSS_LT_PULL sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-TSS_LT_PUSH sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SSD_PRF_ID_I sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-SYNC_CAL_GR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-DYN_FREEZE_HOR sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-ORDTYP_UNSPROD sy-vline
WA_/SAPAPO/CD_TR_CD_TP_STR-PCOSTEX sy-vline
WA_/SAPAPO/CD_TR_CD_TP_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.