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

declare fields dynmically in the internal table

Former Member
0 Likes
1,801

Hi,

I have got stuck at one place. I want to declare fields dynmically in the internal table based on the content of another table.

For eg.

internal table1 - it1 contains 5 rows.(No.of rows can change)

then i want to declare 5 cloumns i.e fields in another internal table it2.

Thanks,

Mohammadi

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
1,772

Hi,


data: begin of it1 occurs 0,
          fieldname type string,
          ...
         end of it1.

data it_fieldcat TYPE lvc_t_fcat with header line.

data: new_table TYPE REF TO data.
data: new_line  TYPE REF TO data.

field-symbols: <new_tab> type any table,
                       <new_line> type any.

Loop at it1.
     "create fied catalog as you would create one for alv
      it_fieldcat-fieldname = it1-fieldname.  "create field of content of IT1-fieldname
....  
    "either set type explicitly or use reference to some DDIC field
    it_fieldcat-INTTYPE  =  ".....your custom type
    "or
    it_fieldcat-ref_field = "...referenced field in DDIC
    it_fieldcat-ref_table = "...and a table
   
    append it_fieldcat.
endloop.

"create dynamic table
   CALL METHOD cl_alv_table_create=>create_dynamic_table
       EXPORTING
        it_fieldcatalog = it_fieldcat[]
       IMPORTING
        ep_table        = new_table.

"now retrieve table from reference pointing to it
assign new_table->* to <new_tab>.
create data new_line like line of <new_tab>.
assign new_line>* to <new_line>.

"now <new_tab> is your dynamic table and <new_line> is a workarea to work with that table
"you can i.e. do like

Loop at <new_tab> assigning <new_line>.
   "work here witl <new_line>
endloop.

Regards

Marcin

Hi,

I have got stuck at one place. I want to declare fields dynmically in the internal table based on the content of another table.

For eg.

internal table1 - it1 contains 5 rows.(No.of rows can change)

then i want to declare 5 cloumns i.e fields in another internal table it2.

Thanks,

Mohammadi

13 REPLIES 13
Read only

Former Member
0 Likes
1,772

Hi,

By describe statement you will get the no of rows of the internal table.

Then according to that no you need to create the internal table using FIELD-SYMBOLS.

DATA : w_line TYPE I.
DESCRIBE TABLE <internal_table> LINES w_line.

Here w_line will contain the no of rows of the internal table.

Now use the FIELD-SYMBOLS and the method CREATE_DYNAMIC_TABLE

of the class CL_ALV_TABLE_CREATE .

You can check the link to get the fields of internal table dynamically -

[https://wiki.sdn.sap.com/wiki/display/Snippets/Howtocreateinternaltable+dynamically]

Regards

Pinaki

Read only

Former Member
0 Likes
1,772

Hi,

Just try the below link

[;

Regards,

Nikhil Kayal

Read only

MarcinPciak
Active Contributor
0 Likes
1,773

Hi,


data: begin of it1 occurs 0,
          fieldname type string,
          ...
         end of it1.

data it_fieldcat TYPE lvc_t_fcat with header line.

data: new_table TYPE REF TO data.
data: new_line  TYPE REF TO data.

field-symbols: <new_tab> type any table,
                       <new_line> type any.

Loop at it1.
     "create fied catalog as you would create one for alv
      it_fieldcat-fieldname = it1-fieldname.  "create field of content of IT1-fieldname
....  
    "either set type explicitly or use reference to some DDIC field
    it_fieldcat-INTTYPE  =  ".....your custom type
    "or
    it_fieldcat-ref_field = "...referenced field in DDIC
    it_fieldcat-ref_table = "...and a table
   
    append it_fieldcat.
endloop.

"create dynamic table
   CALL METHOD cl_alv_table_create=>create_dynamic_table
       EXPORTING
        it_fieldcatalog = it_fieldcat[]
       IMPORTING
        ep_table        = new_table.

"now retrieve table from reference pointing to it
assign new_table->* to <new_tab>.
create data new_line like line of <new_tab>.
assign new_line>* to <new_line>.

"now <new_tab> is your dynamic table and <new_line> is a workarea to work with that table
"you can i.e. do like

Loop at <new_tab> assigning <new_line>.
   "work here witl <new_line>
endloop.

Regards

Marcin

Read only

0 Likes
1,772

Hi,

Using the code provided by you, i was able to create a internal table which is combination of 2 internal table. But now i want to append the data from those internal table to this newly created internal table and pass this internal table to FM- reuse_alv_list_display.

Please help.

Read only

0 Likes
1,772

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = gt1_fieldcat

importing

ep_table = gt_new_table.

assign gt_new_table->* to <gt_table>.

create data gs_new_line like line of <gt_table>.

assign gs_new_line->* to <gs_line>.

this way u can append the data.

assign component 'NTNAME' of structure <gs_line> to <gd_field>.

this way u can pass the data to ALV

<gt_table> - dyn internal table...

try.

cl_salv_table=>factory(

exporting

list_display = abap_false

importing

r_salv_table = lo_alv

changing

t_table = <gt_table> ).

catch cx_salv_msg .

endtry.

lo_alv->display( ).

Read only

0 Likes
1,772

Hi,

Thanks for the reply. But my requirement is as follows--

I have 2 internal tables say it1 and it2.

it1 have fields A, B and C.

it2 have fields A, D, E.

I have created an internal table it3 dynamically which contains the fields of both the internal table.

Now i want to pass the data from it1 and it2 to it3.

and this it3 to FM for display.

Read only

Sm1tje
Active Contributor
0 Likes
1,772

LOOP at it1 and find the corresponding record from it2, next do a MOVE-CORRESPONDING from workare1 and workarea2 to <LINE> and append it.

Read only

0 Likes
1,772

Excatly as Micky said:


loop at it1.
  read table it2 index sy-tabix.  "read corresponding entry in IT2
  if sy-subrc = 0.
    move-corresponding: it1 to <new_line>,
                                   it2 to <new_line>.
    append <new_line> to <new_tab>.  "now <new_tab> stores entries from both IT1 and IT2  
  endif.
endloop.

Regards

Marcin

Read only

0 Likes
1,772

Hi ,

You can check the link I specified in my last post , there it is specified how to

populate dynamically created internal table.

The way to append the data to the created (dynamically) table - -

DATA : W_I TYPE I.

DO 5 TIMES.
  W_I = SY-INDEX - 1.
  DO 5 TIMES.
    ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
    <FS_FIELD> = SY-INDEX + W_I.
  ENDDO.
  INSERT <FS_LINE> INTO TABLE <FS_TABLE>. " Use to populate the table
ENDDO.

Regards

Pinaki

Read only

0 Likes
1,772

Hi ,

I did it , this followed contents will resolve the issue ..

Here it1 table have fld1 , fld2 and fld3 fields and it2 have fld1 , fld4 and fld5.

Now my dynamically create table <FS_TABLE> contains fld1, fld2, fld3, fld4 and fld5.

the structure means the field string is <FS_LINE>.

Check the code -

DATA : BEGIN OF FS1 ,
         FLD1(2),
         FLD2(2),
         FLD3(2),
       END OF FS1,
       IT1 LIKE TABLE OF FS1,
       BEGIN OF FS2 ,
         FLD1(2),
         FLD4(2),
         FLD5(2),
       END OF FS2,
       IT2 LIKE TABLE OF FS2.


FS1-FLD1 = 1.
FS1-FLD2 = 2.
FS1-FLD3 = 3.
APPEND FS1 TO IT1.

FS1-FLD1 = 6.
FS1-FLD2 = 7.
FS1-FLD3 = 8.
APPEND FS1 TO IT1.

FS2-FLD1 = 1.
FS2-FLD4 = 4.
FS2-FLD5 = 5.
APPEND FS2 TO IT2.

FS2-FLD1 = 6.
FS2-FLD4 = 9.
FS2-FLD5 = 10.
APPEND FS2 TO IT2.


LOOP AT IT1 INTO FS1.
  READ TABLE IT2 INTO FS2 with key fld1 = fs1-fld1.
      ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
      MOVE-CORRESPONDING FS1 TO  <FS_LINE>.
      MOVE-CORRESPONDING FS2 TO  <FS_LINE>.
    INSERT <FS_LINE> INTO TABLE <FS_TABLE>.
ENDLOOP.

To display the contents --

LOOP AT <FS_TABLE> INTO <FS_LINE> .
  DO 5 TIMES. "  times because the table have 5 fields (Fld1, Fld2 ....Fld5)
    ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
    WRITE <FS_FIELD>.
  ENDDO.
  NEW-LINE.
ENDLOOP.

Note : As because you got how to create the table dynamically, that is why

I gave the example of populating the table from to internal table.

Regards

Pinaki

Read only

0 Likes
1,772

hi,

Now i am able to create the dynamic internal table and populate it.

I want to display it using FM - reuse_alv_list_display.

For this i am creating fieldcatalog by using FM - reuse_alv_fieldcatalog_merge. In this i am passing i_call back program, i inclname and iinternal table name (dynamically created table)

But my Fieldcatalog is not getting created.

Please help.

Read only

0 Likes
1,772

I'd like to make a remark here:

You obviously not trying to resolve the problem yourself but waiting for excat solution "given you on a plate". Your questions were answered this forum many many times. But apparently you simply can't locate search button on this site. This is not right! You obviously violate Rules and Engagements which should be obligated here. But these you seem not to read ever.

Moreover, people here are to give you a direction how to use some tools, not to use them for you.

And finally I bit of appreciation (by assinng points) to all people sacrifying their time to resolve some basic question and explain it to a newbie is welcome . This would not only encourage people to contribute here further, but also is a matter of good behaviour .

To all people:

Please do not answer here anymore. Reported to moderators.

Read only

matt
Active Contributor
0 Likes
1,772

Locked for the reason given by Marcin. Please read and follow the Rules of Engagement.