ABAP Select data from SAP table BAPI10512ORDKEY 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 BAPI10512ORDKEY 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 BAPI10512ORDKEY. 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 BAPI10512ORDKEY 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_BAPI10512ORDKEY TYPE STANDARD TABLE OF BAPI10512ORDKEY,
      WA_BAPI10512ORDKEY TYPE BAPI10512ORDKEY,
      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: <BAPI10512ORDKEY> TYPE BAPI10512ORDKEY.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPI10512ORDKEY
*  INTO TABLE @DATA(IT_BAPI10512ORDKEY2).
*--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_BAPI10512ORDKEY INDEX 1 INTO DATA(WA_BAPI10512ORDKEY2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPI10512ORDKEY ASSIGNING <BAPI10512ORDKEY>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPI10512ORDKEY>-ORDER_ID22 = 1.
<BAPI10512ORDKEY>-ORDER_ID = 1.
<BAPI10512ORDKEY>-VRSIO_ID22 = 1.
<BAPI10512ORDKEY>-SIM_ID = 1.
<BAPI10512ORDKEY>-ORDTYPE = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPI10512ORDKEY-ORDNO, sy-vline,
WA_BAPI10512ORDKEY-APPLI, sy-vline,
WA_BAPI10512ORDKEY-UPDIN, sy-vline,
WA_BAPI10512ORDKEY-TRP_ID22, sy-vline,
WA_BAPI10512ORDKEY-TRP_ID, sy-vline,
WA_BAPI10512ORDKEY-TRPID_TYPE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPI10512ORDKEY 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_BAPI10512ORDKEY 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_BAPI10512ORDKEY INTO WA_BAPI10512ORDKEY. *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 TSTPS, internal->external for field TCCTMS CALL FUNCTION 'CONVERSION_EXIT_TSTPS_OUTPUT' EXPORTING input = WA_BAPI10512ORDKEY-TCCTMS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI10512ORDKEY-TCCTMS.
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_BAPI10512ORDKEY_STR,
ORDER_ID22 TYPE STRING,
ORDER_ID TYPE STRING,
VRSIO_ID22 TYPE STRING,
SIM_ID TYPE STRING,
ORDTYPE TYPE STRING,
ORDNO TYPE STRING,
APPLI TYPE STRING,
UPDIN TYPE STRING,
TRP_ID22 TYPE STRING,
TRP_ID TYPE STRING,
TRPID_TYPE TYPE STRING,
PRODUCABLE TYPE STRING,
OUTPUT_FIXED TYPE STRING,
INPUT_FIXED TYPE STRING,
PART_DELIVERED TYPE STRING,
FINAL_DELIVERY TYPE STRING,
UNPLANNED TYPE STRING,
RELEASED TYPE STRING,
DATE_FIXED TYPE STRING,
STARTED TYPE STRING,
IS_ISSUED TYPE STRING,
PART_CONFIRMED TYPE STRING,
FINAL_CONFIRMED TYPE STRING,
TTYPE TYPE STRING,
TPRIO TYPE STRING,
TR_ORDER_ID22 TYPE STRING,
TR_ORDER_ID TYPE STRING,
STRAT TYPE STRING,
LOCATION_ID22 TYPE STRING,
LOCATION_ID TYPE STRING,
OLTPTRANSFER TYPE STRING,
CNVIN TYPE STRING,
QTY_EFFECT_INVLD TYPE STRING,
IPPE_PPM_CHANGED TYPE STRING,
CTM_STATUS TYPE STRING,
ATP_ORDER_STATUS TYPE STRING,
TCCTMS TYPE STRING,
MES_STATUS TYPE STRING,
NOTEEXISTS TYPE STRING,
ORDERTYPE_STATUS TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPI10512ORDKEY_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_BAPI10512ORDKEY_STR-ORDER_ID22 sy-vline
WA_BAPI10512ORDKEY_STR-ORDER_ID sy-vline
WA_BAPI10512ORDKEY_STR-VRSIO_ID22 sy-vline
WA_BAPI10512ORDKEY_STR-SIM_ID sy-vline
WA_BAPI10512ORDKEY_STR-ORDTYPE sy-vline
WA_BAPI10512ORDKEY_STR-ORDNO sy-vline
WA_BAPI10512ORDKEY_STR-APPLI sy-vline
WA_BAPI10512ORDKEY_STR-UPDIN sy-vline
WA_BAPI10512ORDKEY_STR-TRP_ID22 sy-vline
WA_BAPI10512ORDKEY_STR-TRP_ID sy-vline
WA_BAPI10512ORDKEY_STR-TRPID_TYPE sy-vline
WA_BAPI10512ORDKEY_STR-PRODUCABLE sy-vline
WA_BAPI10512ORDKEY_STR-OUTPUT_FIXED sy-vline
WA_BAPI10512ORDKEY_STR-INPUT_FIXED sy-vline
WA_BAPI10512ORDKEY_STR-PART_DELIVERED sy-vline
WA_BAPI10512ORDKEY_STR-FINAL_DELIVERY sy-vline
WA_BAPI10512ORDKEY_STR-UNPLANNED sy-vline
WA_BAPI10512ORDKEY_STR-RELEASED sy-vline
WA_BAPI10512ORDKEY_STR-DATE_FIXED sy-vline
WA_BAPI10512ORDKEY_STR-STARTED sy-vline
WA_BAPI10512ORDKEY_STR-IS_ISSUED sy-vline
WA_BAPI10512ORDKEY_STR-PART_CONFIRMED sy-vline
WA_BAPI10512ORDKEY_STR-FINAL_CONFIRMED sy-vline
WA_BAPI10512ORDKEY_STR-TTYPE sy-vline
WA_BAPI10512ORDKEY_STR-TPRIO sy-vline
WA_BAPI10512ORDKEY_STR-TR_ORDER_ID22 sy-vline
WA_BAPI10512ORDKEY_STR-TR_ORDER_ID sy-vline
WA_BAPI10512ORDKEY_STR-STRAT sy-vline
WA_BAPI10512ORDKEY_STR-LOCATION_ID22 sy-vline
WA_BAPI10512ORDKEY_STR-LOCATION_ID sy-vline
WA_BAPI10512ORDKEY_STR-OLTPTRANSFER sy-vline
WA_BAPI10512ORDKEY_STR-CNVIN sy-vline
WA_BAPI10512ORDKEY_STR-QTY_EFFECT_INVLD sy-vline
WA_BAPI10512ORDKEY_STR-IPPE_PPM_CHANGED sy-vline
WA_BAPI10512ORDKEY_STR-CTM_STATUS sy-vline
WA_BAPI10512ORDKEY_STR-ATP_ORDER_STATUS sy-vline
WA_BAPI10512ORDKEY_STR-TCCTMS sy-vline
WA_BAPI10512ORDKEY_STR-MES_STATUS sy-vline
WA_BAPI10512ORDKEY_STR-NOTEEXISTS sy-vline
WA_BAPI10512ORDKEY_STR-ORDERTYPE_STATUS sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.