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

How to Create Dynamic Itab.

former_member226225
Contributor
0 Likes
929

Hi Experts,

I want to create Multiple itabs based on User Selection 

TYPES : BEGIN OF ty_mara,

              matnr TYPE mara-matnr,

              END OF ty_mara.

DATA : lr_data TYPE REF TO data,

           lv_str(20TYPE c.

PARAMETERS : p_num TYPE n.     

FIELD-SYMBOLS : <lt_mara> TYPE STANDARD TABLE,

                             <fs> TYPE any.

CREATE DATA lr_data  TYPE STANDARD TABLE OF ty_mara.

ASSIGN lr_data->* TO <lt_mara>.

DO p_num TIMES.

   CONCATENATE '<lt_mara' p_num '>' INTO lv_str.

   ASSIGN lr_data->* to (lv_str)

ENDDO.

But is Showing an error that lv_str is Unknown . How to Create Multiple Itabs Based on User Selection .

Please Provide if there is any alternative way.

Thanks & Regards,

Raghunadh.K

Hi Experts,

I want to create Multiple itabs based on User Selection 

TYPES : BEGIN OF ty_mara,

              matnr TYPE mara-matnr,

              END OF ty_mara.

DATA : lr_data TYPE REF TO data,

           lv_str(20TYPE c.

PARAMETERS : p_num TYPE n.     

FIELD-SYMBOLS : <lt_mara> TYPE STANDARD TABLE,

                             <fs> TYPE any.

CREATE DATA lr_data  TYPE STANDARD TABLE OF ty_mara.

ASSIGN lr_data->* TO <lt_mara>.

DO p_num TIMES.

   CONCATENATE '<lt_mara' p_num '>' INTO lv_str.

   ASSIGN lr_data->* to (lv_str)

ENDDO.

But is Showing an error that lv_str is Unknown . How to Create Multiple Itabs Based on User Selection .

Please Provide if there is any alternative way.

Thanks & Regards,

Raghunadh.K

5 REPLIES 5
Read only

Former Member
0 Likes
827

Hi Raghunadh,

I think You have to assign to a field symbol directly, and Even if Such a statement is allowed how can you use a Field Symbol without Declaring It

I assume p_num will be having Numbers, now incase p_num contains 1 the string lv_str will have value

<lt_mara1> where this is not declared in the report.

Someone might give you a solution to your problem if you explain your requirement


Thanks and Regards

Pratheek

Read only

0 Likes
827

Hi Pratheek,

                    I want to create Multiple Itabs Dynamically based on the selection if the user selects 2 then <lt_mara1> and <lt_mara2> should be created for this i wrote code like this.

DO p_num TIMES.

   CONCATENATE '<lt_mara' p_num '>' INTO lv_str.

   ASSIGN lr_data->* to (lv_str).

ENDDO.

but lv_str contains <lt_mara1> <lt_mara2> and so on .  but it is not creating itabs then how to create itabs like

<lt_mara1>, <lt_mara2>....  if you have solution for this please provide for this .

Thanks & Regards,

Raghunadh.K


Read only

0 Likes
827

Hi Raghunadh,

You should use data reference to create Dynamic Internal Tables, Fields symbols are not used to create New Objects,

Field symbols are used only to Get the values from Data references, Not to Create New,

So you should have more than one CREATE DATA statement,

But I'm not sure on How to Declare these variables at run time because even even if you concatenate and try, It needs to be declared before hand in DATA statement.

So you should pre- Declare Data Reference, which is not a good solution to you problem.

But in case you have no other way, there is a solution which is a little complex compared to what you are trying to do.

Each Time you create an Internal table you will be assigned a memory, So If you want Multiple Tables to be created at runtime

Create table and Store them into a Internal table,

I'll give you an Example,

DATA:

      dref TYPE REF TO sflight,

      itab LIKE TABLE OF dref.

do 5 times.

CREATE DATA dref.

insert dref into TABLE itab.

ENDDO.

Here Dref can be used to Create as much as Tables as you want and then Insert it into Internal table,

Now when you want to access first table,

read table itab into dref index 1.

then you can assign dref to a field symbol

ASSIGN dref->* TO <fs_table>.

Now you can do the required operation on table as required

Hope you understood the concept, Feel free to ask any doubts incase you have any

Thanks and Regards,

Pratheek

Read only

Former Member
0 Likes
827

Hi Raghunadh,

  As Pratheek told it is not possible to create field symbols dynamically. But it is possible to create dynamic internal tables, using the internal table we can store table values.

Create an internal table which stores internal tables as shown below:

TYPES : BEGIN OF ty_mara,

              matnr TYPE mara-matnr,

              END OF ty_mara.

DATA: lt_mara TYPE STANDARD TABLE OF ty_mara.

* Create an structure to store internal tables

TYPES:
BEGIN OF ty_itab,

  tab_data LIKE lt_mara,

END OF ty_itab.

* Create internal table

DATA: lt_itab TYPE TABLE OF ty_itab,

          ls_itab TYPE                  ty_itab.

PARAMETERS : p_num TYPE n.    

FIELD-SYMBOLS : <lt_mara> TYPE STANDARD TABLE,

                             <fs> TYPE any.

CREATE DATA lr_data  TYPE STANDARD TABLE OF ty_mara.

** ASSIGN lr_data->* TO <lt_mara>.

DO p_num TIMES.

   ASSIGN lr_data->* TO <lt_mara>.

   ls_itab-itab = <lt_mara>.

   APPEND ls_itab TO lt_fieldsymbol.

ENDDO.

Hope this will solve your issue.

Thanks and Regards,

   Satish D R

Read only

arindam_m
Active Contributor
0 Likes
827

Hi,

Found the concept interesting thought why not do it with a twist, see if this helps.

TYPES : BEGIN OF ty_mara,

               tabno  type  n,

               matnr TYPE mara-matnr,

              END OF ty_mara.

DATA: tab_ty_mara TYPE STANDARD TABLE OF ty_mara,

          tab_final TYPE STANDARD TABLE OF ty_mara,

          wa type ty_mara.

PARAMETERS : p_num TYPE n.

**Start of selection**

DATE = sy-datum.

**Now upload MATNR with key (counter 1,2,3,4....upto N as given by user in P_NUM)  and append in **table  Tab_ty_mara

DO p_num TIMES.

SELECT MATNR FROM MARA

                             INTO TABLE Tab_ty_mara

                            WHERE LAEDA EQ date

                

wa-tabno = sy-index.


MODIFY TABLE Tab_ty_mara FROM wa TRANSPORTING tabno WHERE matnr NE space.

  SORT Tab_ty_mara ASCENDING BY  tabno matnr.

  APPEND LINES OF Tab_ty_mara TO tab_final.

date = date + 1.

Clear: Tab_ty_mara.

ENDDO.

You will get a master table tab_final as:

1  100

1  102

1  103

2  200

2  201

3  400

3  404 ...

So its like having multiple tables in single one and when you loop through the table with WHERE condition as tabno = (1,2,3... ) you are accessing a unique subset of data.

Cheers,

Arindam