Skip to Content
author's profile photo Former Member
Former Member

hai abap gurus...

in a loop of an internal table, what is the use of at first &

at last statements?

What is the use of at new statement?

what is the difference between at first & at new

statements?

what is a binary search ? and how it is useful in a sorted

internal table?

when do you need to create an internal table with header

line ?and with out a header line?

what does it mean occurs 0 while creating an internal

table?

what will happen if you don't give occurs clause while

creating an internal table?

what is the difference between clear, delete & refresh

with respect to an internal table?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

8 Answers

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 11:53 AM

    Hi

    AT NEW, AT FIRST, AT END OF AND AT LAST are called control break statements in Internal tables.

    Itab has to be sorted to use these events

    AT new and AT end of is mainly used to do Totals for the qty and amount fields based on a particular field in the ITAB.

    Binary search is used to read the records in Faster manner after the internal tables is sorted by the key fields

    If you wanst to use the header line then you have to declare the internal table with header line, so that first the record will be entered into header and then it is moved to body of the internal table

    Occurs o will assign some memory for the internal table which will increse at run time depending upon the data records.

    It will become a structure if you won't give occurs 0 in the declaration

    clear : will clear the header of the ITAB

    delete : delete the Itab with data and from memory

    refresh : will remove the complete body of the ITAB.

    Reward points if useful

    Regards

    Anji

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 11:58 AM

    Hi,

    An internal table consists of a body and an optional header line.

    Header line is a implicit work area for the internal table. It depends on how the internal table is declared that the itab will have the header line or not.

    e.g.

    data: begin of itab occurs 10,

    ab type c,

    cd type i,

    end of itab. " this table will have the header line.

    data: itab1 like itab occurs 10. " table is without header line.

    The header line is a field string with the same structure as a row of the body, but it can only hold a single row.

    It is a buffer used to hold each record before it is added or each record as it is retrieved from the internal table. It is the default work area for the internal table

    check out this link:

    http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3660358411d1829f0000e829fbfe/frameset.htm

    Reward if useful.

    Regards,

    Pranathi.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 11:58 AM

    Hi,

    <b>Difference Between AT FIRST & AT NEW.</b>

    AT FIRST is used for the first record in the internal table processing. This is similar to checking

    IF SY-TABIX = 1.

    ENDIF.

    AT NEW means the new set of record for the particular field in the internal table.

    AT FIRST is executed only one time during the loop .

    AT NEW is executed every time when the field is changed .

    <b>Binary Search:</b>

    Binary Search is for improving the performance while fetching the data from internal table

    For more info see this link,

    http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/content.htm

    <b>

    Difference Between Clear,Refresh & Delete:</b>

    CLEAR <itab>.

    statement. This statement restores an internal table to the state it was in immediately after you declared it. This means that the table contains no lines. However, the memory already occupied by the memory up until you cleared it remains allocated to the table.

    If you are using internal tables with header lines, remember that the header line and the body of the table have the same name. If you want to address the body of the table in a comparison, you must place two brackets ([ ]) after the table name.

    CLEAR <itab>[].

    To ensure that the table itself has been initialized, you can use the

    REFRESH <itab>.

    statement. This always applies to the body of the table. As with the CLEAR statement, the memory used by the table before you initialized it remains allocated. To release the memory space, use the statement

    DELETE <itab>.

    statement will delete the entries in the Internal Table.

    Regards,

    Padmam.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 12:10 PM

    Hi

    1. in a loop of an internal table, what is the use of at first & at last statements?

    What is the use of at new statement? what is the difference between at first & at new statements?

    At first will be trigerred only once when u enter first in to the loop of an internal table.

    At new will be trigerred when ever u come across a new record in the internal table For ex ur internal table consists of the following data.

    A - Here at new control break statement will gets triggered.

    A

    A - Here at Last control break statement will gets triggered.

    B - Here at New control break statement will gets triggered.

    - Here at Last control break statement will gets triggered.

    C - Here at New control break statement will gets triggered.

    - Here at Last control break statement will gets triggered.

    Means at the end of each repeated record At last gets triggered.

    2. what does it mean occurs 0 while creating an internal table?

    It means at first the internal table stored 0 size in the memory dynamicallly when u execute a program based on the number of records the size get increased.

    3. what will happen if you don't give occurs clause while

    creating an internal table?

    If u dont give Occurs clause it will become a structure not an internal table.

    4. what is the difference between clear, delete & refresh with respect to an internal table?

    Clear - clears the contents of a header line.

    Refresh - clears the contents of the body.

    Delete - completely deletes records in the internal table.

    Regards

    Haritha.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 12:16 PM

    Ans: at first and at new ,at end these are loop control events.

    at first: at first occurrence of every field value it triggers.

    at last : occurrence of last field value it triggers.

    at new : at the occurrence of every new value of field.

    at end : t the occurrence of last value of field .

    occurs 0:

    occurs 0 is used increase the performance in the sense it does not use the buffer much single buffering its going to use.

    Occurs means: when ever you execute the program this occurs 2 if u give it stores 2 records and when the loop is repeating these 2 records going to body and it's again give space for another 2 rec's..

    Clear IT : means it clears the body of an internal table.

    clear IT[ ]: It clears the data of an internal table.

    delete: delete statement works based on your cond'n or to clear complete body and

    refresh It: this also clears the body of an Internal table.

    (I explained by taking one internal table as eg )

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 12:20 PM

    Hi,

    At First it will execute the first entry in loop.

    At Last it will execute the last entry in loop.

    At new it will execute every time when field receives a new value

    At first wil execute when field receives the value for first time but At new it will execute every time when field receives a new value

    Binary search is like it will compare with middle element suppose we have

    2 10 8 11 1

    after sorting

    1 2 8 10 11

    then in binary serch first control will compare suppose we have to compare with i.

    and i = 10

    then first it will compare with middle value that is 8 third entry

    as 8 is less than 11

    now 8 10 11

    Now i will be compared with the second half middle entry i.e 4th entry i.e 10

    hence we got the value only in two iterations so it improves performance

    we should use internale table without header line and create one work area to proceess that.

    If we use with header line it will create one work area same name as table but it is not advisable.

    Occurs 0 statement assigns 8 kb memory and everytime that memory is used it assigns again 8 kb.

    If we wont give occurs it wont assign memory in advance memory will be assigned only at time of use

    Clear is used for work areas and variables.

    IF you use for table use clear: itab[].

    delete for deleting tables,workares etc.

    refresh is used for tables only.

    eg refresh : itab.

    regards,

    Sudha

    reward points if useful.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 12:45 PM

    at first, at last, at new , at end of statements r called as control break stmnts.

    inorder to use these statements the precondition is that internal table should be sorted n these events r used in a loop stmt only.

    at new -> in at new based on previous value if new value is changed then it triggers

    at last -> generally used for printing total grand sum values.

    at first -> triggered only for 1st time when loop is executed.

    internal table with header line is used when we dont want to have an explicit work area to be created for our table.

    internal table with out header line is mainly useful in nesting of internal tables.

    occurs 0 indicates memory allocated 4 internal table at runtime based on records.

    if occurs keyword is not given then it is treated as a structure n not internal table.

    clear -> used 2 clear header and body of an internal table.

    delete -> to delete records of an internal table.

    refresh -> to clear header of an internal table.

    if helpful reward some points.

    with regards,

    suresh babu aluri.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 12:48 PM

    hI,

    SEE THIS PROGRAM U CAN UNDERSTAND BETTER.

    REPORT ZINTERACTIVE_FINAL.

    INCLUDE <COLOR>.

    TABLES:SFLIGHT,SPFLI,SBOOK.

    TYPES: BEGIN OF STRUCTURE,

    CITYFROM LIKE SPFLI-CITYFROM,

    CITYTO LIKE SPFLI-CITYTO,

    CARRID LIKE SPFLI-CARRID ,

    CONNID LIKE SPFLI-CONNID,

    FLDATE LIKE SFLIGHT-FLDATE,

    SEATSMAX LIKE SFLIGHT-SEATSMAX,

    SEATSOCC LIKE SFLIGHT-SEATSOCC,

    END OF STRUCTURE.

    DATA : ITAB TYPE STRUCTURE OCCURS 0.

    DATA: WA TYPE STRUCTURE.

    DATA:LINE LIKE SY-LILLI.

    SELECT SPFLICITYFROM SPFLICITYTO SPFLICARRID SPFLICONNID

    SFLIGHTFLDATE SFLIGHTSEATSMAX SFLIGHT~SEATSOCC INTO (WA-CITYFROM,

    WA-CITYTO,WA-CARRID,WA-CONNID,WA-FLDATE,WA-SEATSMAX,WA-SEATSOCC) FROM

    SPFLI INNER JOIN SFLIGHT ON SPFLIMANDT = SFLIGHTMANDT AND SPFLI~CARRID

    = SFLIGHTCARRID AND SPFLICONNID = SFLIGHT~CONNID.

    *GET SPFLI.

    *SELECT * FROM SPFLI.

    • MOVE-CORRESPONDING SPFLI TO WA.

    *ENDSELECT.

    *GET SFLIGHT.

    *SELECT * FROM SFLIGHT.

    • MOVE-CORRESPONDING SFLIGHT TO WA.

    *ENDSELECT.

    APPEND WA TO ITAB.

    ENDSELECT.

    SET PF-STATUS 'GRUND'.

    SET TITLEBAR 'GRU' .

    SORT ITAB BY CITYFROM CITYTO CARRID CONNID SEATSOCC.

    *LOOP AT ITAB INTO WA.

    *WRITE:/ WA-CARRID,WA-CONNID,WA-CITYFROM,WA-CITYTO.

    *ENDLOOP.

    LOOP AT ITAB INTO WA.

    AT FIRST.

    WRITE:/ 'START OUTPUT INTERNAL TABLE'.

    ULINE.

    ENDAT.

    AT NEW CITYFROM.

    WRITE:/ WA-CITYFROM COLOR COL_KEY.

    ENDAT.

    AT NEW SEATSOCC.

    WRITE:/10 WA-CARRID,WA-CONNID,WA-FLDATE COLOR COL_NORMAL,

    WA-SEATSOCC COLOR COL_NORMAL,WA-SEATSMAX COLOR COL_NORMAL.

    HIDE : WA-CONNID, WA-CARRID, WA-FLDATE.

    ENDAT.

    AT END OF CONNID.

    SUM.

    WRITE:/ WA-SEATSOCC COLOR COL_TOTAL UNDER WA-SEATSOCC,

    WA-SEATSMAX COLOR COL_TOTAL.

    • ULINE .

    ENDAT.

    AT END OF CARRID.

    SUM.

    • ULINE.

    WRITE:/ WA-SEATSOCC COLOR COL_TOTAL INTENSIFIED UNDER

    WA-SEATSOCC, WA-SEATSMAX COLOR COL_TOTAL INTENSIFIED.

    ULINE.

    ENDAT.

    ENDLOOP.

    CLEAR WA.

    AT LINE-SELECTION.

    CASE SY-LSIND.

    WHEN 1.

    WRITE:/' AIRLINE ', WA-CARRID ,

    /'FLIGHT CONNECTION',WA-CONNID ,

    /'FLIGHT DATE',WA-FLDATE.

    ULINE.

    SELECT * FROM SBOOK WHERE CARRID = WA-CARRID AND CONNID = WA-CONNID

    AND FLDATE = WA-FLDATE .

    WRITE :/ SBOOK-BOOKID,SBOOK-CUSTOMID,SBOOK-ORDER_DATE.

    ENDSELECT.

    CLEAR WA.

    WHEN 2.

    LINE = SY-LILLI.

    IF LINE > 4.

    LINE = LINE - 4.

    WRITE:/ 'SELECTED LINE NUMBE:',LINE.

    ENDIF.

    ENDCASE.

    RGDS,

    BHARAT.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.