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

Internal table with vbao and vbap data.

Former Member
0 Likes
1,390

I am getting a sales order number from input and i need to fetch the data based on that to a table. so i have declared like below.

one table itab1 for the final fields i need

and itab2 for the item data fields. and then i am looping and updating it.

So please suggest me mdoifications in this code and how to handle bukrs filed in the final table.

TYPES : BEGIN OF titab1,

bukrs like mseg-bukrs,

vbeln LIKE vbak-vbeln,

erdat LIKE vbak-erdat,

erzet LIKE vbak-erzet,

vkorg LIKE vbak-vkorg,

vtweg LIKE vbak-vtweg,

bstnk LIKE vbak-bstnk,

kunnr LIKE vbak-kunnr,

posnr LIKE vbap-posnr,

matnr LIKE vbap-matnr,

zmeng LIKE vbap-zmeng,

MATKL like vbap-matkl,

ZIEME like vbap-zieme,

END OF titab1.

TYPES : BEGIN OF titab2,

vbeln like vbap-vbeln,

posnr LIKE vbap-posnr,

matnr LIKE vbap-matnr,

zmeng LIKE vbap-zmeng,

MATKL like vbap-matkl,

ZIEME like vbap-zieme,

END OF titab2.

DATA : iTab2 TYPE STANDARD TABLE OF Titab2.

DATA : waitab2 TYPE titab2.

DATA : itab1 TYPE STANDARD TABLE OF titab1.

DATA : waitab1 TYPE titab1.

FIELD-SYMBOLS : <field> type titab1.

SELECT vbeln erdat erzet vkorg vtweg bstnk kunnr

FROM vbak INTO TABLE itab1

WHERE

vbeln = p_vbeln.

SELECT vbeln posnr

matnr

zmeng MATKL ZIEME FROM vbap INTO TABLE itab2

WHERE vbeln = p_vbeln.

LOOP AT itab1 ASSIGNING <field>.

read table itab2 into waitab2 WITH KEY vbeln = <field>-vbeln.

<field>-bukrs = '5120'.

<field>-posnr = waitba2-posnr.

<field>-matnr = waitba2-matnr.

<field>-zmeng = waitba2-zmeng.

<field>-matkl = waitba2-matkl.

<field>-zieme = waitba2-zieme.

endloop.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,132

Hi,

The 1st correct u need to make is,

Before fetching data into ITAB2, check if the header search returned any entries in ITAB1, only if yes then fetch ITEM data into ITAB2 by modifying the WHERE clause as "vbeln IN itab1-vbeln".

2nd.

After having got entries into ITAB2,

form the final structure (ITAB1 + ITAB2) and

LOOP AT ITAB1 (header)...

READ ITAB2 WHERE VBELN = ITAB1-VBELN.

if true then

APPEND final_table.

endif.

ENDLOOP.

Should fulfill ur requirement.

7 REPLIES 7
Read only

Former Member
0 Likes
1,132

Hi,

What is ur final table structure? Also your question is not clear. What is the final requirement?

Regards,

Vik

Read only

0 Likes
1,132

my final requirement is to have the titab1 fields into one table i mean itab1 is my final table i need to fill this with vbak and vbap data, and then i will update it into one ztable.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,132

Hello Gova,

I donot think the way of coding is advisable.

You can try joining VBAK & VBAP and populate the data in one internal table. Modify the lines of the internal table with the hardcode value of BUKRS. That will be a more efficient way of doing this.

wa-bukrs = '5120'.

MODIFY itab FROM wa TRANSPORTING bukrs.

Hope i am clear.

BR,

Suhas

Read only

Former Member
0 Likes
1,132

Hi,

You need to make couple o small modifications in the code:-

After having fetched data into ITAB1, before doing the next fetch. check if ITAB1 has any entries, if yes only then fetch data into ITAB2 by having the WHERE clause as "vbeln in ITAB1-vbeln", so that it fetches the items only for the vbelns fetched @ header and not unless.

Read only

Former Member
0 Likes
1,133

Hi,

The 1st correct u need to make is,

Before fetching data into ITAB2, check if the header search returned any entries in ITAB1, only if yes then fetch ITEM data into ITAB2 by modifying the WHERE clause as "vbeln IN itab1-vbeln".

2nd.

After having got entries into ITAB2,

form the final structure (ITAB1 + ITAB2) and

LOOP AT ITAB1 (header)...

READ ITAB2 WHERE VBELN = ITAB1-VBELN.

if true then

APPEND final_table.

endif.

ENDLOOP.

Should fulfill ur requirement.

Read only

Former Member
0 Likes
1,132

Hi,

Then you can try using a inner join select statement to directly get the required fields in itab1



SELECT a~vbeln a~erdat a~erzet a~vkorg a~vtweg a~bstnk a~kunnr
             b~posnr b~matnr b~zmeng b~MATKL b~ZIEME
into itab1
from ( vbak as a inner join vbap as b
                       ON a~vbeln = b~vbeln)
where a~vbeln = p_vbeln.

Else use FOR ALL ENTRIES like this



SELECT vbeln erdat erzet vkorg vtweg bstnk kunnr
FROM vbak INTO TABLE itab1
WHERE
vbeln = p_vbeln.

if sy-subrc = 0.
SELECT vbeln posnr matnr zmeng MATKL ZIEME
 FROM vbap
 INTO TABLE itab2
FOR ALL ENTRIES in itab1
WHERE vbeln = itab1-vbeln.
endif.

loop at itab1.
read table itab2 with key vbeln = itab1-vbeln.
if sy-subrc = 0.
itab1-posnr = itab2-posnr.
itab1-matnr = itab2-matnr.
itab1-zmeng = itab2-zmeng.
itab1-matkl = itab2-matkl.
itab1-zieme = itab2-zieme.
modify itab1
endif.
endloop.

Regards,

Vik

Read only

guilherme_frisoni
Contributor
0 Likes
1,132

Hi,

try to use a Join instead:


SELECT vbak~vbeln vbak~erdat vbak~erzet vbak~vkorg vbak~vtweg vbak~bstnk vbak~kunnr
             vbap~posnr vbap~matnr vbap~zmeng vbap~MATKL vbap~ZIEME
  FROM vbak INNER JOIN VBAP
  ON vbak~vbeln = vbap~vbeln
  INTO TABLE itab1
  WHERE
    vbeln = p_vbeln.

Regards,

Frisoni