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

Looping at internal table

Former Member
0 Likes
855

Hi, I work on SAP BW and need guidance on the functionality from the below code:

l_s_datapak_line[] = datapak[].

l_s_datapak[] = datapak[].

sort l_s_datapak_line by pernr endda descending.

loop at l_s_datapak.

read table l_s_datapak_line with key pernr = l_s_datapak-pernr

endda = '99991231'.

if sy-subrc = 0.

pernr = l_s_datapak_line-pernr.

curr_compcode = l_s_datapak_line-bukrs.

curr_job = l_s_datapak_line-stell.

curr_mastcctr = l_s_datapak_line-kostl.

curr_orgunit = l_s_datapak_line-orgeh.

curr_persarea = l_s_datapak_line-werks.

curr_perssubarea = l_s_datapak_line-BTRTL.

curr_position = l_s_datapak_line-plans.

curr_chief = l_s_datapak_line-zzchiefpernr.

curr_supervisor = l_s_datapak_line-zzsupervisor.

modify i_curr index i_index.

  • exit.

endif.

endloop.

In the above code, I want the loop to run only once for each pernr. If there are 10 records in l_s_datapak with pernr = 100, then the loop would run 100 times although there would be only one record in l_s_datapak_line with endda = '99991231'. I want the loop to run only once , assign the values and then l_s_datapak-pernr should become 101. How do I do it?

Any help is highly appreciated and thanks for the help in advance.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
835

I am not sure I understood the requirement here. Both l_s_datapak_line and l_s_datapak have the same records (since both are filled from datapak). If the purpose is to get the current employee record out of datapak, why don't you simply do this.

l_s_datapak[] = datapak[].

SORT l_s_datapak BY pernr endda DESCENDING.

DELETE ADJACENT DUPLICATES FROM l_s_datapak COMPARING pernr.

*-- Now l_s_datapak has only unique pernr records with only the record that has

  • the greatest end date. But we are interested only in those PERNR records that

  • have an end date of '99991231'.

LOOP AT l_s_datapak WHERE endda = '99991231'.

pernr = l_s_datapak-pernr.

curr_compcode = l_s_datapak-bukrs.

curr_job = l_s_datapak-stell.

curr_mastcctr = l_s_datapak-kostl.

curr_orgunit = l_s_datapak-orgeh.

curr_persarea = l_s_datapak-werks.

curr_perssubarea = l_s_datapak-BTRTL.

curr_position = l_s_datapak-plans.

curr_chief = l_s_datapak-zzchiefpernr.

curr_supervisor = l_s_datapak-zzsupervisor.

modify i_curr index i_index.

ENDLOOP.

Message was edited by:

Srinivas Adavi

Hi, I work on SAP BW and need guidance on the functionality from the below code:

l_s_datapak_line[] = datapak[].

l_s_datapak[] = datapak[].

sort l_s_datapak_line by pernr endda descending.

loop at l_s_datapak.

read table l_s_datapak_line with key pernr = l_s_datapak-pernr

endda = '99991231'.

if sy-subrc = 0.

pernr = l_s_datapak_line-pernr.

curr_compcode = l_s_datapak_line-bukrs.

curr_job = l_s_datapak_line-stell.

curr_mastcctr = l_s_datapak_line-kostl.

curr_orgunit = l_s_datapak_line-orgeh.

curr_persarea = l_s_datapak_line-werks.

curr_perssubarea = l_s_datapak_line-BTRTL.

curr_position = l_s_datapak_line-plans.

curr_chief = l_s_datapak_line-zzchiefpernr.

curr_supervisor = l_s_datapak_line-zzsupervisor.

modify i_curr index i_index.

  • exit.

endif.

endloop.

In the above code, I want the loop to run only once for each pernr. If there are 10 records in l_s_datapak with pernr = 100, then the loop would run 100 times although there would be only one record in l_s_datapak_line with endda = '99991231'. I want the loop to run only once , assign the values and then l_s_datapak-pernr should become 101. How do I do it?

Any help is highly appreciated and thanks for the help in advance.

6 REPLIES 6
Read only

Former Member
0 Likes
835

You can do something like this...


  LOOP AT T_NPEDI ASSIGNING <NPEDI>.
    IF W_NPEDI NE <NPEDI>-NPEDI.
      W_NPEDI = <NPEDI>-NPEDI.
    ELSE.
      CONTINUE.
    ENDIF.
*Do logic....
ENDLOOP.

Greetings,

Blag.

Read only

0 Likes
835

Hi Alvaro, thanks for the prompt response. however i am at the basic level of abap and could not understand much of the field symbol logic that you have posted. could you please write the same using the table names that I have given.

Thanks

Read only

0 Likes
835

Sure -:)


l_s_datapak_line[] = datapak[].
l_s_datapak[] = datapak[].
sort l_s_datapak_line by pernr endda descending.

loop at l_s_datapak.

*----ADD THIS LOGIC----*
if w_pernr ne l_s_datapak-pernr.
w_pernr = l_s_datapak-pernr.
else.
continue.
endif.
*----ADD THIS LOGIC----*

read table l_s_datapak_line with key pernr = l_s_datapak-pernr
endda = '99991231'.

if sy-subrc = 0.
pernr = l_s_datapak_line-pernr.
curr_compcode = l_s_datapak_line-bukrs.
curr_job = l_s_datapak_line-stell.
curr_mastcctr = l_s_datapak_line-kostl.
curr_orgunit = l_s_datapak_line-orgeh.
curr_persarea = l_s_datapak_line-werks.
curr_perssubarea = l_s_datapak_line-BTRTL.
curr_position = l_s_datapak_line-plans.
curr_chief = l_s_datapak_line-zzchiefpernr.
curr_supervisor = l_s_datapak_line-zzsupervisor.
modify i_curr index i_index.
* exit.
endif.
endloop.

Done -;)

Greetings,

Blag.

Read only

Former Member
0 Likes
835

Hi,

Use Control Break statements i.e. AT NEW....ENDAT as shown below.

l_s_datapak_line[] = datapak[].

l_s_datapak[] = datapak[].

sort l_s_datapak_line by pernr endda descending.

loop at l_s_datapak.

<b>AT NEW pernr.</b>

read table l_s_datapak_line with key pernr = l_s_datapak-pernr

endda = '99991231'.

if sy-subrc = 0.

pernr = l_s_datapak_line-pernr.

curr_compcode = l_s_datapak_line-bukrs.

curr_job = l_s_datapak_line-stell.

curr_mastcctr = l_s_datapak_line-kostl.

curr_orgunit = l_s_datapak_line-orgeh.

curr_persarea = l_s_datapak_line-werks.

curr_perssubarea = l_s_datapak_line-BTRTL.

curr_position = l_s_datapak_line-plans.

curr_chief = l_s_datapak_line-zzchiefpernr.

curr_supervisor = l_s_datapak_line-zzsupervisor.

modify i_curr index i_index.

  • exit.

endif.

<b>ENDAT.</b>

endloop.

Reward points if the answer is helpful.

Regards,

Mukul

Read only

Former Member
0 Likes
836

I am not sure I understood the requirement here. Both l_s_datapak_line and l_s_datapak have the same records (since both are filled from datapak). If the purpose is to get the current employee record out of datapak, why don't you simply do this.

l_s_datapak[] = datapak[].

SORT l_s_datapak BY pernr endda DESCENDING.

DELETE ADJACENT DUPLICATES FROM l_s_datapak COMPARING pernr.

*-- Now l_s_datapak has only unique pernr records with only the record that has

  • the greatest end date. But we are interested only in those PERNR records that

  • have an end date of '99991231'.

LOOP AT l_s_datapak WHERE endda = '99991231'.

pernr = l_s_datapak-pernr.

curr_compcode = l_s_datapak-bukrs.

curr_job = l_s_datapak-stell.

curr_mastcctr = l_s_datapak-kostl.

curr_orgunit = l_s_datapak-orgeh.

curr_persarea = l_s_datapak-werks.

curr_perssubarea = l_s_datapak-BTRTL.

curr_position = l_s_datapak-plans.

curr_chief = l_s_datapak-zzchiefpernr.

curr_supervisor = l_s_datapak-zzsupervisor.

modify i_curr index i_index.

ENDLOOP.

Message was edited by:

Srinivas Adavi

Read only

0 Likes
835

Hi Srinivas, thanks a lot, my issue is almost resolved. I am not able to test it as the system is down, but looking at the code i am sure that it will work. thanks !!

Points awarded too !!!