ABAP Select data from SAP table VN1CORDER 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 VN1CORDER 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 VN1CORDER. 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 VN1CORDER 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_VN1CORDER TYPE STANDARD TABLE OF VN1CORDER,
      WA_VN1CORDER TYPE VN1CORDER,
      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: <VN1CORDER> TYPE VN1CORDER.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM VN1CORDER
*  INTO TABLE @DATA(IT_VN1CORDER2).
*--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_VN1CORDER INDEX 1 INTO DATA(WA_VN1CORDER2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_VN1CORDER ASSIGNING <VN1CORDER>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<VN1CORDER>-MANDT = 1.
<VN1CORDER>-CORDERID = 1.
<VN1CORDER>-EINRI = 1.
<VN1CORDER>-REFTYP = 1.
<VN1CORDER>-PATNR = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_VN1CORDER-PAPID, sy-vline,
WA_VN1CORDER-ETRBY, sy-vline,
WA_VN1CORDER-ETROE, sy-vline,
WA_VN1CORDER-ETRGP, sy-vline,
WA_VN1CORDER-ORDDEP, sy-vline,
WA_VN1CORDER-ORDPRI, sy-vline.
ENDLOOP. *Add any further fields from structure WA_VN1CORDER 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_VN1CORDER 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_VN1CORDER INTO WA_VN1CORDER. *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 EINRI CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VN1CORDER-EINRI IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VN1CORDER-EINRI.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field ABSESN CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VN1CORDER-ABSESN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VN1CORDER-ABSESN.
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_VN1CORDER_STR,
MANDT TYPE STRING,
CORDERID TYPE STRING,
EINRI TYPE STRING,
REFTYP TYPE STRING,
PATNR TYPE STRING,
PAPID TYPE STRING,
ETRBY TYPE STRING,
ETROE TYPE STRING,
ETRGP TYPE STRING,
ORDDEP TYPE STRING,
ORDPRI TYPE STRING,
CORDTITLE TYPE STRING,
PRGNR TYPE STRING,
RCKRUF TYPE STRING,
KANAM TYPE STRING,
KALTX TYPE STRING,
FRAGE TYPE STRING,
FRLTX TYPE STRING,
SCHWKZ TYPE STRING,
SCHWO TYPE STRING,
IFG TYPE STRING,
RMCORD TYPE STRING,
RMLTX TYPE STRING,
WLPRI TYPE STRING,
WLTYP TYPE STRING,
WLREM TYPE STRING,
WLRDT TYPE STRING,
WLRRN TYPE STRING,
WLSTA TYPE STRING,
WLHSP TYPE STRING,
WLADT TYPE STRING,
ABSBDT TYPE STRING,
ABSEDT TYPE STRING,
ABSESN TYPE STRING,
ERDAT TYPE STRING,
ERTIM TYPE STRING,
ERUSR TYPE STRING,
UPDAT TYPE STRING,
UPTIM TYPE STRING,
UPUSR TYPE STRING,
STORN TYPE STRING,
STUSR TYPE STRING,
STDAT TYPE STRING,
STTIME TYPE STRING,
STOID TYPE STRING,
USER1 TYPE STRING,
USER2 TYPE STRING,
USER3 TYPE STRING,
USER4 TYPE STRING,
USER5 TYPE STRING,
USER6 TYPE STRING,
USER7 TYPE STRING,
CREATTYP TYPE STRING,
KZ TYPE STRING,END OF T_EKKO_STR. DATA: WA_VN1CORDER_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_VN1CORDER_STR-MANDT sy-vline
WA_VN1CORDER_STR-CORDERID sy-vline
WA_VN1CORDER_STR-EINRI sy-vline
WA_VN1CORDER_STR-REFTYP sy-vline
WA_VN1CORDER_STR-PATNR sy-vline
WA_VN1CORDER_STR-PAPID sy-vline
WA_VN1CORDER_STR-ETRBY sy-vline
WA_VN1CORDER_STR-ETROE sy-vline
WA_VN1CORDER_STR-ETRGP sy-vline
WA_VN1CORDER_STR-ORDDEP sy-vline
WA_VN1CORDER_STR-ORDPRI sy-vline
WA_VN1CORDER_STR-CORDTITLE sy-vline
WA_VN1CORDER_STR-PRGNR sy-vline
WA_VN1CORDER_STR-RCKRUF sy-vline
WA_VN1CORDER_STR-KANAM sy-vline
WA_VN1CORDER_STR-KALTX sy-vline
WA_VN1CORDER_STR-FRAGE sy-vline
WA_VN1CORDER_STR-FRLTX sy-vline
WA_VN1CORDER_STR-SCHWKZ sy-vline
WA_VN1CORDER_STR-SCHWO sy-vline
WA_VN1CORDER_STR-IFG sy-vline
WA_VN1CORDER_STR-RMCORD sy-vline
WA_VN1CORDER_STR-RMLTX sy-vline
WA_VN1CORDER_STR-WLPRI sy-vline
WA_VN1CORDER_STR-WLTYP sy-vline
WA_VN1CORDER_STR-WLREM sy-vline
WA_VN1CORDER_STR-WLRDT sy-vline
WA_VN1CORDER_STR-WLRRN sy-vline
WA_VN1CORDER_STR-WLSTA sy-vline
WA_VN1CORDER_STR-WLHSP sy-vline
WA_VN1CORDER_STR-WLADT sy-vline
WA_VN1CORDER_STR-ABSBDT sy-vline
WA_VN1CORDER_STR-ABSEDT sy-vline
WA_VN1CORDER_STR-ABSESN sy-vline
WA_VN1CORDER_STR-ERDAT sy-vline
WA_VN1CORDER_STR-ERTIM sy-vline
WA_VN1CORDER_STR-ERUSR sy-vline
WA_VN1CORDER_STR-UPDAT sy-vline
WA_VN1CORDER_STR-UPTIM sy-vline
WA_VN1CORDER_STR-UPUSR sy-vline
WA_VN1CORDER_STR-STORN sy-vline
WA_VN1CORDER_STR-STUSR sy-vline
WA_VN1CORDER_STR-STDAT sy-vline
WA_VN1CORDER_STR-STTIME sy-vline
WA_VN1CORDER_STR-STOID sy-vline
WA_VN1CORDER_STR-USER1 sy-vline
WA_VN1CORDER_STR-USER2 sy-vline
WA_VN1CORDER_STR-USER3 sy-vline
WA_VN1CORDER_STR-USER4 sy-vline
WA_VN1CORDER_STR-USER5 sy-vline
WA_VN1CORDER_STR-USER6 sy-vline
WA_VN1CORDER_STR-USER7 sy-vline
WA_VN1CORDER_STR-CREATTYP sy-vline
WA_VN1CORDER_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.