ABAP Select data from SAP table BIW_ASSET 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 BIW_ASSET 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 BIW_ASSET. 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 BIW_ASSET 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_BIW_ASSET TYPE STANDARD TABLE OF BIW_ASSET,
      WA_BIW_ASSET TYPE BIW_ASSET,
      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: <BIW_ASSET> TYPE BIW_ASSET.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BIW_ASSET
*  INTO TABLE @DATA(IT_BIW_ASSET2).
*--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_BIW_ASSET INDEX 1 INTO DATA(WA_BIW_ASSET2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BIW_ASSET ASSIGNING <BIW_ASSET>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BIW_ASSET>-MANDT = 1.
<BIW_ASSET>-BUKRS = 1.
<BIW_ASSET>-ANLN1 = 1.
<BIW_ASSET>-ANLN2 = 1.
<BIW_ASSET>-DATETO = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BIW_ASSET-DATEFROM, sy-vline,
WA_BIW_ASSET-ANLKL, sy-vline,
WA_BIW_ASSET-KOKRS, sy-vline,
WA_BIW_ASSET-KOSTL, sy-vline,
WA_BIW_ASSET-WERKS, sy-vline,
WA_BIW_ASSET-GSBER, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BIW_ASSET 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_BIW_ASSET 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_BIW_ASSET INTO WA_BIW_ASSET. *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 ANLN1 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BIW_ASSET-ANLN1 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BIW_ASSET-ANLN1.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

*Conversion exit GJAHR, internal->external for field ZUJHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_BIW_ASSET-ZUJHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BIW_ASSET-ZUJHR.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ABPSN, internal->external for field POSID CALL FUNCTION 'CONVERSION_EXIT_ABPSN_OUTPUT' EXPORTING input = WA_BIW_ASSET-POSID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BIW_ASSET-POSID.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit IMKEY, internal->external for field IMKEY CALL FUNCTION 'CONVERSION_EXIT_IMKEY_OUTPUT' EXPORTING input = WA_BIW_ASSET-IMKEY IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BIW_ASSET-IMKEY.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ABPSP, internal->external for field PS_PSP_PNR2 CALL FUNCTION 'CONVERSION_EXIT_ABPSP_OUTPUT' EXPORTING input = WA_BIW_ASSET-PS_PSP_PNR2 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BIW_ASSET-PS_PSP_PNR2.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ALPHA, internal->external for field KTOGR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BIW_ASSET-KTOGR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BIW_ASSET-KTOGR.
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_BIW_ASSET_STR,
MANDT TYPE STRING,
BUKRS TYPE STRING,
ANLN1 TYPE STRING,
ANLN2 TYPE STRING,
DATETO TYPE STRING,
DATEFROM TYPE STRING,
ANLKL TYPE STRING,
KOKRS TYPE STRING,
KOSTL TYPE STRING,
WERKS TYPE STRING,
GSBER TYPE STRING,
CAUFN TYPE STRING,
GPLAB TYPE STRING,
AKTIV TYPE STRING,
ZUPER TYPE STRING,
ZUJHR TYPE STRING,
ORD41 TYPE STRING,
ORD42 TYPE STRING,
ORD43 TYPE STRING,
ORD44 TYPE STRING,
ANLUE TYPE STRING,
EAUFN TYPE STRING,
GDLGRP TYPE STRING,
POSID TYPE STRING,
LSTAR TYPE STRING,
STORT TYPE STRING,
PERNR TYPE STRING,
KOSTLV TYPE STRING,
FISTL TYPE STRING,
GEBER TYPE STRING,
FKBER TYPE STRING,
GRANT_NBR TYPE STRING,
GEBER2 TYPE STRING,
FKBER2 TYPE STRING,
GRANT_NBR2 TYPE STRING,
FISTL2 TYPE STRING,
IMKEY TYPE STRING,
PS_PSP_PNR2 TYPE STRING,
BUDGET_PD TYPE STRING,
BUDGET_PD2 TYPE STRING,
SEGMENT TYPE STRING,
PRCTR TYPE STRING,
DEAKT TYPE STRING,
INKEN TYPE STRING,
ZUGDT TYPE STRING,
ERGSO TYPE STRING,
ERDAT TYPE STRING,
ERNAM TYPE STRING,
KTOGR TYPE STRING,END OF T_EKKO_STR. DATA: WA_BIW_ASSET_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_BIW_ASSET_STR-MANDT sy-vline
WA_BIW_ASSET_STR-BUKRS sy-vline
WA_BIW_ASSET_STR-ANLN1 sy-vline
WA_BIW_ASSET_STR-ANLN2 sy-vline
WA_BIW_ASSET_STR-DATETO sy-vline
WA_BIW_ASSET_STR-DATEFROM sy-vline
WA_BIW_ASSET_STR-ANLKL sy-vline
WA_BIW_ASSET_STR-KOKRS sy-vline
WA_BIW_ASSET_STR-KOSTL sy-vline
WA_BIW_ASSET_STR-WERKS sy-vline
WA_BIW_ASSET_STR-GSBER sy-vline
WA_BIW_ASSET_STR-CAUFN sy-vline
WA_BIW_ASSET_STR-GPLAB sy-vline
WA_BIW_ASSET_STR-AKTIV sy-vline
WA_BIW_ASSET_STR-ZUPER sy-vline
WA_BIW_ASSET_STR-ZUJHR sy-vline
WA_BIW_ASSET_STR-ORD41 sy-vline
WA_BIW_ASSET_STR-ORD42 sy-vline
WA_BIW_ASSET_STR-ORD43 sy-vline
WA_BIW_ASSET_STR-ORD44 sy-vline
WA_BIW_ASSET_STR-ANLUE sy-vline
WA_BIW_ASSET_STR-EAUFN sy-vline
WA_BIW_ASSET_STR-GDLGRP sy-vline
WA_BIW_ASSET_STR-POSID sy-vline
WA_BIW_ASSET_STR-LSTAR sy-vline
WA_BIW_ASSET_STR-STORT sy-vline
WA_BIW_ASSET_STR-PERNR sy-vline
WA_BIW_ASSET_STR-KOSTLV sy-vline
WA_BIW_ASSET_STR-FISTL sy-vline
WA_BIW_ASSET_STR-GEBER sy-vline
WA_BIW_ASSET_STR-FKBER sy-vline
WA_BIW_ASSET_STR-GRANT_NBR sy-vline
WA_BIW_ASSET_STR-GEBER2 sy-vline
WA_BIW_ASSET_STR-FKBER2 sy-vline
WA_BIW_ASSET_STR-GRANT_NBR2 sy-vline
WA_BIW_ASSET_STR-FISTL2 sy-vline
WA_BIW_ASSET_STR-IMKEY sy-vline
WA_BIW_ASSET_STR-PS_PSP_PNR2 sy-vline
WA_BIW_ASSET_STR-BUDGET_PD sy-vline
WA_BIW_ASSET_STR-BUDGET_PD2 sy-vline
WA_BIW_ASSET_STR-SEGMENT sy-vline
WA_BIW_ASSET_STR-PRCTR sy-vline
WA_BIW_ASSET_STR-DEAKT sy-vline
WA_BIW_ASSET_STR-INKEN sy-vline
WA_BIW_ASSET_STR-ZUGDT sy-vline
WA_BIW_ASSET_STR-ERGSO sy-vline
WA_BIW_ASSET_STR-ERDAT sy-vline
WA_BIW_ASSET_STR-ERNAM sy-vline
WA_BIW_ASSET_STR-KTOGR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.