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: 

Diff. between At New and On Change Of

Former Member
0 Kudos

Hi,

Could anybody exlain the difference between At New event and On Change Of event in detail? And how can we avoid the inner loops that means avoiding inner loops at internal tables.

ex: loop at it_mara.

loop at it_marc.

-


-


endloop.

endloop.

Please send the reply asap.

thanks.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

"At New" will be triggered if the key of the internal table ( uptill this field ) changes . Key of an internal table can be specified explicitly or if you donot define it all the charcater fields form the key. Sorting of an itab is necessary to use AT NEW.

DATA : BEGIN OF ITAB OCCURS 0,

A(5),

B(5),

C(5),

END OF ITAB.

... Put some vales in ITAB.

SORt ITAB.

LOOP AT ITAB,

AT NEW A.

<Only when value of A Changes.>

ENDAT

AT NEW B.

<When Combined vales of A & B Changes i.e either A or B Or both>.

ENDAT.

On CHANGE OF B.

< Every time a value of B changes >

ENDON.

ENDLOOP.

For On change of sorting is not mandatory.

Cheers.

6 REPLIES 6

Vinod_Chandran
Active Contributor
0 Kudos

Hi,

AT command is used for internal table control break processing. You can also use ON CHANGE for internal tables. But SAP suggest using AT for itabs.

Instead of nested loop, you can use the combination of DO...ENDDO and READ command.

Cheers

Vinod

Former Member
0 Kudos

hai.

the difference between at new and on change of is when you specify the field name in at new <fieldname> and if there are fields to the left of the fieldname specified in at new then the code will be excuted below the at new even if teh value of <fieldname> does not changes. where as in case of on change of <fieldname> even if you have fields towardsleft of the fieldname specified in on change of then the change of value in left of fields will not trigger the code below on change of , only the <fieldname> values has to changed to trigger the code below the on change of.

you can make use of EXIT command to skip the inner loops based on condition.

pls come back if any problem

award points if helpfull.

Former Member
0 Kudos

"At New" will be triggered if the key of the internal table ( uptill this field ) changes . Key of an internal table can be specified explicitly or if you donot define it all the charcater fields form the key. Sorting of an itab is necessary to use AT NEW.

DATA : BEGIN OF ITAB OCCURS 0,

A(5),

B(5),

C(5),

END OF ITAB.

... Put some vales in ITAB.

SORt ITAB.

LOOP AT ITAB,

AT NEW A.

<Only when value of A Changes.>

ENDAT

AT NEW B.

<When Combined vales of A & B Changes i.e either A or B Or both>.

ENDAT.

On CHANGE OF B.

< Every time a value of B changes >

ENDON.

ENDLOOP.

For On change of sorting is not mandatory.

Cheers.

andreas_mann3
Active Contributor
0 Kudos

Hi,

look F1 for both commands:

on change of is used during GET events or SELECT/ENDSELECT processing.

at new / at end of is used for itabs in loops

2) sometimes you cannot avoid an inner loop- if you are searching for morr than 1 entry- here try to use sorted tables to improve performance

regards Andreas

Former Member
0 Kudos

one more difference is that with at new you can specify only one field as

REPORT YBALTEST .

tables: agr_1251.

data itab like agr_1251 occurs 0 with header line.

select * from agr_1251 into table itab .

sort itab by agr_name.

loop at itab.

at new agr_name .

write : / itab-agr_name.

endat.

endloop.

On change of you can mention as many fields you want to , but it should be in accordance with sorting.

REPORT YBALTEST .

tables: agr_1251.

data itab like agr_1251 occurs 0 with header line.

select * from agr_1251 into table itab .

sort itab by agr_name.

loop at itab.

on change of itab-agr_name or itab-object.

write : / itab-agr_name, itab-object.

endon.

endloop.

also as you can see above you have to tell on change that this field belongs to which table.

hope it helps

regards

Former Member
0 Kudos

Hi,

Once I had bad experience of using this AT NEW and AT END events.

Once in my report when i was using this I am able to see the ******** in the fields next to the field what we give to that event.

Ex: AT END OF BUKRS.

After the bukrs field what ever the fields we have they will be display in output as **** *****.

But when I use the ON CHANGE OF the problem was solved.

Thanks.