Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Creating ITAB dynamically as per selection-Screen entry

NR_Ranasingh
Participant
0 Likes
2,791

Hello Guru's,

I am looking for option to achieve this requirement .

Requirement  - :  In My selection screen - In Select-Option , I have given 3 entries (** Dynamic - This can be any no of entries as per user  ) ,

                       

S_option
A1
A2
A3

Now i want to create 3 internal tables (as i have 3 entries ), with name ITAB_A1, ITAB _A2, ITAB_A3 .. so on (If more entries in Select-Option)

The Internal table will have same structure for all . (Ex- All ITAB will have the same structure of VIEW . *view used in below code )

Normally what we do  to define ITAB with dynamic reference is  -

{code

***## ----Dynamic Reference Internal Table .
  DATA: t_ref  TYPE REF TO data .

    FIELD-SYMBOLS: <t_itab>  TYPE    STANDARD TABLE ,    "ANY TABLE ,
                              <wa_itab> TYPE   any  .                       


     CREATE DATA t_ref TYPE TABLE OF (view) .
             ASSIGN : t_ref->* TO <t_itab> .

                 

Code}

in above code if i can create Field symbols dynamically, then its almost solved.

like below approach, though below code is not correct , i am looking for solution to this.

Loop at  s_option .

    Var = ITAB_A1 .                    **-- Var = conacatinating ITAb and values from S_Option-Low .
   FIELD-SYMBOLS:  <(Var)>  TYPE    STANDARD TABLE .

  

   Create Data  <(Var)>  type table of (view) .


End loop .

Please give your inputs /Suggestions .

Thanks all.

Regards

Nihar

11 REPLIES 11
Read only

Former Member
0 Likes
2,427

hi sagar,

I think you are missing something.

Can you have a look at the sample code:

DATA: w_tabname TYPE w_tabname,        
    w_dref TYPE REF TO data,  

  FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE.   

 

Loop at  s_option .

  w_tabname = s-option-low.   

  CREATE DATA w_dref TYPE TABLE OF (w_tabname). 

  ASSIGN w_dref->* TO <t_itab>.

endloop.

Read only

0 Likes
2,427

Hi Abdul,

A1, A2, A3 are not table name , these are different system name.

So this does nt meet the purpose.

*Loop at  s_option .

  *w_tabname = s-option-low.   

  *CREATE DATA w_dref TYPE TABLE OF (w_tabname). 

  *ASSIGN w_dref->* TO <t_itab>.

*endloop.


I want <t_itab> to be crated as per A1, A2...


Thanks

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
2,427

Try something like this:


   TYPES: BEGIN OF t_tab,

     name TYPE char2,

     lt_view TYPE TABLE OF view WITH DEFAULT KEY,

   END OF t_tab.

   DATA: lref_data TYPE REF TO data,

         lt_tab    TYPE TABLE OF t_tab.

   FIELD-SYMBOLS: <ls_tab> LIKE LINE OF lt_tab,

                  <lt_view> TYPE ANY TABLE.

   LOOP AT s_option.

     CREATE DATA lref_data TYPE TABLE OF view.

     ASSIGN lref_data->* TO <lt_view>.

     APPEND INITIAL LINE TO lt_tab ASSIGNING <ls_tab>.

     <ls_tab>-name = s_option-low.

     <ls_tab>-lt_view = <lt_view>.

   ENDLOOP.

-- Tomas --
Read only

0 Likes
2,427

Hi Tomas,

Thanks , I tried with your way. But at here, its not assigning the structure generated dynamically to  <ls_tab>-lt_view . Though <lt_view> has the dynamic generated structure .

   

*{Code

     <ls_tab>-name = s_option-low.

     <ls_tab>-lt_view = <lt_view>.


*Code}


Anyways thanks, i will try again modifying your code. Will let you know once done.

Read only

0 Likes
2,427

t_tab-lt_view and dynamic table in <lt_view> should have same structure

-- Tomas --
Read only

Former Member
0 Likes
2,427

I would say instead trying to have multiple field symbols referencing multiple table, create a single internal table of deep structure, where each line can hold one internal table then based on your seletc options create as many internal table you want and append into this complex  final table. This was you only need to create one field symbol.

so after :

  CREATE DATA t_ref TYPE TABLE OF (view) .
             ASSIGN : t_ref->* TO <t_itab> .


Append <t_itab> to the final table as a row and then clear tref & <t_itab> for next iteration.


your final table will be something like.


types: t_view type table of view.

types: begin of ty_final,

line type t_view,

end of ty_final.


I think if u get the idea you can proceed to code, if stuck let me know I'll try to give pseudo codes.

Read only

0 Likes
2,427

Kartik ,

Please provide sample code

Read only

0 Likes
2,427

Tell me how much you've tried, if you face issues or stuck anywhere I'll help you out. use the declaration I gave above to create a complex internal table.

you already mentioned how to created dynamic table, use the same method and then append this table - > <t_itab>  to the final internal table, then unassign the field symbol & proceed same way for others.

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
2,427

Hi,

If your requirement is to have same structure for all the tables, you can better have one internal table and differentiate it from others with a extra flag field which may hold 'A1', 'A2', 'A3' etc.,

Read only

0 Likes
2,427

Hi Jayanthi,

Thats the last option, if not got anything , then have to do like your approach.

Add all data of A1, A2,A3 into ITAB_FINAL , with a field mentioning A1 /A2/A3.

Then Move All A1 into ITAB_A1...

My Objective is to compare data of same table in Diff systems. Now i hope i am clear about the requirement .

Thanks Jayanthi

Read only

hirabd
Explorer
0 Likes
2,427

This document may be helpful for you.

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abapgroupby_clause.htm

Check this code.

PARAMETERS: p_tname TYPE tabname.
DATA: lr_tab TYPE REF TO data.
FIELD-SYMBOLS: <tab> TYPE ANY TABLE.

CREATE DATA lr_tab TYPE TABLE OF (p_tname).
ASSIGN lr_tab->* TO <tab>.
IF sy-subrc EQ 0.
  SELECT * FROM (p_tname) INTO TABLE <tab> UP TO 10 ROWS.
  cl_demo_output=>display( <tab> ).
ENDIF.