ABAP Select data from SAP table IMPR_FILE 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 IMPR_FILE 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 IMPR_FILE. 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 IMPR_FILE 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_IMPR_FILE TYPE STANDARD TABLE OF IMPR_FILE,
      WA_IMPR_FILE TYPE IMPR_FILE,
      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: <IMPR_FILE> TYPE IMPR_FILE.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM IMPR_FILE
*  INTO TABLE @DATA(IT_IMPR_FILE2).
*--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_IMPR_FILE INDEX 1 INTO DATA(WA_IMPR_FILE2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_IMPR_FILE ASSIGNING <IMPR_FILE>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<IMPR_FILE>-LEVEL = 1.
<IMPR_FILE>-POST1 = 1.
<IMPR_FILE>-MANDT = 1.
<IMPR_FILE>-POSNR = 1.
<IMPR_FILE>-GJAHR = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_IMPR_FILE-POSID, sy-vline,
WA_IMPR_FILE-ABJHR, sy-vline,
WA_IMPR_FILE-BIJHR, sy-vline,
WA_IMPR_FILE-PRNAM, sy-vline,
WA_IMPR_FILE-PPRIO, sy-vline,
WA_IMPR_FILE-IZWEK, sy-vline.
ENDLOOP. *Add any further fields from structure WA_IMPR_FILE 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_IMPR_FILE 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_IMPR_FILE INTO WA_IMPR_FILE. *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 POSNR, internal->external for field POSNR CALL FUNCTION 'CONVERSION_EXIT_POSNR_OUTPUT' EXPORTING input = WA_IMPR_FILE-POSNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMPR_FILE-POSNR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

*Conversion exit POSNR, internal->external for field PARNR CALL FUNCTION 'CONVERSION_EXIT_POSNR_OUTPUT' EXPORTING input = WA_IMPR_FILE-PARNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMPR_FILE-PARNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit POSNR, internal->external for field LEFNR CALL FUNCTION 'CONVERSION_EXIT_POSNR_OUTPUT' EXPORTING input = WA_IMPR_FILE-LEFNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMPR_FILE-LEFNR.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit CUNIT, internal->external for field USE04 CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_IMPR_FILE-USE04 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMPR_FILE-USE04.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field USE05 CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_IMPR_FILE-USE05 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMPR_FILE-USE05.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field FERC_IND CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IMPR_FILE-FERC_IND IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMPR_FILE-FERC_IND.
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_IMPR_FILE_STR,
LEVEL TYPE STRING,
POST1 TYPE STRING,
MANDT TYPE STRING,
POSNR TYPE STRING,
GJAHR TYPE STRING,
POSID TYPE STRING,
ABJHR TYPE STRING,
BIJHR TYPE STRING,
PRNAM TYPE STRING,
PPRIO TYPE STRING,
IZWEK TYPE STRING,
OBJNR TYPE STRING,
VERNR TYPE STRING,
VERNA TYPE STRING,
ERNAM TYPE STRING,
ERDAT TYPE STRING,
AENAM TYPE STRING,
AEDAT TYPE STRING,
ERGSO TYPE STRING,
BUKRS TYPE STRING,
GSBER TYPE STRING,
KOKRS TYPE STRING,
KOSTL TYPE STRING,
WERKS TYPE STRING,
PRCTR TYPE STRING,
BEBER TYPE STRING,
TPLNR TYPE STRING,
XPLAN TYPE STRING,
XBUDG TYPE STRING,
PARNR TYPE STRING,
LEFNR TYPE STRING,
XAKTB TYPE STRING,
LOGSYV TYPE STRING,
RCOMP TYPE STRING,
SIZECL TYPE STRING,
LAND1 TYPE STRING,
OWAERS TYPE STRING,
USR00 TYPE STRING,
USR01 TYPE STRING,
USR02 TYPE STRING,
USR03 TYPE STRING,
USR04 TYPE STRING,
USE04 TYPE STRING,
USR05 TYPE STRING,
USE05 TYPE STRING,
USR06 TYPE STRING,
USE06 TYPE STRING,
USR07 TYPE STRING,
USE07 TYPE STRING,
USR08 TYPE STRING,
USR09 TYPE STRING,
USR10 TYPE STRING,
USR11 TYPE STRING,
USR12 TYPE STRING,
USR13 TYPE STRING,
RHIER TYPE STRING,
FERC_IND TYPE STRING,END OF T_EKKO_STR. DATA: WA_IMPR_FILE_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_IMPR_FILE_STR-LEVEL sy-vline
WA_IMPR_FILE_STR-POST1 sy-vline
WA_IMPR_FILE_STR-MANDT sy-vline
WA_IMPR_FILE_STR-POSNR sy-vline
WA_IMPR_FILE_STR-GJAHR sy-vline
WA_IMPR_FILE_STR-POSID sy-vline
WA_IMPR_FILE_STR-ABJHR sy-vline
WA_IMPR_FILE_STR-BIJHR sy-vline
WA_IMPR_FILE_STR-PRNAM sy-vline
WA_IMPR_FILE_STR-PPRIO sy-vline
WA_IMPR_FILE_STR-IZWEK sy-vline
WA_IMPR_FILE_STR-OBJNR sy-vline
WA_IMPR_FILE_STR-VERNR sy-vline
WA_IMPR_FILE_STR-VERNA sy-vline
WA_IMPR_FILE_STR-ERNAM sy-vline
WA_IMPR_FILE_STR-ERDAT sy-vline
WA_IMPR_FILE_STR-AENAM sy-vline
WA_IMPR_FILE_STR-AEDAT sy-vline
WA_IMPR_FILE_STR-ERGSO sy-vline
WA_IMPR_FILE_STR-BUKRS sy-vline
WA_IMPR_FILE_STR-GSBER sy-vline
WA_IMPR_FILE_STR-KOKRS sy-vline
WA_IMPR_FILE_STR-KOSTL sy-vline
WA_IMPR_FILE_STR-WERKS sy-vline
WA_IMPR_FILE_STR-PRCTR sy-vline
WA_IMPR_FILE_STR-BEBER sy-vline
WA_IMPR_FILE_STR-TPLNR sy-vline
WA_IMPR_FILE_STR-XPLAN sy-vline
WA_IMPR_FILE_STR-XBUDG sy-vline
WA_IMPR_FILE_STR-PARNR sy-vline
WA_IMPR_FILE_STR-LEFNR sy-vline
WA_IMPR_FILE_STR-XAKTB sy-vline
WA_IMPR_FILE_STR-LOGSYV sy-vline
WA_IMPR_FILE_STR-RCOMP sy-vline
WA_IMPR_FILE_STR-SIZECL sy-vline
WA_IMPR_FILE_STR-LAND1 sy-vline
WA_IMPR_FILE_STR-OWAERS sy-vline
WA_IMPR_FILE_STR-USR00 sy-vline
WA_IMPR_FILE_STR-USR01 sy-vline
WA_IMPR_FILE_STR-USR02 sy-vline
WA_IMPR_FILE_STR-USR03 sy-vline
WA_IMPR_FILE_STR-USR04 sy-vline
WA_IMPR_FILE_STR-USE04 sy-vline
WA_IMPR_FILE_STR-USR05 sy-vline
WA_IMPR_FILE_STR-USE05 sy-vline
WA_IMPR_FILE_STR-USR06 sy-vline
WA_IMPR_FILE_STR-USE06 sy-vline
WA_IMPR_FILE_STR-USR07 sy-vline
WA_IMPR_FILE_STR-USE07 sy-vline
WA_IMPR_FILE_STR-USR08 sy-vline
WA_IMPR_FILE_STR-USR09 sy-vline
WA_IMPR_FILE_STR-USR10 sy-vline
WA_IMPR_FILE_STR-USR11 sy-vline
WA_IMPR_FILE_STR-USR12 sy-vline
WA_IMPR_FILE_STR-USR13 sy-vline
WA_IMPR_FILE_STR-RHIER sy-vline
WA_IMPR_FILE_STR-FERC_IND sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.