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: 

Loop in retiving data

Former Member
0 Kudos

Hi frnds,

Suppose i hav 10 Purchase orders and perr PO i hav 10 line items.

so frnds can anyone tell me how to retive data using this.

like i guess we can use loop within a loop .

can we loop at header and read the item data. Just give me necessary help frnds.

regards,

satish

1 ACCEPTED SOLUTION

Former Member
0 Kudos

instead of loop inside a loop use read inside a loop . it gives a gd performance and also solves the purpose.

syntax is

loop at itab1 into wa_itab1.

read table itab2 with key itab2-field = itab1-field

do somethn

endloop.

hopen ths helps

regards

navjhot

reward accordingly

14 REPLIES 14

Former Member
0 Kudos

instead of loop inside a loop use read inside a loop . it gives a gd performance and also solves the purpose.

syntax is

loop at itab1 into wa_itab1.

read table itab2 with key itab2-field = itab1-field

do somethn

endloop.

hopen ths helps

regards

navjhot

reward accordingly

Former Member
0 Kudos

Satish,

Say you have 2 internal tables.

1. For header data -


i_header

2.Line item data -


i_lineitem

Take one temp internal table------i_temp like header internal table.

i_tem[] = i_header[].

Sort i_temp by your key fields.

Delete adjacent duplicates from i_temp.

loop at i_temp.

loop at i_line item where your key fields conditions.

here you can move header and line items data here to final internal table.

Endloop.

Endloop.

Clear i_temp.

Free i_temp.

Pls. reward if useful.

Former Member
0 Kudos

Hi,

<b>If you are talking about retrieving EKKO and EKPO data from database, you

can use JOIN.</b> See this code

*----------------------------------------------------------------------*
*----------------------------------------------------------------------*

TABLES: ekko.

SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

DATA: BEGIN OF lit_podata OCCURS 0,
        ebeln LIKE ekpo-ebeln,
        ebelp LIKE ekpo-ebelp,
        matnr LIKE ekpo-matnr,
        netwr LIKE ekpo-netwr,
        navnw LIKE ekpo-navnw,
        bukrs LIKE ekko-bukrs,
        bsart LIKE ekko-bsart,
        lifnr LIKE ekko-lifnr,
        ekorg LIKE ekko-ekorg,
      END OF lit_podata.

SELECT
      a~ebeln
      a~ebelp
      a~netwr
      a~navnw
      a~matnr
      b~bukrs
      b~bsart
      b~lifnr
      b~ekorg FROM ekpo AS a
              INNER JOIN ekko AS b
              ON a~ebeln = b~ebeln
              INTO TABLE lit_podata
        WHERE b~ebeln IN s_ebeln.
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*

<b>If you are talking about processing internal table having EKKO and EKPO data then check this code.</b>

*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
DATA: lit_ekko LIKE ekko OCCURS 0 WITH HEADER LINE,
      lit_ekpo LIKE ekpo OCCURS 0 WITH HEADER LINE.

*" Get PO header data in lit_ekko
*" Get PO line item data in lit_ekpo

SORT: lit_ekko BY ebeln,
      lit_ekpo BY ebeln
                  ebelp.

LOOP AT lit_ekpo.

  READ TABLE lit_ekko WITH KEY ebeln = lit_ekpo-ebeln
                      BINARY SEARCH.

  IF sy-subrc = 0.

*"  Carry out your logic based on lit_ekpo, lit_ekko data

  ENDIF.

ENDLOOP.
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*

Let me know if you have any question.

Regards,

RS

Former Member
0 Kudos

Hi Sathish,

Sort the ITAB1 & ITAB2 by Purchase Order.

Loop at ITAB2.

At new Purchase order

Read table ITAB1 with key Purchase order = ITAB2-Purchase order

Binary search.

endat.

......

Endloop.

Former Member
0 Kudos

Hi..satish..

this will stop u from loop within loop but acheives result...

lets say, itab::PO

gtab::line items

htab::result

data: w_flag type c value '0'.

loop at itab into wa1.

while w_flag eq '0'.

read table gtab with key PO = wa1-PO into wa2.

if sy-subrc eq 0.

htab-PO = wa1-PO.

htab-LINE = wa2-LINEITEM.

append htab.

delete gtab index sy-tabix.

else.

w_flag = '1'.

endif.

endwhile.

w_flag = '0'.

endloop.

Former Member
0 Kudos

HI..,

be clear with your requirement..

If u are going to retrieve data from data base ... then u can use <b>JOIN in SELECT statement !!</b>

between the header table and item table ...

as RS suggested !!

But if u want process the retrieved data between two internal tables .. then Use <b>Read statement inside the LOOP of the Header table, for the item table</b>..

this increases ur performance !!!!!!!

<b>Plz do remember to close the thread , when ur problem is solved!!</b>

reward all helpful answers,

sai ramesh

Former Member
0 Kudos

Hi frnds,

Thanks for ur replies.

In my query can anyone tell me shuld we loop at item internal table or header internal table.

Thnks and regards,

satish

0 Kudos

Any help frnds.

thanks and regards,

satish

0 Kudos

loop the item internal table.

ex:

sort it_head by ebeln.

sort it_item by ebeln.

loop at it_item.

read it_head with key ebeln = it_item-ebeln binary search.

if sy-subrc = 0.

endif.

endloop.

Former Member
0 Kudos

Hi Satish,

What you are thinking is absolutely correct......


LOOP AT HEADER_TABLE.

LOOP AT ITEM_TABLE WHERE (map all the key fields so that you have only one line item every pass of loop at item_table per 1 header detail)

**write your logic here....

ENDLOOP.

ENDLOOP.

Hope this is Helpful......

Thanks and Regards,

Kunjal Patel

Former Member
0 Kudos

sort ithead by ebeln.

sort ititem by ebeln.

loop at ititem.

write : / ititem-ebeln, ititem-matnr, ititem-menge.

read table itheader with key ebeln = ititem-ebeln.

if sy-subrc = 0.

write : ithead-lifnr.

endif.

endloop.

since the hedaer data is same for all the items and ebeln is the only primary key in ekko as here you have to loop at item and read header.

regards

shiba dutta

0 Kudos

Thank u frnds For ur replies.

So is it mandatory that we shuld always go for looping at item and reading the header data.

Just can anyone tell wht is the requirement that when will we go for looping header and when will we go for looping at item.

THis will solve my query and so that i can close the thread.

thanks u all again.

regards,

satish

0 Kudos

actually it depends on the data you have in your itab..

because read statement only read the first row when it meets the criterion with key addition ,.. here ebeln is unique for header table.. so if you are reading header table means you are getting one and only one row by this and it is satisfying your requirement... generally for one header data there may be lots of item so you have to loop at item and the same header data for those items by passing the primary key..

regards

shiba dutta

0 Kudos

Reading item data inside a loop is time consuming compare to header data.

You can use Read statement to fetch header data, but you have to use loop to read item data.