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: 

how to write 2 loops into single loop ?

0 Kudos
1,150
so i have created 2 internal tables and i tried to find the name of employee from first table with base salary from 2nd table and i have manually inserted data into it. so how can i write both loops into single loop. and here is the code i have written -

TYPES : BEGIN OF s_empdetails,
empid TYPE n,
fname(20) TYPE c,

END OF s_empdetails.

TYPES : BEGIN OF s_empsal,
empid TYPE n,
base_sal TYPE p,
END OF s_empsal.
DATA : gt_header TYPE TABLE OF s_empdetails WITH HEADER LINE,
gwa_header TYPE s_empdetails.
DATA : gt_items TYPE TABLE OF s_empsal,
gwa_items TYPE s_empsal.
LOOP AT gt_items INTO gwa_items.
IF gt_items IS INITIAL.
gv_highest_num = gwa_items-base_sal.
ELSEIF gv_highest_num < gwa_items-base_sal.
gv_highest_num = gwa_items-base_sal.
ENDIF.
ENDLOOP.
LOOP AT gt_items INTO gwa_items.
IF gt_items IS INITIAL.
gv_second_number = gwa_items-base_sal.
ELSEIF gwa_items-base_sal > gv_second_number AND gwa_items-base_sal < gv_highest_num.
gv_second_number = gwa_items-base_sal.
ENDIF.
ENDLOOP.
READ TABLE gt_items INTO gwa_items WITH key base_sal = gv_second_number.
READ TABLE gt_header INTO gwa_header WITH KEY empid = gwa_items-empid.
6 REPLIES 6

former_member751591
Participant
597

Thank you for visiting SAP Community to get answers to your questions. Since you're asking a question here for the first time, I recommend that you familiarize yourself with Community Q&A , as it provides tips for preparing questions that draw responses from our members.

For example, you can:

- outline what steps you took to find answers (and why they weren't helpful)

- share screenshots of what you've seen/done

- make sure you've applied the appropriate tags

- use a more descriptive subject line.

The more details you provide, the more likely it is that members will be able to respond. Feel free to also take our Q&A tutorial Q&A tutorial

Should you wish, you can revise your question by selecting Actions, then Edit.

By adding a picture to your Profile you encourage readers to respond.

Former Member
597

As far as I understand you are trying to find the employee with the second highest salary and then read its name from the other table.

If so, it is a bit of an over-complication. You can simply use a table which is sorted according to the salary.

DATA gt_items TYPE SORTED TABLE OF s_empsal WITH NON-UNIQUE KEY base_sal.
...
READ TABLE gt_items INTO gwa_items INDEX lines( gt_items ) - 1.

This way, you'll have the employee number available in gwa_items and can proceed with the second read command. (Don't forget to check if the table have less then 2 items.)

Also do not use obsolete language elements, like tables with header lines.

0 Kudos
597

Thank you for the response. actually i already got the answer using reading with index but here i am trying to get the answer using loops that wrote into single loop . can u help me with that?

0 Kudos
597

Thank you for the response . actually i already tried with reading with index different way . but here i want to get the anwer using loops that i have mentioned but now in single loop . how can i get it?

and also in the read statement that you have given ,if suppose i have big number of entried in table , then how i should get employee number using index lines?

597

sumedh7

Yes, it is possible to implement a logic that uses single iteration. But I'd only use it if there is no option to use sorted tables.

DATA first TYPE s_empsal.
DATA second TYPE s_empsal.

READ TABLE gt_items INDEX 1 INTO first.
READ TABLE gt_items INDEX 1 INTO second.

LOOP AT gt_items FROM 2 INTO gwa_items.

IF gwa_items-base_sal > first-base_sal.
second = first.
first = gwa_items.
ELSEIF gwa_items-base_sal > second-base_sal.
second = gwa_items.
ENDIF.

ENDLOOP

matt
Active Contributor
597
 gt_header  TYPE TABLE OF s_empdetails WITH HEADER LINE,

Tables with HEADER LINES have been obsolete for over twenty years. Don't use them. They're bad programming practice.