ABAP Select data from SAP table BAPITPSRC2X 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 BAPITPSRC2X 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 BAPITPSRC2X. 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 BAPITPSRC2X 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_BAPITPSRC2X TYPE STANDARD TABLE OF BAPITPSRC2X,
      WA_BAPITPSRC2X TYPE BAPITPSRC2X,
      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: <BAPITPSRC2X> TYPE BAPITPSRC2X.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPITPSRC2X
*  INTO TABLE @DATA(IT_BAPITPSRC2X2).
*--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_BAPITPSRC2X INDEX 1 INTO DATA(WA_BAPITPSRC2X2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPITPSRC2X ASSIGNING <BAPITPSRC2X>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPITPSRC2X>-PROCRELATIONID = 1.
<BAPITPSRC2X>-COUNTER = 1.
<BAPITPSRC2X>-PROC_LOGSYSTEM = 1.
<BAPITPSRC2X>-PUR_DOC_CAT = 1.
<BAPITPSRC2X>-PUR_DOC_NO = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPITPSRC2X-PUR_DOC_ITEM, sy-vline,
WA_BAPITPSRC2X-PURCH_ORG, sy-vline,
WA_BAPITPSRC2X-CONSIGNATION, sy-vline,
WA_BAPITPSRC2X-SUBCONTRACTING, sy-vline,
WA_BAPITPSRC2X-LOCATION_TO, sy-vline,
WA_BAPITPSRC2X-LOCATION_TO_TYPE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPITPSRC2X 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_BAPITPSRC2X 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_BAPITPSRC2X INTO WA_BAPITPSRC2X. *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_BAPITPSRC2X_STR,
PROCRELATIONID TYPE STRING,
COUNTER TYPE STRING,
PROC_LOGSYSTEM TYPE STRING,
PUR_DOC_CAT TYPE STRING,
PUR_DOC_NO TYPE STRING,
PUR_DOC_ITEM TYPE STRING,
PURCH_ORG TYPE STRING,
CONSIGNATION TYPE STRING,
SUBCONTRACTING TYPE STRING,
LOCATION_TO TYPE STRING,
LOCATION_TO_TYPE TYPE STRING,
PRODUCT_NUMBER TYPE STRING,
VENDOR TYPE STRING,
PLANT_FROM TYPE STRING,
PURCH_GROUP TYPE STRING,
VAL_FROM TYPE STRING,
VAL_TO TYPE STRING,
ACCOUNTING_LOGSYSTEM TYPE STRING,
ACCOUNTING_INDICATOR TYPE STRING,
ACCOUNTING_KEY TYPE STRING,
PROC_REL_ACTIVE TYPE STRING,
PLAN_DEL TYPE STRING,
USE_PLAN_DEL TYPE STRING,
PO_UNIT TYPE STRING,
PO_UNIT_ISO TYPE STRING,
BASE_UNIT TYPE STRING,
BASE_UNIT_ISO TYPE STRING,
NUMERATOR TYPE STRING,
DENOMINATOR TYPE STRING,
APO_COM TYPE STRING,
CREATION_PROF TYPE STRING,
LCREL TYPE STRING,
FORECAST TYPE STRING,
WEBRL TYPE STRING,
VENDOR_MATERIAL TYPE STRING,
UNLOADING_POINT TYPE STRING,
CONF_PROF TYPE STRING,
PROCESS_SCHED_AGR TYPE STRING,
RECONCILIATION_DATE TYPE STRING,
AGREED_CUMULATIVE_QUAN TYPE STRING,
FIRM_ZONE TYPE STRING,
TRADE_OFF_ZONE TYPE STRING,
DELIVERY_LOCATION_TO TYPE STRING,
PROC_PRIORITY TYPE STRING,
GOODS_RECEIPT_TIME TYPE STRING,
USE_GRPRT TYPE STRING,
STORAGELOC_TO TYPE STRING,
STORAGELOC_FROM TYPE STRING,
MEASGRSUP TYPE STRING,
UEBTO TYPE STRING,
UNTTO TYPE STRING,
SETUP_COST TYPE STRING,
PROCESSING_METHOD TYPE STRING,
MIN_LOT_SIZE TYPE STRING,
MAX_LOT_SIZE TYPE STRING,
ZBD1T TYPE STRING,
ZBD2T TYPE STRING,
ZBD3T TYPE STRING,
ZBD1P TYPE STRING,
ZBD2P TYPE STRING,
INCO1 TYPE STRING,
INCO2 TYPE STRING,
WAERS TYPE STRING,
WEBRE TYPE STRING,
XERSY TYPE STRING,
SKTOF TYPE STRING,
REPOS TYPE STRING,
NETPR TYPE STRING,
PEINH TYPE STRING,
BPRME TYPE STRING,
BPUMZ TYPE STRING,
BPUMN TYPE STRING,
LIFRE TYPE STRING,
TXJCD TYPE STRING,
EREKZ TYPE STRING,
MWSKZ TYPE STRING,
ZTERM TYPE STRING,
UEBTK TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPITPSRC2X_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_BAPITPSRC2X_STR-PROCRELATIONID sy-vline
WA_BAPITPSRC2X_STR-COUNTER sy-vline
WA_BAPITPSRC2X_STR-PROC_LOGSYSTEM sy-vline
WA_BAPITPSRC2X_STR-PUR_DOC_CAT sy-vline
WA_BAPITPSRC2X_STR-PUR_DOC_NO sy-vline
WA_BAPITPSRC2X_STR-PUR_DOC_ITEM sy-vline
WA_BAPITPSRC2X_STR-PURCH_ORG sy-vline
WA_BAPITPSRC2X_STR-CONSIGNATION sy-vline
WA_BAPITPSRC2X_STR-SUBCONTRACTING sy-vline
WA_BAPITPSRC2X_STR-LOCATION_TO sy-vline
WA_BAPITPSRC2X_STR-LOCATION_TO_TYPE sy-vline
WA_BAPITPSRC2X_STR-PRODUCT_NUMBER sy-vline
WA_BAPITPSRC2X_STR-VENDOR sy-vline
WA_BAPITPSRC2X_STR-PLANT_FROM sy-vline
WA_BAPITPSRC2X_STR-PURCH_GROUP sy-vline
WA_BAPITPSRC2X_STR-VAL_FROM sy-vline
WA_BAPITPSRC2X_STR-VAL_TO sy-vline
WA_BAPITPSRC2X_STR-ACCOUNTING_LOGSYSTEM sy-vline
WA_BAPITPSRC2X_STR-ACCOUNTING_INDICATOR sy-vline
WA_BAPITPSRC2X_STR-ACCOUNTING_KEY sy-vline
WA_BAPITPSRC2X_STR-PROC_REL_ACTIVE sy-vline
WA_BAPITPSRC2X_STR-PLAN_DEL sy-vline
WA_BAPITPSRC2X_STR-USE_PLAN_DEL sy-vline
WA_BAPITPSRC2X_STR-PO_UNIT sy-vline
WA_BAPITPSRC2X_STR-PO_UNIT_ISO sy-vline
WA_BAPITPSRC2X_STR-BASE_UNIT sy-vline
WA_BAPITPSRC2X_STR-BASE_UNIT_ISO sy-vline
WA_BAPITPSRC2X_STR-NUMERATOR sy-vline
WA_BAPITPSRC2X_STR-DENOMINATOR sy-vline
WA_BAPITPSRC2X_STR-APO_COM sy-vline
WA_BAPITPSRC2X_STR-CREATION_PROF sy-vline
WA_BAPITPSRC2X_STR-LCREL sy-vline
WA_BAPITPSRC2X_STR-FORECAST sy-vline
WA_BAPITPSRC2X_STR-WEBRL sy-vline
WA_BAPITPSRC2X_STR-VENDOR_MATERIAL sy-vline
WA_BAPITPSRC2X_STR-UNLOADING_POINT sy-vline
WA_BAPITPSRC2X_STR-CONF_PROF sy-vline
WA_BAPITPSRC2X_STR-PROCESS_SCHED_AGR sy-vline
WA_BAPITPSRC2X_STR-RECONCILIATION_DATE sy-vline
WA_BAPITPSRC2X_STR-AGREED_CUMULATIVE_QUAN sy-vline
WA_BAPITPSRC2X_STR-FIRM_ZONE sy-vline
WA_BAPITPSRC2X_STR-TRADE_OFF_ZONE sy-vline
WA_BAPITPSRC2X_STR-DELIVERY_LOCATION_TO sy-vline
WA_BAPITPSRC2X_STR-PROC_PRIORITY sy-vline
WA_BAPITPSRC2X_STR-GOODS_RECEIPT_TIME sy-vline
WA_BAPITPSRC2X_STR-USE_GRPRT sy-vline
WA_BAPITPSRC2X_STR-STORAGELOC_TO sy-vline
WA_BAPITPSRC2X_STR-STORAGELOC_FROM sy-vline
WA_BAPITPSRC2X_STR-MEASGRSUP sy-vline
WA_BAPITPSRC2X_STR-UEBTO sy-vline
WA_BAPITPSRC2X_STR-UNTTO sy-vline
WA_BAPITPSRC2X_STR-SETUP_COST sy-vline
WA_BAPITPSRC2X_STR-PROCESSING_METHOD sy-vline
WA_BAPITPSRC2X_STR-MIN_LOT_SIZE sy-vline
WA_BAPITPSRC2X_STR-MAX_LOT_SIZE sy-vline
WA_BAPITPSRC2X_STR-ZBD1T sy-vline
WA_BAPITPSRC2X_STR-ZBD2T sy-vline
WA_BAPITPSRC2X_STR-ZBD3T sy-vline
WA_BAPITPSRC2X_STR-ZBD1P sy-vline
WA_BAPITPSRC2X_STR-ZBD2P sy-vline
WA_BAPITPSRC2X_STR-INCO1 sy-vline
WA_BAPITPSRC2X_STR-INCO2 sy-vline
WA_BAPITPSRC2X_STR-WAERS sy-vline
WA_BAPITPSRC2X_STR-WEBRE sy-vline
WA_BAPITPSRC2X_STR-XERSY sy-vline
WA_BAPITPSRC2X_STR-SKTOF sy-vline
WA_BAPITPSRC2X_STR-REPOS sy-vline
WA_BAPITPSRC2X_STR-NETPR sy-vline
WA_BAPITPSRC2X_STR-PEINH sy-vline
WA_BAPITPSRC2X_STR-BPRME sy-vline
WA_BAPITPSRC2X_STR-BPUMZ sy-vline
WA_BAPITPSRC2X_STR-BPUMN sy-vline
WA_BAPITPSRC2X_STR-LIFRE sy-vline
WA_BAPITPSRC2X_STR-TXJCD sy-vline
WA_BAPITPSRC2X_STR-EREKZ sy-vline
WA_BAPITPSRC2X_STR-MWSKZ sy-vline
WA_BAPITPSRC2X_STR-ZTERM sy-vline
WA_BAPITPSRC2X_STR-UEBTK sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.