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

join 2 internal tables based on condition

usersant077
Explorer
0 Likes
2,969

Hi
I have 2 internal tables,
ill give example:

itab1:
wa_1

vbeln
posnr
material (no value)
field4
field5
field6......


itab2
wa_2

vbeln
posnr
material (has value)

i want to join both in a way that ill get itab2-material value into itab1-material,
where itab1-vbeln = itab2 -vbeln
and itab1-posnr = itab2-posnr.

(their structure is different)

I guess its quite a simple question but would be great if someone can help with the way to achive this,
Thank you very much.

Hi
I have 2 internal tables,
ill give example:

itab1:
wa_1

vbeln
posnr
material (no value)
field4
field5
field6......


itab2
wa_2

vbeln
posnr
material (has value)

i want to join both in a way that ill get itab2-material value into itab1-material,
where itab1-vbeln = itab2 -vbeln
and itab1-posnr = itab2-posnr.

(their structure is different)

I guess its quite a simple question but would be great if someone can help with the way to achive this,
Thank you very much.

8 REPLIES 8
Read only

usersant077
Explorer
0 Likes
2,738

Ill just add that on my case unfortunately I cant join everything into 1 internal table from the first place so I have to find a way to merge both

thanks again.

Read only

geert-janklaps
SAP Mentor
SAP Mentor
2,738

Hi,

You could try something like following:

FIELD-SYMBOLS: <fs_itab1> LIKE LINE OF itab1,
               <fs_itab2> LIKE LINE OF itab2.

LOOP AT itab1 ASSIGNING <fs_itab1>.
  READ TABLE itab2 ASSIGNING <fs_itab2> WITH KEY vbeln = <fs_itab1>-vbeln
                                                 posnr = <fs_itab1>-posnr.

  IF sy-subrc = 0.
    <fs_itab1>-material = <fs_itab2>-material.
  ENDIF.
ENDLOOP.

Best regards,

Geert-Jan Klaps

Read only

0 Likes
2,738

Hi, thank you very much, ive tried but it still dosent fetch the value... any idea why maybe?

Read only

0 Likes
2,738

Maybe post the actual code so we can have a better look. Above example should work if itab1 and itab2 contain the data you described.

Read only

matt
Active Contributor
0 Likes
2,738

<fs_itab2> should be defined with as HASHED table with UNIQUE-KEY vbeln posnr, and the read should be:

  READ TABLE itab2 ASSIGNING <fs_itab2> WITH TABLE KEY vbeln = <fs_itab1>-vbeln
                                                        posnr = <fs_itab1>-posnr.

otherwise you are likely to have performance problems.


(Also, using fs inside the angle brackets to indicate a field symbol when you already know that it's a field symbol due to the angle bracket... 😮 ).

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,738

Please use the button CODE to format your examples, like this:

itab1:

vbeln posnr material   field4 field5 field6
----- ----- ---------- ------ ------ ------
XX    XX    (no value) XX     XX     XX

itab2:

vbeln posnr material
----- ----- ----------
XX    XX    aaaaa

I want to join both in a way that ill get itab2-material value into itab1-material:

vbeln posnr material   field4 field5 field6
----- ----- ---------- ------ ------ ------
XX    XX    aaaaa      XX     XX     XX
Read only

MateuszAdamus
Active Contributor
0 Likes
2,738

Hello usersant077

The solution provided by 8d8214c7f9734f45be69f95cc0d5aeee should work. If it does not then please share the definition of ITAB1 and ITAB2 types. There might by some inconsistencies in the field types (probably missing zeros in the VBELN and/or POSNR fields).Posted on
Kind regards,
Mateusz
Read only

michael_piesche
Active Contributor
2,738

usersant077, 'joining' two internal tables based on two corresponding values (e.g. vbeln, posnr), should indeed be a 'quite simple' task. A very standard answer to solve this 'problem' has been presented to you, and if the data and the structures are ok, it works 100%.

There is nothing else the community can do for you now, unless you also post the relevant parts of the coding (data definition and 'joining' technique, e.g. loop/read table) and possible give information about the data that should match but doesnt (e.g. screenshot of itabs in debugger showing the two fields).