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

PROBLEM

former_member630092
Participant
0 Likes
1,175

I AM BEGGENER TO ABAP. CAN U CHECK THIS CODE. LOOP DOES NOT SHOW ANY THING. WHY ?

DATA : BEGIN OF ITAB1 OCCURS 0,

SALES TYPE P DECIMALS 2,

NAME(10),

END OF ITAB1.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1 SORTED BY SALES.

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1 SORTED BY SALES.

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1 SORTED BY SALES.

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,131

Hi,

Good question..

While doing Append we can do sorting at the same time.

I wil give you one example...

It is like Ranking of any Sprots. ( FIFA Ranking, ICC Ranking)

Say you give ranking from 1 to 10.

At any point of time u will find only 10 members in the ranking according to the points.

If a new member gains more points than the 10 nth member, he will be included in the TOP TEN and the other member will be taken off from the Ranking.

In your case....

You declared Internal table as OCCURS 0....

that means at any point of time, it can store ZERO records.

If you declare internal table as OCCURS 5...

that means at any point of time, it can store only 5 records...

If you declare internal table as OCCURS 10...

that means at any point of time, it can store only 10 records...

While using APPEND TAB SORTED BY SALES.

It sorts the record then only it appends.

12 REPLIES 12
Read only

Former Member
0 Likes
1,131

Hi

Use

<b>APPEND ITAB1</b>.

After adding all records, just say

<b>SORT ITAB1 BY SALES.</b>

Regards

Raj

Read only

Former Member
0 Likes
1,131

Hi Ravi,

Change your code as.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1. .

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1.

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1.

SORT ITAB1 BY SALES.

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

Reward points if useful.

Regards,

Atish

Read only

alex_m
Active Contributor
0 Likes
1,131

Can you try this way.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1.

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1.

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1.

SORT itab1 by sales.

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

Read only

Former Member
0 Likes
1,131

I modified the program :

DATA : BEGIN OF ITAB1 OCCURS 0,

SALES TYPE P DECIMALS 2,

NAME(10),

END OF ITAB1.

start-of-selection.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1 ."SORTED BY SALES.

clear itab1.

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1. "SORTED BY SALES.

clear itab1.

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1. "SORTED BY SALES.

clear itab1.

sort itab1 by sales.

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

do not use sort with append statement.

Thanks

Seshu

Read only

Former Member
0 Likes
1,131

Yes, It won't work...

Use only append and sort it afterwards..

DATA : BEGIN OF ITAB1 OCCURS 0,

SALES TYPE P DECIMALS 2,

NAME(10),

END OF ITAB1.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1 .

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1 .

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1 .

SORT itab1 BY SALES.

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

Read only

Former Member
0 Likes
1,131

Hi RAvi,

In append statement you no need to write sorted by. Just write Append ITAB1.

If you want to sort the table by Sales the use this follwoing after the last append statement:

SORT ITAB1 BY SALES.

Reward points if helpful answer.

Ashvender

Read only

Former Member
0 Likes
1,131

hi there,

You please try this.

Declaration is ok.

avoid sorting all the time.

itab1-sales = 100 .

itab1-name = 'jack' .

append itab1 .

itab1-sales = 200 .

itab1-name = 'jacL' .

append itab1 .

itab1-sales = 300 .

itab1-name = 'jacM' .

append itab1 .

*****at the end of the program before write statement sort itab1.

SORT ITAB1 BY SALES.

LOOP AT ITAB1 .

WRITE : / ITAB1-SALES , 20 ITAB1-NAME .

ENDLOOP .

REEWARDS EILL BE APPRECIATED.

Read only

Former Member
0 Likes
1,131

You try this it should work. i did not check as the server is down.

data : begin of itab1 occurs 0 with header line,

sales type p decimals 2 ,

name(10) type c ,

end of itab1 .

itab1-sales = 100 .

itab1-name = 'jack' .

append itab1 .

itab1-sales = 200 .

itab1-name = 'jacL' .

append itab1 .

itab1-sales = 300 .

itab1-name = 'jacM' .

append itab1 .

*****at the end of the program before write statement sort itab1.

SORT ITAB1 BY SALES.

LOOP AT ITAB1 .

WRITE : / ITAB1-SALES , 20 ITAB1-NAME .

ENDLOOP .

Read only

Former Member
0 Likes
1,131

Hi,

Use this code it will store in ITAB1.

DATA : BEGIN OF ITAB1 OCCURS 0,

SALES TYPE P DECIMALS 2,

NAME(10),

END OF ITAB1.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1.

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1.

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1.

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

IF HELPFULL REWARD

Read only

abdul_hakim
Active Contributor
0 Likes
1,131

DATA : BEGIN OF ITAB1 OCCURS 0,

SALES TYPE P DECIMALS 2,

NAME(10),

END OF ITAB1.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1.

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1.

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1..

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

Dont use APPEND SORTED BY..as it is used to create Ranked List..

If you still want to use APPEND SORTED BY then use the below code..

DATA : BEGIN OF ITAB1 OCCURS 3,

SALES TYPE P DECIMALS 2,

NAME(10),

END OF ITAB1.

ITAB1-SALES = 100.

ITAB1-NAME = 'JACK'.

APPEND ITAB1 SORTED BY SALES.

ITAB1-SALES = 200.

ITAB1-NAME = 'JACL'.

APPEND ITAB1 SORTED BY SALES.

ITAB1-SALES = 300.

ITAB1-NAME = 'ABCD'.

APPEND ITAB1 SORTED BY SALES.

LOOP AT ITAB1.

WRITE : / ITAB1-SALES, 20 ITAB1-NAME.

ENDLOOP.

Read only

Former Member
0 Likes
1,132

Hi,

Good question..

While doing Append we can do sorting at the same time.

I wil give you one example...

It is like Ranking of any Sprots. ( FIFA Ranking, ICC Ranking)

Say you give ranking from 1 to 10.

At any point of time u will find only 10 members in the ranking according to the points.

If a new member gains more points than the 10 nth member, he will be included in the TOP TEN and the other member will be taken off from the Ranking.

In your case....

You declared Internal table as OCCURS 0....

that means at any point of time, it can store ZERO records.

If you declare internal table as OCCURS 5...

that means at any point of time, it can store only 5 records...

If you declare internal table as OCCURS 10...

that means at any point of time, it can store only 10 records...

While using APPEND TAB SORTED BY SALES.

It sorts the record then only it appends.

Read only

former_member630092
Participant
0 Likes
1,131

Answered