ABAP Select data from SAP table ANLCV 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 ANLCV 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 ANLCV. 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 ANLCV 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_ANLCV TYPE STANDARD TABLE OF ANLCV,
      WA_ANLCV TYPE ANLCV,
      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: <ANLCV> TYPE ANLCV.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM ANLCV
*  INTO TABLE @DATA(IT_ANLCV2).
*--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_ANLCV INDEX 1 INTO DATA(WA_ANLCV2).


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

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_ANLCV-AFABE, sy-vline,
WA_ANLCV-AFBLPE, sy-vline,
WA_ANLCV-AFBANZ, sy-vline,
WA_ANLCV-XAFAR, sy-vline,
WA_ANLCV-NDABJ, sy-vline,
WA_ANLCV-NDABP, sy-vline.
ENDLOOP. *Add any further fields from structure WA_ANLCV 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_ANLCV 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_ANLCV INTO WA_ANLCV. *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_ANLCV-ANLN1 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_ANLCV-ANLN1.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit GJAHR, internal->external for field GJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_ANLCV-GJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_ANLCV-GJAHR.
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_ANLCV_STR,
MANDT TYPE STRING,
BUKRS TYPE STRING,
ANLN1 TYPE STRING,
ANLN2 TYPE STRING,
GJAHR TYPE STRING,
AFABE TYPE STRING,
AFBLPE TYPE STRING,
AFBANZ TYPE STRING,
XAFAR TYPE STRING,
NDABJ TYPE STRING,
NDABP TYPE STRING,
ANDSJ TYPE STRING,
ANDSP TYPE STRING,
RSTCK TYPE STRING,
KANSW TYPE STRING,
KAUFW TYPE STRING,
KINVZ TYPE STRING,
KNAFA TYPE STRING,
KSAFA TYPE STRING,
KAAFA TYPE STRING,
KMAFA TYPE STRING,
KZINW TYPE STRING,
KAUFN TYPE STRING,
KANZA TYPE STRING,
KVOST TYPE STRING,
AUFWP TYPE STRING,
NAFAP TYPE STRING,
SAFAP TYPE STRING,
AAFAP TYPE STRING,
MAFAP TYPE STRING,
AUFNP TYPE STRING,
AUFWB TYPE STRING,
NAFAG TYPE STRING,
SAFAG TYPE STRING,
AAFAG TYPE STRING,
MAFAG TYPE STRING,
AUFNG TYPE STRING,
ANSWL TYPE STRING,
ABGAN TYPE STRING,
ANSAZ TYPE STRING,
AUFWM TYPE STRING,
INVZM TYPE STRING,
NAFAM TYPE STRING,
SAFAM TYPE STRING,
AAFAM TYPE STRING,
MAFAM TYPE STRING,
AUFNM TYPE STRING,
AUFWV TYPE STRING,
INVZV TYPE STRING,
NAFAV TYPE STRING,
SAFAV TYPE STRING,
AAFAV TYPE STRING,
MAFAV TYPE STRING,
AUFNV TYPE STRING,
AUFWL TYPE STRING,
INVZL TYPE STRING,
NAFAL TYPE STRING,
SAFAL TYPE STRING,
AAFAL TYPE STRING,
MAFAL TYPE STRING,
AUFNL TYPE STRING,
ANUPD TYPE STRING,
KSANS TYPE STRING,
SANSL TYPE STRING,
GJA_AUFW TYPE STRING,
GJE_AUFW TYPE STRING,
GEB_AUFWTG TYPE STRING,
GJA_KANSW TYPE STRING,
LFD_KANSW TYPE STRING,
GJE_KANSW TYPE STRING,
GJA_BCHWRT TYPE STRING,
LFD_BCHWRT TYPE STRING,
GJE_BCHWRT TYPE STRING,
GJE_ZUSCHR TYPE STRING,
GJE_BWGWB TYPE STRING,
GEB_ABSCHR TYPE STRING,
GEP_ABSCHR TYPE STRING,
NVG_ABSCHR TYPE STRING,
RVG_ABSCHR TYPE STRING,
RGP_ABSCHR TYPE STRING,
GJE_INVZ TYPE STRING,
BEST_GJA TYPE STRING,
BEST_BEW TYPE STRING,
BEST_LFD TYPE STRING,
BEST_GJE TYPE STRING,
ANSW_GJA TYPE STRING,
ANSW_BEW TYPE STRING,
ANSW_LFD TYPE STRING,
ANSW_GJE TYPE STRING,
INV_GJA TYPE STRING,
INV_BEW TYPE STRING,
INV_ANT TYPE STRING,
INV_LFD TYPE STRING,
INV_GJE TYPE STRING,
AUFW_GJA TYPE STRING,
AUFW_GEP TYPE STRING,
AUFW_GEB TYPE STRING,
AUFW_ANT TYPE STRING,
AUFW_LFD TYPE STRING,
AUFW_GJE TYPE STRING,
AUFW_LFDP TYPE STRING,
AUFW_LFDG TYPE STRING,
RUECK_GJA TYPE STRING,
RUECK_GEP TYPE STRING,
RUECK_GEB TYPE STRING,
RUECK_ZUS TYPE STRING,
RUECK_ANT TYPE STRING,
RUECK_LFD TYPE STRING,
RUECK_GJE TYPE STRING,
RUECK_LFDP TYPE STRING,
RUECK_LFDG TYPE STRING,
NAFA_GJA TYPE STRING,
NAFA_GEP TYPE STRING,
NAFA_GEB TYPE STRING,
NAFA_ZUS TYPE STRING,
NAFA_ANT TYPE STRING,
NAFA_LFD TYPE STRING,
NAFA_GJE TYPE STRING,
NAFA_LFDP TYPE STRING,
NAFA_LFDG TYPE STRING,
RAFA_GJA TYPE STRING,
RAFA_GEP TYPE STRING,
RAFA_GEB TYPE STRING,
RAFA_ZUS TYPE STRING,
RAFA_ANT TYPE STRING,
RAFA_LFD TYPE STRING,
RAFA_GJE TYPE STRING,
RAFA_LFDP TYPE STRING,
RAFA_LFDG TYPE STRING,
AFA_GJA TYPE STRING,
AFA_GEP TYPE STRING,
AFA_GEB TYPE STRING,
AFA_ZUS TYPE STRING,
AFA_ANT TYPE STRING,
AFA_LFD TYPE STRING,
AFA_GJE TYPE STRING,
AFA_LFDP TYPE STRING,
AFA_LFDG TYPE STRING,
SZINS_GJA TYPE STRING,
SZINS_GEP TYPE STRING,
SZINS_GEB TYPE STRING,
SZINS_LFD TYPE STRING,
SZINS_GJE TYPE STRING,
SZINS_LFDP TYPE STRING,
SZINS_LFDG TYPE STRING,
BACKL_GJA TYPE STRING,
BACKL_GEP TYPE STRING,
BACKL_GEB TYPE STRING,
BACKL_LFD TYPE STRING,
BACKL_GJE TYPE STRING,
BACKL_LFDG TYPE STRING,
BACKL_LFDP TYPE STRING,
BCHWRT_GJA TYPE STRING,
BCHWRT_LFD TYPE STRING,
BCHWRT_GJE TYPE STRING,END OF T_EKKO_STR. DATA: WA_ANLCV_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_ANLCV_STR-MANDT sy-vline
WA_ANLCV_STR-BUKRS sy-vline
WA_ANLCV_STR-ANLN1 sy-vline
WA_ANLCV_STR-ANLN2 sy-vline
WA_ANLCV_STR-GJAHR sy-vline
WA_ANLCV_STR-AFABE sy-vline
WA_ANLCV_STR-AFBLPE sy-vline
WA_ANLCV_STR-AFBANZ sy-vline
WA_ANLCV_STR-XAFAR sy-vline
WA_ANLCV_STR-NDABJ sy-vline
WA_ANLCV_STR-NDABP sy-vline
WA_ANLCV_STR-ANDSJ sy-vline
WA_ANLCV_STR-ANDSP sy-vline
WA_ANLCV_STR-RSTCK sy-vline
WA_ANLCV_STR-KANSW sy-vline
WA_ANLCV_STR-KAUFW sy-vline
WA_ANLCV_STR-KINVZ sy-vline
WA_ANLCV_STR-KNAFA sy-vline
WA_ANLCV_STR-KSAFA sy-vline
WA_ANLCV_STR-KAAFA sy-vline
WA_ANLCV_STR-KMAFA sy-vline
WA_ANLCV_STR-KZINW sy-vline
WA_ANLCV_STR-KAUFN sy-vline
WA_ANLCV_STR-KANZA sy-vline
WA_ANLCV_STR-KVOST sy-vline
WA_ANLCV_STR-AUFWP sy-vline
WA_ANLCV_STR-NAFAP sy-vline
WA_ANLCV_STR-SAFAP sy-vline
WA_ANLCV_STR-AAFAP sy-vline
WA_ANLCV_STR-MAFAP sy-vline
WA_ANLCV_STR-AUFNP sy-vline
WA_ANLCV_STR-AUFWB sy-vline
WA_ANLCV_STR-NAFAG sy-vline
WA_ANLCV_STR-SAFAG sy-vline
WA_ANLCV_STR-AAFAG sy-vline
WA_ANLCV_STR-MAFAG sy-vline
WA_ANLCV_STR-AUFNG sy-vline
WA_ANLCV_STR-ANSWL sy-vline
WA_ANLCV_STR-ABGAN sy-vline
WA_ANLCV_STR-ANSAZ sy-vline
WA_ANLCV_STR-AUFWM sy-vline
WA_ANLCV_STR-INVZM sy-vline
WA_ANLCV_STR-NAFAM sy-vline
WA_ANLCV_STR-SAFAM sy-vline
WA_ANLCV_STR-AAFAM sy-vline
WA_ANLCV_STR-MAFAM sy-vline
WA_ANLCV_STR-AUFNM sy-vline
WA_ANLCV_STR-AUFWV sy-vline
WA_ANLCV_STR-INVZV sy-vline
WA_ANLCV_STR-NAFAV sy-vline
WA_ANLCV_STR-SAFAV sy-vline
WA_ANLCV_STR-AAFAV sy-vline
WA_ANLCV_STR-MAFAV sy-vline
WA_ANLCV_STR-AUFNV sy-vline
WA_ANLCV_STR-AUFWL sy-vline
WA_ANLCV_STR-INVZL sy-vline
WA_ANLCV_STR-NAFAL sy-vline
WA_ANLCV_STR-SAFAL sy-vline
WA_ANLCV_STR-AAFAL sy-vline
WA_ANLCV_STR-MAFAL sy-vline
WA_ANLCV_STR-AUFNL sy-vline
WA_ANLCV_STR-ANUPD sy-vline
WA_ANLCV_STR-KSANS sy-vline
WA_ANLCV_STR-SANSL sy-vline
WA_ANLCV_STR-GJA_AUFW sy-vline
WA_ANLCV_STR-GJE_AUFW sy-vline
WA_ANLCV_STR-GEB_AUFWTG sy-vline
WA_ANLCV_STR-GJA_KANSW sy-vline
WA_ANLCV_STR-LFD_KANSW sy-vline
WA_ANLCV_STR-GJE_KANSW sy-vline
WA_ANLCV_STR-GJA_BCHWRT sy-vline
WA_ANLCV_STR-LFD_BCHWRT sy-vline
WA_ANLCV_STR-GJE_BCHWRT sy-vline
WA_ANLCV_STR-GJE_ZUSCHR sy-vline
WA_ANLCV_STR-GJE_BWGWB sy-vline
WA_ANLCV_STR-GEB_ABSCHR sy-vline
WA_ANLCV_STR-GEP_ABSCHR sy-vline
WA_ANLCV_STR-NVG_ABSCHR sy-vline
WA_ANLCV_STR-RVG_ABSCHR sy-vline
WA_ANLCV_STR-RGP_ABSCHR sy-vline
WA_ANLCV_STR-GJE_INVZ sy-vline
WA_ANLCV_STR-BEST_GJA sy-vline
WA_ANLCV_STR-BEST_BEW sy-vline
WA_ANLCV_STR-BEST_LFD sy-vline
WA_ANLCV_STR-BEST_GJE sy-vline
WA_ANLCV_STR-ANSW_GJA sy-vline
WA_ANLCV_STR-ANSW_BEW sy-vline
WA_ANLCV_STR-ANSW_LFD sy-vline
WA_ANLCV_STR-ANSW_GJE sy-vline
WA_ANLCV_STR-INV_GJA sy-vline
WA_ANLCV_STR-INV_BEW sy-vline
WA_ANLCV_STR-INV_ANT sy-vline
WA_ANLCV_STR-INV_LFD sy-vline
WA_ANLCV_STR-INV_GJE sy-vline
WA_ANLCV_STR-AUFW_GJA sy-vline
WA_ANLCV_STR-AUFW_GEP sy-vline
WA_ANLCV_STR-AUFW_GEB sy-vline
WA_ANLCV_STR-AUFW_ANT sy-vline
WA_ANLCV_STR-AUFW_LFD sy-vline
WA_ANLCV_STR-AUFW_GJE sy-vline
WA_ANLCV_STR-AUFW_LFDP sy-vline
WA_ANLCV_STR-AUFW_LFDG sy-vline
WA_ANLCV_STR-RUECK_GJA sy-vline
WA_ANLCV_STR-RUECK_GEP sy-vline
WA_ANLCV_STR-RUECK_GEB sy-vline
WA_ANLCV_STR-RUECK_ZUS sy-vline
WA_ANLCV_STR-RUECK_ANT sy-vline
WA_ANLCV_STR-RUECK_LFD sy-vline
WA_ANLCV_STR-RUECK_GJE sy-vline
WA_ANLCV_STR-RUECK_LFDP sy-vline
WA_ANLCV_STR-RUECK_LFDG sy-vline
WA_ANLCV_STR-NAFA_GJA sy-vline
WA_ANLCV_STR-NAFA_GEP sy-vline
WA_ANLCV_STR-NAFA_GEB sy-vline
WA_ANLCV_STR-NAFA_ZUS sy-vline
WA_ANLCV_STR-NAFA_ANT sy-vline
WA_ANLCV_STR-NAFA_LFD sy-vline
WA_ANLCV_STR-NAFA_GJE sy-vline
WA_ANLCV_STR-NAFA_LFDP sy-vline
WA_ANLCV_STR-NAFA_LFDG sy-vline
WA_ANLCV_STR-RAFA_GJA sy-vline
WA_ANLCV_STR-RAFA_GEP sy-vline
WA_ANLCV_STR-RAFA_GEB sy-vline
WA_ANLCV_STR-RAFA_ZUS sy-vline
WA_ANLCV_STR-RAFA_ANT sy-vline
WA_ANLCV_STR-RAFA_LFD sy-vline
WA_ANLCV_STR-RAFA_GJE sy-vline
WA_ANLCV_STR-RAFA_LFDP sy-vline
WA_ANLCV_STR-RAFA_LFDG sy-vline
WA_ANLCV_STR-AFA_GJA sy-vline
WA_ANLCV_STR-AFA_GEP sy-vline
WA_ANLCV_STR-AFA_GEB sy-vline
WA_ANLCV_STR-AFA_ZUS sy-vline
WA_ANLCV_STR-AFA_ANT sy-vline
WA_ANLCV_STR-AFA_LFD sy-vline
WA_ANLCV_STR-AFA_GJE sy-vline
WA_ANLCV_STR-AFA_LFDP sy-vline
WA_ANLCV_STR-AFA_LFDG sy-vline
WA_ANLCV_STR-SZINS_GJA sy-vline
WA_ANLCV_STR-SZINS_GEP sy-vline
WA_ANLCV_STR-SZINS_GEB sy-vline
WA_ANLCV_STR-SZINS_LFD sy-vline
WA_ANLCV_STR-SZINS_GJE sy-vline
WA_ANLCV_STR-SZINS_LFDP sy-vline
WA_ANLCV_STR-SZINS_LFDG sy-vline
WA_ANLCV_STR-BACKL_GJA sy-vline
WA_ANLCV_STR-BACKL_GEP sy-vline
WA_ANLCV_STR-BACKL_GEB sy-vline
WA_ANLCV_STR-BACKL_LFD sy-vline
WA_ANLCV_STR-BACKL_GJE sy-vline
WA_ANLCV_STR-BACKL_LFDG sy-vline
WA_ANLCV_STR-BACKL_LFDP sy-vline
WA_ANLCV_STR-BCHWRT_GJA sy-vline
WA_ANLCV_STR-BCHWRT_LFD sy-vline
WA_ANLCV_STR-BCHWRT_GJE sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.