Skip to Content

Overview:

If the data records of a internaltable of structured type or DB table has to be moved into a simple field of type C ,this cannot be done directly due to type conversion conflict. The field values should be individually handled in order to move it to the simple field taking into account of the field lengths and offsets.

This could come as a handy tool for various different purposes. One of the purpose it came handy for me was when i used it in few BADI’s (In SRM System) where i wanted to see what data is being passed into the BADI’s.In few scenarios it was not possible to use the debug mode as few of the calls to BADI’s are made through workflow and in some cases asynchronous.

Limitation :

This utility cannot handle nested or deep structured tables.

Scenarios:

  This utility function can handle three different scenarios to read the data from a DB table or an internal table and transfer to a file.

Scenario 1: When user wants to read and transfer the data from an internal table which has a structure referenced to a pre defined DDIC structure.

Scenario 2: When user wants to read and transfer the data from an internal table that is created by combining components of simple data types.

Scenario 3: When user wants to read and transfer the data from a DDIC table.

Technical Details:

The function Module would basically have the following Parameters:

Import Parameters: CASTING TYPE (ref_table).

  ENDIF.

  • ———————————————————-

  • find out about the structure of QUERY_TABLE

  • ———————————————————-

  IF NOT ref_table IS INITIAL.

    CALL FUNCTION ‘DDIF_FIELDINFO_GET’

      EXPORTING

        tabname        = ref_table

      IMPORTING

        ddobjtype      = table_type

      TABLES

        dfies_tab      = table_structure

      EXCEPTIONS

        not_found      = 1

        internal_error = 2

        OTHERS         = 3.

    IF sy-subrc <> 0.

    ENDIF.

    DELETE table_structure WHERE domname = ‘DUMMY’.

  ENDIF.

  • ———————————————————-

  • isolate first field of DATA as output field

  • (i.e. allow for changes to structure DATA!)

  • ———————————————————-

  ASSIGN COMPONENT 0 OF STRUCTURE data TO  LENGTH line_length IN CHARACTER MODE.

  • ———————————————————-

  • if FIELDS are not specified, read all available fields

  • ———————————————————-

  DESCRIBE TABLE fields LINES number_of_fields.

  IF number_of_fields = 0.

    LOOP AT table_structure.

      MOVE table_structure-fieldname TO fields-fieldname.

      APPEND fields.

    ENDLOOP.

  ENDIF.

  • ———————————————————-

  • for each field which has to be read, copy structure information

  • into tables FIELDS_INT (internal use) and FIELDS (output)

  • ———————————————————-

  line_cursor = 0.

  • for each field which has to be read …

  LOOP AT fields.

    READ TABLE table_structure WITH KEY fieldname = fields-fieldname.

  • compute the place for field contents in DATA rows:

  • if not first field in row, allow space for delimiter

    IF line_cursor <> 0.

      MOVE delimiter TO data+line_cursor.

      line_cursor = line_cursor + STRLEN( delimiter ).

    ENDIF.

  • … copy structure information into tables FIELDS_INT

  • (which is used internally during SELECT) …

    fields_int-fieldname  = table_structure-fieldname.

    fields_int-length_src = table_structure-intlen.

    IF table_structure-leng NE table_structure-outputlen.

      fields_int-length_dst = table_structure-outputlen.

    ELSE.

      fields_int-length_dst = table_structure-leng.

    ENDIF.

    fields_int-offset_src = table_structure-offset.

    fields_int-offset_dst = line_cursor.

    fields_int-type       = table_structure-inttype.

    fields_int-decimals   = table_structure-decimals.

  • compute the place for contents of next field in DATA rows

    line_cursor = line_cursor + fields_int-length_dst.

    APPEND fields_int.

  • … and into table FIELDS (which is output to the caller)

    fields-fieldtext = table_structure-fieldtext.

    fields-type      = table_structure-inttype.

    fields-length    = fields_int-length_dst.

    fields-offset    = fields_int-offset_dst.

    MODIFY fields.

  ENDLOOP.

  • end of loop at FIELDS

  IF NOT query_table IS INITIAL.

  • ———————————————————-

  • read data from the database and copy relevant portions into DATA

  • ———————————————————-

  • output data only if NO_DATA equals space (otherwise the structure

  • information in FIELDS is the only result of the module)

    SELECT * FROM (query_table) INTO  .

  •   copy all relevant fields into DATA (output) table

      LOOP AT fields_int.

        IF fields_int-type = ‘P’.

          ASSIGN COMPONENT fields_int-fieldname

              OF STRUCTURE +fields_int-offset_dst(fields_int-length_dst).

      ENDLOOP.

  •   end of loop at FIELDS_INT

      APPEND data.

    ENDSELECT.

  ELSE.

  • ———————————————————-

  • read data from the Internal table and copy relevant portions into

  • DATA

  • ———————————————————-

    LOOP AT itab INTO .

  •   copy all relevant fields into DATA (output) table

      LOOP AT fields_int.

        IF fields_int-type = ‘P’.

          ASSIGN COMPONENT fields_int-fieldname

              OF STRUCTURE +fields_int-offset_dst(fields_int-length_dst).

      ENDLOOP.

  •   end of loop at FIELDS_INT

      APPEND data.

    ENDLOOP.

  ENDIF.

  IF NOT keep IS INITIAL.

    OPEN DATASET ‘/tmp/rrr.txt’ FOR APPENDING  IN TEXT MODE ENCODING

                                DEFAULT.

  ELSE.

    OPEN DATASET ‘/tmp/rrr.txt’ FOR OUTPUT  IN TEXT MODE ENCODING

                                DEFAULT.

  ENDIF.

  LOOP AT data INTO data_wa.

    TRANSFER data_wa TO ‘/tmp/rrr.txt’ LENGTH l_len.

  ENDLOOP.

  CLOSE DATASET ‘/tmp/rrr.txt’.

Examples for different scenarios:

For Scenario 1:

Data: gt_itab type standard table of mara.

CALL FUNCTION ‘Z_FUNCTION’

EXPORTING

REF_TABLE = ‘MARA’

DELIMITER = ‘|’

TABLES

ITAB = gt_itab.

+For Scenario 2: +

If an internal table is defined without referring to an pre defined DDIC structure. The we would have to fill in the table parameter “FIELDS” with the field attributes of each field of the internal table like below.

Data: begin of gt_itab occurs 0,

zmatnr(10),

……..

………

end of gt_itab.

Data: gt_fields type standard table of rfc_db_fld,
gs_fields type rfc_db_fld.

gs_fields-fieldname = ‘ZMATNR’.
gs_fields-offset = ‘ ‘.
gs_fields-length = 10.
gs_fields-type = ‘C’
gs_fields-fieldtext = ‘Material’.
Append gs fields to gtfields.
…….
…….
Append gs fields to gtfields.

CALL FUNCTION ‘Z_FUNCTION’
EXPORTING
REF_TABLE = <br>DELIMITER = ‘|’ <br> KEEP =
TABLES
FIELDS = gt_fields
* DATA =
itab = gt_itab .

+For Scenario 3: +

CALL FUNCTION ‘Z_FUNCTION’
EXPORTING
QUERY_TABLE = ‘MARA’
REF_TABLE = ‘MARA’
DELIMITER = ‘|’
TABLES
* FIELDS =
* DATA = * itab =
                                   

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Ken G
    Raghu,

    Your post is almost an exact copy of function module, RFC_READ_TABLE.

    It does have limitations as well in a Unicode system.  Also, try working with table KONV (one that has a RAW field in it).  I don’t think the code will work.

    SAP_ken at comcast dot net

    (0) 

Leave a Reply