Skip to Content
author's profile photo Former Member
Former Member

Populating dynamic internal table using work area without loops

Hello,

I have a dynamic internal table and this is my code.

call method cl_alv_table_create=>create_dynamic_table
     exporting
       it_fieldcatalog = LT_FIELDCATALOG
     importing
       ep_table = <FS_DATA>
     exceptions
       generate_subpool_dir_full = 1
       others = 2
		.
if sy-subrc <> 0.
endif.

assign <FS_DATA>->* to <FS_1>.

create data NEW_LINE like line of <FS_1>.

assign NEW_LINE->*  to <FS_2>.

Now my problem is that i want to populate the internal table <FS_1> without using a loop. Something like populating the work-area <FS_2> one field at a time and then appending it to <FS_1>.

The biggest challenge here is that the fieldnames are not know until runtime. So, i cant use expressions like <FS_2>-field1.

If Field1 is my field name how do i write code to populate the internal table.

Please let me know if you need clarifications. I know that the explanation is not the best.

Thanks in advance

Mz

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 04, 2009 at 05:50 AM
    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 03, 2009 at 05:11 PM

    Hi

    U've all information about your fields in catalog table, so how to fill the dynamic table should depend on your source where u get the values to be transfered to the table:

     LT_FIELDCATALOG.
      ASSIGN COMPONENT LT_FIELDCATALOG-FIELDNAME OF STRUCTURE <FS_2> TO <FS_3>.
    
    * Here u need a logic in order to decid how to transfer the information:
      CASE  LT_FIELDCATALOG-FIELDNAME.
        WHEN 'FIELD1'.
           SELECT SINGLE * FROM <TABLE> WHERE .......
           IF SY-SUBRC EQ 0.
              <FS_3> = <TABLE>-FIELD1.
           ENDIF. 
         .......
       ENDCASE. 
    
    ENDLOOP.
    APPEND <FS_2> TO <FS_1>.

    Max

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 04, 2009 at 05:42 AM

    Thanks Max and Amit for your replies.

    Max,

    Your reply is actually very near to my requirement. But my problem is that i am not using select statements to fill up my internal table.

    Let me be a bit more precise.

    Let TAB be my internal table and WA be my work area (Both dynamic).......

    I want to fillup data in my internal table similar to the funcitonality given below.

    clear TAB.
    do 10 times.
    
    If (condiiton A).
    WA-Field1 = 'XYZ'. <--------------------------
    else.
    WA-Field1 = 'ABC'. <--------------------------------
    endif.
    
    append WA to TAB.
    
    
    enddo.
    

    Here the challenge is that Field1 is not know until runtime. So is there any statement i can use which has the same functionality as the above marked lines.

    The Statement Assign Component X of structure Y into Z assigns the field X of a structure Y into the Field Z. I want to do something like the opposite of this.

    Maybe a kind of assign Z to component X of structure Y.

    Sorry for the confusion created............ but that is exactly how i feel at the moment.

    Thanks in advance.

    Mz

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 03, 2009 at 04:52 PM

    Hi,

    The biggest challenge here is that the fieldnames are not know until runtime. So, i cant use expressions like <FS_2>-field1

    This answers your query. Your dynamic internal table is created during runtime.

    It as been declared as <fs> type ANYwhich implies that it doesnt hold any particular structure.

    When you try to move values using MOVE-CORRESPONDING in a loop, after assigning the reference through a pointer you would see that in DEBUG mode it will show you <FS_2>-field1 has a value.

    But if you try doing some like,

    MOVE <fs>-field1 to <FS_2>-field1.

    It will give you a dump.

    As far as I know, I have faced the same issue once and it didnt work out well.

    Regards,

    Amit

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 04, 2009 at 07:40 AM

    hi mz,

    you should have a look at rtts - run time type services in abap. With special system classes you can get infos about your data types during runtime. As far as I know you can get the structure of dynamic i.e. data types usually you don't know.

    (see i.e. cl_abap_typedescr=>describe_by_data().

    As far as I understand your problem this is what you need.

    Regards,

    Jens

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 04, 2009 at 08:45 AM

    Thanks Max and Suresh.....

    Suresh, the first document was particularly helpful.

    This is my final code.

    Data : lt_dyn_table TYPE REF TO data,
           lw_dyn_table TYPE REF TO data,
           lt_fieldcat type lvc_t_fcat,
           lw_fieldcat like line of lt_fieldcat.
    
    field-symbols : <FS_TABLE> type standard table,
                    <FS_WA> type ANY,
                    <FS_VARIABLE> type ANY.
    
    clear lt_fieldcat.
    lw_fieldcat-fieldname = 'CUST_TEAM'.
    lw_fieldcat-inttype = 'C'.
    lw_fieldcat-outputlen = '10'.
    lw_fieldcat-coltext = 'Customer Team'.
    lw_fieldcat-seltext = lw_fieldcat-coltext.
    append lw_fieldcat to lt_fieldcat.
    
    lw_fieldcat-fieldname = 'GRP'.
    lw_fieldcat-inttype = 'C'.
    lw_fieldcat-outputlen = '10'.
    lw_fieldcat-coltext = 'Category Group'.
    lw_fieldcat-seltext = lw_fieldcat-coltext.
    
    append lw_fieldcat to lt_fieldcat.
    
    CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
    it_fieldcatalog = lt_fieldcat
    IMPORTING
    ep_table = lt_dyn_table
    EXCEPTIONS
    generate_subpool_dir_full = 1
    OTHERS = 2.
    IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    
    Assign lt_dyn_table->* to <FS_TABLE>.
    
    create data lw_dyn_table like line of <FS_TABLE>.
    
    Assign lw_dyn_table->* to <FS_WA>.
    
    Assign component 'CUST_TEAM' of structure <FS_WA> to <FS_VARIABLE>.
    <FS_VARIABLE> = 'Team 1'.
    
    Assign component 'GRP' of structure <FS_WA> to <FS_VARIABLE>.
    <FS_VARIABLE> = 'GRP1'.
    
    append <FS_WA> to <FS_TABLE>.
    

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.