ABAP Select data from SAP table VRNG12 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 VRNG12 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 VRNG12. 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 VRNG12 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_VRNG12 TYPE STANDARD TABLE OF VRNG12,
      WA_VRNG12 TYPE VRNG12,
      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: <VRNG12> TYPE VRNG12.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM VRNG12
*  INTO TABLE @DATA(IT_VRNG122).
*--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_VRNG12 INDEX 1 INTO DATA(WA_VRNG122).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_VRNG12 ASSIGNING <VRNG12>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<VRNG12>-LFDNR = 1.
<VRNG12>-NUMKZ = 1.
<VRNG12>-NO_B_SEGM = 1.
<VRNG12>-MANDT = 1.
<VRNG12>-EINRI = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_VRNG12-KUNNR, sy-vline,
WA_VRNG12-PATKZ, sy-vline,
WA_VRNG12-BUKRS, sy-vline,
WA_VRNG12-KTOKD, sy-vline,
WA_VRNG12-NAME1, sy-vline,
WA_VRNG12-NAME2, sy-vline.
ENDLOOP. *Add any further fields from structure WA_VRNG12 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_VRNG12 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_VRNG12 INTO WA_VRNG12. *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_VRNG12-EINRI IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VRNG12-EINRI.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ISOLA, internal->external for field SPRAS CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_VRNG12-SPRAS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VRNG12-SPRAS.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

*Conversion exit ALPHA, internal->external for field PATNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VRNG12-PATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VRNG12-PATNR.
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_VRNG12_STR,
LFDNR TYPE STRING,
NUMKZ TYPE STRING,
NO_B_SEGM TYPE STRING,
MANDT TYPE STRING,
EINRI TYPE STRING,
KUNNR TYPE STRING,
PATKZ TYPE STRING,
BUKRS TYPE STRING,
KTOKD TYPE STRING,
NAME1 TYPE STRING,
NAME2 TYPE STRING,
NAME3 TYPE STRING,
ORT TYPE STRING,
PSTLZ TYPE STRING,
ORT2 TYPE STRING,
PFACH TYPE STRING,
PSTL2 TYPE STRING,
LAND TYPE STRING,
STRAS TYPE STRING,
TELF1 TYPE STRING,
TELFX TYPE STRING,
TELEX TYPE STRING,
SPRAS TYPE STRING,
SORTL TYPE STRING,
BANKS TYPE STRING,
BANKL TYPE STRING,
BANKN TYPE STRING,
XEZER TYPE STRING,
AKONT TYPE STRING,
ZUAWA TYPE STRING,
ZTERM TYPE STRING,
VTEXT TYPE STRING,
MAHNA TYPE STRING,
LANDX TYPE STRING,
BANAM TYPE STRING,
WAERS TYPE STRING,
BEGRS TYPE STRING,
ZWELS TYPE STRING,
BEGRF TYPE STRING,
HBKID TYPE STRING,
BUSAB_MA TYPE STRING,
BUSAB TYPE STRING,
TOGRU TYPE STRING,
SELBZ TYPE STRING,
MAHNA_2 TYPE STRING,
BUSAB_MA2 TYPE STRING,
MABER TYPE STRING,
MABER_2 TYPE STRING,
FDGRV TYPE STRING,
XZVER TYPE STRING,
ZAHLS TYPE STRING,
ANRED TYPE STRING,
TITEL TYPE STRING,
NAMZU TYPE STRING,
VORSW TYPE STRING,
MGRUP TYPE STRING,
BRSCH TYPE STRING,
ALTKN TYPE STRING,
ADRNR TYPE STRING,
ADROB TYPE STRING,
REGIO TYPE STRING,
STRAZ TYPE STRING,
EINZG TYPE STRING,
LANPF TYPE STRING,
ORTPF TYPE STRING,
PSTLF TYPE STRING,
TELXT TYPE STRING,
FAXXT TYPE STRING,
BUILD TYPE STRING,
FLOOR TYPE STRING,
ROOM TYPE STRING,
NAME4 TYPE STRING,
BKONT TYPE STRING,
BVTYP TYPE STRING,
BKREF TYPE STRING,
KOINH TYPE STRING,
STCD1 TYPE STRING,
STCD2 TYPE STRING,
KNRZA TYPE STRING,
VZSKZ TYPE STRING,
ZINDT TYPE STRING,
ZINRT TYPE STRING,
ZSABE TYPE STRING,
XAUSZ TYPE STRING,
XZEMP TYPE STRING,
PATNR TYPE STRING,
TELF2 TYPE STRING,
KONZS TYPE STRING,
COUNC TYPE STRING,
CITYC TYPE STRING,
SORT1 TYPE STRING,
SORT2 TYPE STRING,
HSNM1 TYPE STRING,
HSNM2 TYPE STRING,
CITY3 TYPE STRING,
TZONE TYPE STRING,
TXJCD TYPE STRING,
PERNR TYPE STRING,
CONAM TYPE STRING,
STREET2 TYPE STRING,
STREET3 TYPE STRING,
STKZA TYPE STRING,
STKZN TYPE STRING,
STKZU TYPE STRING,
ANREDE TYPE STRING,
TELNR TYPE STRING,
PO_BX_NUM TYPE STRING,
TITEL1 TYPE STRING,
IBAN TYPE STRING,
IBAN_DATE TYPE STRING,
KZ TYPE STRING,END OF T_EKKO_STR. DATA: WA_VRNG12_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_VRNG12_STR-LFDNR sy-vline
WA_VRNG12_STR-NUMKZ sy-vline
WA_VRNG12_STR-NO_B_SEGM sy-vline
WA_VRNG12_STR-MANDT sy-vline
WA_VRNG12_STR-EINRI sy-vline
WA_VRNG12_STR-KUNNR sy-vline
WA_VRNG12_STR-PATKZ sy-vline
WA_VRNG12_STR-BUKRS sy-vline
WA_VRNG12_STR-KTOKD sy-vline
WA_VRNG12_STR-NAME1 sy-vline
WA_VRNG12_STR-NAME2 sy-vline
WA_VRNG12_STR-NAME3 sy-vline
WA_VRNG12_STR-ORT sy-vline
WA_VRNG12_STR-PSTLZ sy-vline
WA_VRNG12_STR-ORT2 sy-vline
WA_VRNG12_STR-PFACH sy-vline
WA_VRNG12_STR-PSTL2 sy-vline
WA_VRNG12_STR-LAND sy-vline
WA_VRNG12_STR-STRAS sy-vline
WA_VRNG12_STR-TELF1 sy-vline
WA_VRNG12_STR-TELFX sy-vline
WA_VRNG12_STR-TELEX sy-vline
WA_VRNG12_STR-SPRAS sy-vline
WA_VRNG12_STR-SORTL sy-vline
WA_VRNG12_STR-BANKS sy-vline
WA_VRNG12_STR-BANKL sy-vline
WA_VRNG12_STR-BANKN sy-vline
WA_VRNG12_STR-XEZER sy-vline
WA_VRNG12_STR-AKONT sy-vline
WA_VRNG12_STR-ZUAWA sy-vline
WA_VRNG12_STR-ZTERM sy-vline
WA_VRNG12_STR-VTEXT sy-vline
WA_VRNG12_STR-MAHNA sy-vline
WA_VRNG12_STR-LANDX sy-vline
WA_VRNG12_STR-BANAM sy-vline
WA_VRNG12_STR-WAERS sy-vline
WA_VRNG12_STR-BEGRS sy-vline
WA_VRNG12_STR-ZWELS sy-vline
WA_VRNG12_STR-BEGRF sy-vline
WA_VRNG12_STR-HBKID sy-vline
WA_VRNG12_STR-BUSAB_MA sy-vline
WA_VRNG12_STR-BUSAB sy-vline
WA_VRNG12_STR-TOGRU sy-vline
WA_VRNG12_STR-SELBZ sy-vline
WA_VRNG12_STR-MAHNA_2 sy-vline
WA_VRNG12_STR-BUSAB_MA2 sy-vline
WA_VRNG12_STR-MABER sy-vline
WA_VRNG12_STR-MABER_2 sy-vline
WA_VRNG12_STR-FDGRV sy-vline
WA_VRNG12_STR-XZVER sy-vline
WA_VRNG12_STR-ZAHLS sy-vline
WA_VRNG12_STR-ANRED sy-vline
WA_VRNG12_STR-TITEL sy-vline
WA_VRNG12_STR-NAMZU sy-vline
WA_VRNG12_STR-VORSW sy-vline
WA_VRNG12_STR-MGRUP sy-vline
WA_VRNG12_STR-BRSCH sy-vline
WA_VRNG12_STR-ALTKN sy-vline
WA_VRNG12_STR-ADRNR sy-vline
WA_VRNG12_STR-ADROB sy-vline
WA_VRNG12_STR-REGIO sy-vline
WA_VRNG12_STR-STRAZ sy-vline
WA_VRNG12_STR-EINZG sy-vline
WA_VRNG12_STR-LANPF sy-vline
WA_VRNG12_STR-ORTPF sy-vline
WA_VRNG12_STR-PSTLF sy-vline
WA_VRNG12_STR-TELXT sy-vline
WA_VRNG12_STR-FAXXT sy-vline
WA_VRNG12_STR-BUILD sy-vline
WA_VRNG12_STR-FLOOR sy-vline
WA_VRNG12_STR-ROOM sy-vline
WA_VRNG12_STR-NAME4 sy-vline
WA_VRNG12_STR-BKONT sy-vline
WA_VRNG12_STR-BVTYP sy-vline
WA_VRNG12_STR-BKREF sy-vline
WA_VRNG12_STR-KOINH sy-vline
WA_VRNG12_STR-STCD1 sy-vline
WA_VRNG12_STR-STCD2 sy-vline
WA_VRNG12_STR-KNRZA sy-vline
WA_VRNG12_STR-VZSKZ sy-vline
WA_VRNG12_STR-ZINDT sy-vline
WA_VRNG12_STR-ZINRT sy-vline
WA_VRNG12_STR-ZSABE sy-vline
WA_VRNG12_STR-XAUSZ sy-vline
WA_VRNG12_STR-XZEMP sy-vline
WA_VRNG12_STR-PATNR sy-vline
WA_VRNG12_STR-TELF2 sy-vline
WA_VRNG12_STR-KONZS sy-vline
WA_VRNG12_STR-COUNC sy-vline
WA_VRNG12_STR-CITYC sy-vline
WA_VRNG12_STR-SORT1 sy-vline
WA_VRNG12_STR-SORT2 sy-vline
WA_VRNG12_STR-HSNM1 sy-vline
WA_VRNG12_STR-HSNM2 sy-vline
WA_VRNG12_STR-CITY3 sy-vline
WA_VRNG12_STR-TZONE sy-vline
WA_VRNG12_STR-TXJCD sy-vline
WA_VRNG12_STR-PERNR sy-vline
WA_VRNG12_STR-CONAM sy-vline
WA_VRNG12_STR-STREET2 sy-vline
WA_VRNG12_STR-STREET3 sy-vline
WA_VRNG12_STR-STKZA sy-vline
WA_VRNG12_STR-STKZN sy-vline
WA_VRNG12_STR-STKZU sy-vline
WA_VRNG12_STR-ANREDE sy-vline
WA_VRNG12_STR-TELNR sy-vline
WA_VRNG12_STR-PO_BX_NUM sy-vline
WA_VRNG12_STR-TITEL1 sy-vline
WA_VRNG12_STR-IBAN sy-vline
WA_VRNG12_STR-IBAN_DATE sy-vline
WA_VRNG12_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.