ABAP Select data from SAP table PFNORM 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 PFNORM 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 PFNORM. 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 PFNORM 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_PFNORM TYPE STANDARD TABLE OF PFNORM,
      WA_PFNORM TYPE PFNORM,
      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: <PFNORM> TYPE PFNORM.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PFNORM
*  INTO TABLE @DATA(IT_PFNORM2).
*--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_PFNORM INDEX 1 INTO DATA(WA_PFNORM2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PFNORM ASSIGNING <PFNORM>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PFNORM>-EYEON = 1.
<PFNORM>-RECTYPE = 1.
<PFNORM>-NEXTREC = 1.
<PFNORM>-DISPCNT = 1.
<PFNORM>-DATE = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PFNORM-STARTTIME1, sy-vline,
WA_PFNORM-STARTTIME2, sy-vline,
WA_PFNORM-ENDTIME1, sy-vline,
WA_PFNORM-ENDTIME2, sy-vline,
WA_PFNORM-CPUTI, sy-vline,
WA_PFNORM-QUEUETI, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PFNORM 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_PFNORM 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_PFNORM INTO WA_PFNORM. *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.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_PFNORM_STR,
EYEON TYPE STRING,
RECTYPE TYPE STRING,
NEXTREC TYPE STRING,
DISPCNT TYPE STRING,
DATE TYPE STRING,
STARTTIME1 TYPE STRING,
STARTTIME2 TYPE STRING,
ENDTIME1 TYPE STRING,
ENDTIME2 TYPE STRING,
CPUTI TYPE STRING,
QUEUETI TYPE STRING,
V1SATI TYPE STRING,
WPID TYPE STRING,
TASKTYPE TYPE STRING,
TABLOAD TYPE STRING,
TCODE TYPE STRING,
TERMINALID TYPE STRING,
DYNPRONR TYPE STRING,
REPORT TYPE STRING,
ACCOUNT TYPE STRING,
MANDT TYPE STRING,
FILLER1 TYPE STRING,
ROLLKEY TYPE STRING,
INPUTLEN TYPE STRING,
OUTPUTLEN TYPE STRING,
MAXROLL TYPE STRING,
MAXPAGE TYPE STRING,
ROLLINCNT TYPE STRING,
ROLLINTI TYPE STRING,
ROLLOUTCNT TYPE STRING,
ROLLOUTTI TYPE STRING,
LOCKCNT TYPE STRING,
LOCKTI TYPE STRING,
TXXXCNT TYPE STRING,
TXXXTI TYPE STRING,
READDIRCNT TYPE STRING,
READDIRTI TYPE STRING,
READDIRREC TYPE STRING,
READDIRBUF TYPE STRING,
READSEQCNT TYPE STRING,
READSEQTI TYPE STRING,
READSEQREC TYPE STRING,
READSEQBUF TYPE STRING,
PHYREADCNT TYPE STRING,
INSCNT TYPE STRING,
INSTI TYPE STRING,
INSREC TYPE STRING,
PHYINSCNT TYPE STRING,
UPDCNT TYPE STRING,
UPDTI TYPE STRING,
UPDREC TYPE STRING,
PHYUPDCNT TYPE STRING,
DELCNT TYPE STRING,
DELTI TYPE STRING,
DELREC TYPE STRING,
PHYDELCNT TYPE STRING,
COMMITTI TYPE STRING,
GENERATETI TYPE STRING,
REPLOADTI TYPE STRING,
CUALOADTI TYPE STRING,
DYNPLOADTI TYPE STRING,
NTABCNT TYPE STRING,
DSQLCNT TYPE STRING,
ABAPSCNT TYPE STRING,
ABAPLCNT TYPE STRING,
DYNPSCNT TYPE STRING,
DYNPLCNT TYPE STRING,
CUALCNT TYPE STRING,
QUECNT TYPE STRING,
QUETI TYPE STRING,
DDICCNT TYPE STRING,
DDICTI TYPE STRING,
PRIVMODE TYPE STRING,
WPRESTART TYPE STRING,
FILLER2 TYPE STRING,
MCTI TYPE STRING,
MCREADCNT TYPE STRING,
MCUPDCNT TYPE STRING,
MCINSCNT TYPE STRING,
MCDELCNT TYPE STRING,
CUAPRG TYPE STRING,
CUAFUNC TYPE STRING,
MEMSUM TYPE STRING,
PRIVSUM TYPE STRING,
USEDBYTES TYPE STRING,
MAXBYTES TYPE STRING,
MAXBYTESDI TYPE STRING,
RFCCNT TYPE STRING,
RFCTI TYPE STRING,
ROLLED_OUT TYPE STRING,
EYEOFF TYPE STRING,END OF T_EKKO_STR. DATA: WA_PFNORM_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_PFNORM_STR-EYEON sy-vline
WA_PFNORM_STR-RECTYPE sy-vline
WA_PFNORM_STR-NEXTREC sy-vline
WA_PFNORM_STR-DISPCNT sy-vline
WA_PFNORM_STR-DATE sy-vline
WA_PFNORM_STR-STARTTIME1 sy-vline
WA_PFNORM_STR-STARTTIME2 sy-vline
WA_PFNORM_STR-ENDTIME1 sy-vline
WA_PFNORM_STR-ENDTIME2 sy-vline
WA_PFNORM_STR-CPUTI sy-vline
WA_PFNORM_STR-QUEUETI sy-vline
WA_PFNORM_STR-V1SATI sy-vline
WA_PFNORM_STR-WPID sy-vline
WA_PFNORM_STR-TASKTYPE sy-vline
WA_PFNORM_STR-TABLOAD sy-vline
WA_PFNORM_STR-TCODE sy-vline
WA_PFNORM_STR-TERMINALID sy-vline
WA_PFNORM_STR-DYNPRONR sy-vline
WA_PFNORM_STR-REPORT sy-vline
WA_PFNORM_STR-ACCOUNT sy-vline
WA_PFNORM_STR-MANDT sy-vline
WA_PFNORM_STR-FILLER1 sy-vline
WA_PFNORM_STR-ROLLKEY sy-vline
WA_PFNORM_STR-INPUTLEN sy-vline
WA_PFNORM_STR-OUTPUTLEN sy-vline
WA_PFNORM_STR-MAXROLL sy-vline
WA_PFNORM_STR-MAXPAGE sy-vline
WA_PFNORM_STR-ROLLINCNT sy-vline
WA_PFNORM_STR-ROLLINTI sy-vline
WA_PFNORM_STR-ROLLOUTCNT sy-vline
WA_PFNORM_STR-ROLLOUTTI sy-vline
WA_PFNORM_STR-LOCKCNT sy-vline
WA_PFNORM_STR-LOCKTI sy-vline
WA_PFNORM_STR-TXXXCNT sy-vline
WA_PFNORM_STR-TXXXTI sy-vline
WA_PFNORM_STR-READDIRCNT sy-vline
WA_PFNORM_STR-READDIRTI sy-vline
WA_PFNORM_STR-READDIRREC sy-vline
WA_PFNORM_STR-READDIRBUF sy-vline
WA_PFNORM_STR-READSEQCNT sy-vline
WA_PFNORM_STR-READSEQTI sy-vline
WA_PFNORM_STR-READSEQREC sy-vline
WA_PFNORM_STR-READSEQBUF sy-vline
WA_PFNORM_STR-PHYREADCNT sy-vline
WA_PFNORM_STR-INSCNT sy-vline
WA_PFNORM_STR-INSTI sy-vline
WA_PFNORM_STR-INSREC sy-vline
WA_PFNORM_STR-PHYINSCNT sy-vline
WA_PFNORM_STR-UPDCNT sy-vline
WA_PFNORM_STR-UPDTI sy-vline
WA_PFNORM_STR-UPDREC sy-vline
WA_PFNORM_STR-PHYUPDCNT sy-vline
WA_PFNORM_STR-DELCNT sy-vline
WA_PFNORM_STR-DELTI sy-vline
WA_PFNORM_STR-DELREC sy-vline
WA_PFNORM_STR-PHYDELCNT sy-vline
WA_PFNORM_STR-COMMITTI sy-vline
WA_PFNORM_STR-GENERATETI sy-vline
WA_PFNORM_STR-REPLOADTI sy-vline
WA_PFNORM_STR-CUALOADTI sy-vline
WA_PFNORM_STR-DYNPLOADTI sy-vline
WA_PFNORM_STR-NTABCNT sy-vline
WA_PFNORM_STR-DSQLCNT sy-vline
WA_PFNORM_STR-ABAPSCNT sy-vline
WA_PFNORM_STR-ABAPLCNT sy-vline
WA_PFNORM_STR-DYNPSCNT sy-vline
WA_PFNORM_STR-DYNPLCNT sy-vline
WA_PFNORM_STR-CUALCNT sy-vline
WA_PFNORM_STR-QUECNT sy-vline
WA_PFNORM_STR-QUETI sy-vline
WA_PFNORM_STR-DDICCNT sy-vline
WA_PFNORM_STR-DDICTI sy-vline
WA_PFNORM_STR-PRIVMODE sy-vline
WA_PFNORM_STR-WPRESTART sy-vline
WA_PFNORM_STR-FILLER2 sy-vline
WA_PFNORM_STR-MCTI sy-vline
WA_PFNORM_STR-MCREADCNT sy-vline
WA_PFNORM_STR-MCUPDCNT sy-vline
WA_PFNORM_STR-MCINSCNT sy-vline
WA_PFNORM_STR-MCDELCNT sy-vline
WA_PFNORM_STR-CUAPRG sy-vline
WA_PFNORM_STR-CUAFUNC sy-vline
WA_PFNORM_STR-MEMSUM sy-vline
WA_PFNORM_STR-PRIVSUM sy-vline
WA_PFNORM_STR-USEDBYTES sy-vline
WA_PFNORM_STR-MAXBYTES sy-vline
WA_PFNORM_STR-MAXBYTESDI sy-vline
WA_PFNORM_STR-RFCCNT sy-vline
WA_PFNORM_STR-RFCTI sy-vline
WA_PFNORM_STR-ROLLED_OUT sy-vline
WA_PFNORM_STR-EYEOFF sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.