Application Development 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: 

moving data from two itabs into 1 dynamic itab

Former Member
0 Kudos
159

Hi,

I've been looking at the other threads on the subject of dynamic internal tables including the weblog of Subramanian V. I think I am close, but can't apply the logic to solve this particular scenario. Can anyone help?

I have two internal tables the first itab1 has a number of fields in it including matnr.

I then have a itab2 which has two fields matnr and a field containg an alternative material name (call it altmatnr).

For any material there may be 'x' number of alternative material names and so 'x' number of lines for that material in itab2.

What I want to do is check itab2 and find out what the maximum number of alternate lines is for any one material and then create a new internal table 'itab3'. Itab 3 will have all the fields from itab1 but also additional fields altmatnr1, altmatnr2, altmatnr..n, depending on how many altmatnrs are needed.

Finally I want to loop through itab1 writing the data to itab3, and for each line of itab1 loop around itab2 anf fill the fields altmatnr1, altmatnr2, to altmatnr..n as required.

Can anyone help with the logic for this? I've put down what I think is all the info. Hopefully this makes sense, if not please shout and I will try to explain.

Thanks in advance.

Phil.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
123

Try something like the following:


field-symbols <fs>.

loop at itab1 into wa_itab1.
  // do the next 2 statements for each field of itab1 you want in the new table
  ASSIGN COMPONENT 'MATERIAL' OF STRUCTURE <fs_2> TO <fs>.
  <fs> = wa_itab1-material.

  loop at itab2 into wa_itab2 where material = itab1-material.
    CONCATENATE 'MGFPART' sy-tabix INTO l_mfgpart.
    ASSIGN COMPONENT l_mfgpart of structure <fs_2> TO <fs>.
    <fs> = itab2-altmaterial.
  endloop.
  APPEND <fs_2> TO <fs_1>.
endloop.

This code isn't tested, but it is the general idea of what to do.

Brian

9 REPLIES 9

andreas_mann3
Active Contributor
0 Kudos
123

Hi Phil,

here's an approach:

Andreas

0 Kudos
123

Hi Andreas, thanks for this. Unfortunately I'm having difficulty still applying that solution across and a little confused. Can you expand on this?

many thanks.

0 Kudos
123

Actually - I have the first part cracked I think. I have used some of the code found elsewhere on this sight. Many thanks to those who have inadvetantly helped me!

What I'm stuck on is once I have created my table dymanically how do i fill it. This isn't obvious to me from the examples given (my lack of understanding probably).

Here is what I have so far - please excuse my bad code.

  • calculate what the maximum number of partnumber variations there are

sort t_partnos by matnr.

<i>"t_partnos conatins fields matnr and mfgpart

i.e. matnr1 - xxxx

matnr1 - yyyy

matnr2 - aaaa

matnr3 - jjjj

matnr3 - hhhh</i>

  • calculate what the maximum number of partnumber variations there are

SORT t_partnos BY matnr.

LOOP AT t_partnos INTO wa_partnos.

AT NEW matnr.

l_index = 0.

ENDAT.

ADD 1 TO l_index.

AT END OF matnr.

IF l_index GT l_maxidx.

MOVE l_index TO l_maxidx.

ENDIF.

ENDAT.

ENDLOOP.

  • fill fieldcat

DEFINE fill_fieldcat.

move &1 to ls_fieldcatalog-fieldname.

move &2 to ls_fieldcatalog-inttype.

append ls_fieldcatalog to lt_fieldcatalog.

END-OF-DEFINITION.

fill_fieldcat 'IDNRK' 'C'.

l_index = 0.

DO l_maxidx TIMES.

ADD 1 TO l_index.

MOVE l_index TO l_indexc.

CONCATENATE 'MGFPART' l_indexc INTO l_mfgpart.

CONDENSE l_mfgpart.

fill_fieldcat l_mfgpart 'C'.

ENDDO.

fill_fieldcat 'MATMK' 'C'.

fill_fieldcat 'OJTXP' 'C'.

fill_fieldcat 'MEINS' 'C'.

fill_fieldcat 'LABST' 'C'.

fill_fieldcat 'VERPR' 'C'.

fill_fieldcat 'PLIFZ' 'C'.

ASSIGN lt_data TO <fs_data>.

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.

      • So <FS_1> now points to our dynamic internal table.

ASSIGN <fs_data>->* TO <fs_1>.

      • Next step is to create a work area for our dynamic internal table.

CREATE DATA new_line LIKE LINE OF <fs_1>.

      • A field-symbol to access that work area

ASSIGN new_line->* TO <fs_2>.

now I want to loop at my main itab (itab1) and write the fields to the corresponding fields of my dynamic itab. For each line of itab1 I also want to loop around t_partnos and write the mfgpart field from this table to mfgpart1, mfgpart2 etc depending on how many lines exist.

Hope this helps. Can you advise?

thanks

0 Kudos
123

Take at look at the ASSIGN COMPONENT command. This will allow you to access the fields in your structure that <fs_2> points to. You can access the fields by their name or by their position.

Former Member
0 Kudos
124

Try something like the following:


field-symbols <fs>.

loop at itab1 into wa_itab1.
  // do the next 2 statements for each field of itab1 you want in the new table
  ASSIGN COMPONENT 'MATERIAL' OF STRUCTURE <fs_2> TO <fs>.
  <fs> = wa_itab1-material.

  loop at itab2 into wa_itab2 where material = itab1-material.
    CONCATENATE 'MGFPART' sy-tabix INTO l_mfgpart.
    ASSIGN COMPONENT l_mfgpart of structure <fs_2> TO <fs>.
    <fs> = itab2-altmaterial.
  endloop.
  APPEND <fs_2> TO <fs_1>.
endloop.

This code isn't tested, but it is the general idea of what to do.

Brian

0 Kudos
123

thanks guys,

I was in the process of looking up the assign component when the second tip came in.

It all makes perfect sense now! Many thanks. I think that this is going to come in very handy in the future for me.

0 Kudos
123

final problem I think!

<fs_2> has my line of fields and values and I want to append it to <fs_1> my table.

Doing an append gives me an error message that "append is not allowed with type hashed or 'any table", and trying other methods seems to short dump when I run the program.

my field symbols are defined

FIELD-SYMBOLS: <fs_data> TYPE REF TO data,

<fs_1> TYPE ANY TABLE,

<fs_2>,

<fs_3>.

can you put me straight on this final step?

thanks

Former Member
0 Kudos
123

Define <fs_1> like:


FIELD-SYMBOLS: <fs_1> TYPE STANDARD TABLE.

Brian

0 Kudos
123

Thanks Brian,

I think I changed everything but that declaration!