Today I had a requeriment which consisted in migrating specific tables from a SAP system to another. To get this done I’m passing a table which contains the tables names and receiving a table with the totality of records from these tables. This last table must be a generic string table in order to transport all kind of records. The idea was easy to conceive, but the problem came when I started coding. Dynamic programming can be a little bit tricky.

I had several dumps when trying to assign an specific record of a table, in the form of a field symbol, to the string field of my result table. Inmediately started looking through the forum to see if it was a way to achieve this and then realized the existence of the static method “cl_abap_container_utilities=>fill_container_c”. Yeah, I though I had this done, but actually, that didn’t happened. First, I tried this method with an only-character fields table and it worked great. Then, I tried the same with a larger table (mara). I thought the method was woking fine until I realized that the conversion was not that good. I found my string field had been filled with something like this: “1000000000000000002 #########3####d###…” and that’s because of the packed numbers type fields. Once again, I browsed the forum and found out that people was having the same problem with this method . But I still had my requeriment unfinished.

You need to work different if you want different results, paraphrasing Einstein, and that’s what I did. The following is just an example of how I made this work:

TYPES: BEGIN OF gty_string,

          str TYPE string,

        END OF gty_string.

DATA: gt_tab TYPE REF TO data,

       gs_lin TYPE REF TO data,

       g_field TYPE REF TO data,

       g_len     TYPE i,

       g_typ     TYPE c,

       g_chr     TYPE c LENGTH 4000,

       g_str_len TYPE i.

DATA: gt_string TYPE STANDARD TABLE OF gty_string.

FIELD-SYMBOLS: <fs_tab>     TYPE STANDARD TABLE,

                <fs_lin>     TYPE any,

                <fs_field>   TYPE any,

                <fs_field_c> TYPE any,

                <fs_str>     TYPE gty_string.

   CREATE DATA gt_tab TYPE STANDARD TABLE OF (‘mara’)

   ASSIGN gt_tab->* TO <fs_tab>.

   CREATE DATA gs_lin TYPE (‘mara’).

   ASSIGN gs_lin->* TO <fs_lin>.

   select *

     from mara

     into table <fs_tab>

     up to 1 rows.

LOOP AT <fs_tab> INTO <fs_lin>.

     APPEND INITIAL LINE TO gt_string ASSIGNING <fs_str>.                    ” result table

     DO.

       ASSIGN COMPONENT syindex OF STRUCTURE <fs_lin> TO <fs_field>.

       IF sysubrc NE 0.

         EXIT.

       ELSE.

         DESCRIBE FIELD <fs_field> OUTPUTLENGTH g_len.

         CREATE DATA g_field TYPE c LENGTH g_len.

         ASSIGN g_field->* TO <fs_field_c>.

         <fs_field_c> = <fs_field>.

         CONCATENATE g_chr <fs_field_c> ‘|’ INTO g_chr.                               ” g_chr is a long char (4096)

       ENDIF.

     ENDDO.

     g_aux = strlen( g_chr ) 1.                                                               

     clear g_chr+g_aux(1).                                                                           ” deleting last field delimiter ‘|’

     <fs_str>str = g_str.

ENDLOOP.

With that code you can have your dynamic records in an string typed table.

And, to fill your structured table with the string typed table you need to do the following, it’s basically the opposite:

LOOP AT gt_string ASSIGNING <fs_str>.

     CLEAR <fs_lin>.

     g_chr = <fs_str>str.

     DO.

       ASSIGN COMPONENT syindex OF STRUCTURE <fs_lin> TO <fs_field>.

       IF sysubrc NE 0.

         EXIT.

       ELSE.

         SPLIT g_chr AT ‘|’ INTO g_field_aux g_chr.

         <fs_field> = g_field_aux.

       ENDIF.

     ENDDO.

     APPEND <fs_lin> TO <fs_tab>.

   ENDLOOP.


I hope someone can find this useful.

To report this post you need to login first.

Be the first to leave a comment

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

Leave a Reply