Problem solve Get help with specific problems with your technologies, process and projects.

Dynamic structure reading

Here's how to dynamically loop through the fields in a structure in an ABAP program.

Here's how to dynamically loop through the fields in a structure in an ABAP program, truncate them, and concatenate...

with delimiters into another field. The example I have provided will download the results into a text file. The goal was finding a solution for efficiently outputting multiple comma separated files (ie. multiple structures to download) from within one ABAP program. With this structure, you do not need to include anything from the dictionary. Your structure can be defined within the ABAP iself using TYPE statements. Look at the ABAP help pages for definitions of what the ASSIGN, DESCRIBE and ABAP statements are doing to understand how this is all achieved. The code below was written in R/3 4.6C.


REPORT yjmtip .

DATA: BEGIN OF v,
        string TYPE string,
        outfile LIKE rlgrap-filename VALUE 'c:tempexample.txt',
        n TYPE i,
        typ(1) TYPE c,
      END OF v.

DATA: my_itab LIKE usr01 OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF outdata_itab OCCURS 0,
        line(300).
DATA: END OF outdata_itab.

FIELD-SYMBOLS: <data_itab> TYPE STANDARD TABLE,
               <f>.

START-OF-SELECTION.
  SELECT *
  INTO CORRESPONDING FIELDS OF TABLE my_itab
  FROM usr01
  UP TO 20 ROWS.

END-OF-SELECTION.
  ASSIGN my_itab[] TO <data_itab>.
  PERFORM write_file USING v-outfile
                           my_itab.

*---------------------------------------------------------------------*
*       FORM write_file                                               *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  P_FILENAME                                                    *
*  -->  P_STRUCTURE                                                   *
*---------------------------------------------------------------------*
FORM write_file USING    p_filename
                         p_structure.

  CLEAR outdata_itab.
  REFRESH outdata_itab.
  LOOP AT <data_itab> INTO p_structure.  "xhead_itab.
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE p_structure TO <f>.
      IF sy-subrc <> 0. EXIT. ENDIF.
      DESCRIBE FIELD <f> TYPE v-typ COMPONENTS v-n.
      IF v-n EQ 0.
        IF sy-index = 1.
          CONCATENATE '"' <f> outdata_itab-line INTO outdata_itab-line.
        ELSE.
          CONCATENATE outdata_itab-line <f> INTO outdata_itab-line
                                            SEPARATED BY '","'.


        ENDIF.
      ENDIF.
    ENDDO.
    CONCATENATE outdata_itab-line '"' INTO outdata_itab-line.
    APPEND outdata_itab.
    WRITE:/ outdata_itab-line.
    CLEAR outdata_itab-line.
  ENDLOOP.
  CALL FUNCTION 'WS_DOWNLOAD'
       EXPORTING
            filename = p_filename
            filetype = 'ASC'
       TABLES
            data_tab = outdata_itab.
ENDFORM.  

This was last published in November 2002

Dig Deeper on SAP ABAP

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close