05-internal tables

Upload: karankumar12345

Post on 09-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 05-Internal Tables

    1/108

    Internal TablesInternal Tables

  • 8/7/2019 05-Internal Tables

    2/108

    Internal Tables

    Objective:

    The following section explains :

    Defining Internal Tables

    Processing Internal Tables

    Accessing Internal Tables

    Initializing Internal Tables

  • 8/7/2019 05-Internal Tables

    3/108

    Internal tables are structured data types provided by ABAP/4.

    Internal tables cannot be accessed outside the programenvironment.

    Purpose of internal tables

    Internal tables are used to reorganize the contents of

    database tables according to the needs of your program

    Internal tables are used to perform calculations on

    subsets of database tables.

    The number of lines in an internal table is not fixed.

    Internal tables exist only during the run time of a

    program.

    Internal Tables

  • 8/7/2019 05-Internal Tables

    4/108

    Internal Tables

    Introduction to Internal Tables

    Creating Internal Tables

    Working with Internal Tables

  • 8/7/2019 05-Internal Tables

    5/108

    Internal Tables

    Introduction to Internal Tables

    What are Internal Tables ?

    Structure of Internal Tables

    Accessing Internal Tables

  • 8/7/2019 05-Internal Tables

    6/108

    Internal Tables

  • 8/7/2019 05-Internal Tables

    7/108

    Internal Tables

    Internal tables are a means of storing data with a

    particular structure in ABAP memory.

    The data is stored line-by-line in memory, and each

    line has the same structure.

    The individual columns of a line are known as the

    columns of the internal table.

  • 8/7/2019 05-Internal Tables

    8/108

    Internal TablesAn internal table is one of the two structured data

    types in ABAP/4.

    Internal tables exists only during the runtime of the

    program.

    Number of lines of an internal table are not fixed.

    There are additional commands to loop through

    internal tables and process records one by one.

  • 8/7/2019 05-Internal Tables

    9/108

  • 8/7/2019 05-Internal Tables

    10/108

    Structure of Internal TablesAn Internal Table has a header record and individual item records.

    The Header record is a temporary holding record for the

    Internal Table. The item records are the records already stored in the Internal

    Table.

    Field1 Field2 Field3 Field4 FieldX

    Header

    Item1

    Item2

    Item3

  • 8/7/2019 05-Internal Tables

    11/108

    Header Line

    Structure of Internal Tables

    Item Lines

  • 8/7/2019 05-Internal Tables

    12/108

    Accessing Internal Tables For accessing Internal Tables we have to use a

    work area as an interface for transferring data to

    and from the table.

  • 8/7/2019 05-Internal Tables

    13/108

    Accessing Internal Tables

    We can distinguish between two kinds of internal

    tables in ABAP/4:

    Internal tables with header line

    Internal tables without header line

  • 8/7/2019 05-Internal Tables

    14/108

    Accessing Internal TablesExample:-

  • 8/7/2019 05-Internal Tables

    15/108

    Internal Tables

    Creating Internal Tables

    Creating Internal Table Data Types

    Creating Internal Table Data Objects.

  • 8/7/2019 05-Internal Tables

    16/108

    Creating Internal Table

    Data Types

    To create an internal table data type, we use the

    TYPESstatement

    Syntax:-

    TYPES OCCURS .

    Example:-

    TYPES VECTOR TYPE I OCCURS 10.

  • 8/7/2019 05-Internal Tables

    17/108

    Creating Internal Table

    Data TypesExample:-

    To define a structured data type, we write:

    TYPES: BEGIN OF ,

    ,

    ..............END OF .

  • 8/7/2019 05-Internal Tables

    18/108

    Creating Internal Table

    Data TypesExample:-

    TYPES:BEGIN OF LINE,

    COLUMN1 TYPE I,

    COLUMN2 TYPE I,

    COLUMN3 TYPE I,

    END OF LINE.

    TYPES ITAB TYPE LINE OCCURS 10.

  • 8/7/2019 05-Internal Tables

    19/108

    Creating Internal Table

    Data Objects To create an internal table data object, we use the

    DATAstatement

    Syntax:-

    DATA: BEGIN OF OCCURS ,

    ,

    ..............

    END OF .

  • 8/7/2019 05-Internal Tables

    20/108

  • 8/7/2019 05-Internal Tables

    21/108

    Creating Internal TableReferring to a StructureSyntax:-

    DATA OCCURS [WITH HEADER LINE].

    DATA IFLIGHT LIKE SFLIGHT OCCURS 10.

    This example creates a data object IFLIGHT

    which has the same structure as the database table

    SFLIGHT.

  • 8/7/2019 05-Internal Tables

    22/108

    Creating Internal TableReferring to a StructureDATA IMARA LIKE MARA OCCURS 10 WITH HEADER

    LINE.

    This example creates a data object IMARA

    which has the same structure as the database table

    MARA.

  • 8/7/2019 05-Internal Tables

    23/108

    Internal Tables

    Working with Internal Tables

    Filling Internal Tables

    Reading Internal Tables

    Changing and Deleting Lines of Internal Tables

    Sorting Internal Tables

  • 8/7/2019 05-Internal Tables

    24/108

    Internal Tables

    Working with Internal Tables

    Loop Processing

    Initializing Internal Tables

  • 8/7/2019 05-Internal Tables

    25/108

    Filling Internal Tables

    Filling an Internal table:

    Append lines using APPEND command

    Insert lines using INSERT command

    Transport tables using MOVE command

  • 8/7/2019 05-Internal Tables

    26/108

    Filling Internal Tables To append a line to an internal table, use the

    APPEND statement as follows:

    Syntax:-

    APPEND [ TO|INITIAL LINE TO] .

  • 8/7/2019 05-Internal Tables

    27/108

    Filling Internal Tables

    Example:-DATA: BEGIN OF ITAB OCCURS 10,

    COL1 TYPE C,

    COL2 TYPE I,END OF ITAB.

    DO 3 TIMES.

    APPEND INITIAL LINE TO ITAB.

    ITAB-COL1 = SY-INDEX. ITAB-COL2 = SY-INDEX * 2.

    APPEND ITAB.ENDDO.

    LOOP AT ITAB.

    WRITE: / ITAB-COL1, ITAB-COL2.

    ENDLOOP.

  • 8/7/2019 05-Internal Tables

    28/108

    Filling Internal Tables

  • 8/7/2019 05-Internal Tables

    29/108

  • 8/7/2019 05-Internal Tables

    30/108

    Filling Internal Tables To insert a new line before a line in an internal table,

    we use the INSERT statement.

    Syntax :-

    INSERT [ INTO|INITIAL LINE INTO]

    [INDEX ].

  • 8/7/2019 05-Internal Tables

    31/108

    Filling Internal Tables

    Example :- DATA: BEGIN OF LINE,COL1 TYPE I,

    COL2 TYPE I,

    END OF LINE.

    DATA ITAB LIKE LINE OCCURS 10.

    DO 2 TIMES.LINE-COL1 = SY-INDEX.

    LINE-COL2 = SY-INDEX * 2.

    APPEND LINE TO ITAB.

    ENDDO.

    LINE-COL1 = 11. LINE-COL2 = 22.INSERT LINE INTO ITAB INDEX 2.

    INSERT INITIAL LINE INTO ITAB INDEX 1.

    LOOP AT ITAB INTO LINE.

    WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

    ENDLOOP.

  • 8/7/2019 05-Internal Tables

    32/108

    Filling Internal Tables

    Output :-

    line-col1 line-col2

    1 0 0

    2 1 1

    3 11 22

    4 2 4

  • 8/7/2019 05-Internal Tables

    33/108

    Filling Internal Tables

    To copy the entire contents of one internal table into

    another in one execution, use theMOVE statement

    or the assignment operator (=) as follows:

    Syntax:-

    MOVE TO .

  • 8/7/2019 05-Internal Tables

    34/108

    Filling Internal Tables

    DATA: BEGIN OF LINE,

    COL1,

    COL2,

    END OF LINE.

    DATA ETAB LIKE LINE OCCURS 10 WITH HEADERLINE.

    DATA FTAB LIKE LINE OCCURS 10.

    LINE-COL1 = 'A'. LINE-COL2 = 'B'.

    APPEND LINE TO ETAB.

    MOVE ETAB[] TO FTAB.

    LOOP AT FTAB INTO LINE.

    WRITE: / LINE-COL1, LINE-COL2.

    ENDLOOP.

  • 8/7/2019 05-Internal Tables

    35/108

    Reading Internal Tables

    Reading Internal tables line by line.

    Reading single lines using the index

    Reading single lines using a key

    Searching Internal tables for character strings

    Determining the attributes of Internal tables

  • 8/7/2019 05-Internal Tables

    36/108

    Reading Internal Tables

    Reading Internal tables line by line.

    To read an internal table into a work area line by

    line, we can use the LOOP statement.

    Syntax:-

    LOOP AT [INTO ] [FROM ] [TO ]

    [WHERE ].

    .....

    ENDLOOP.

  • 8/7/2019 05-Internal Tables

    37/108

    Reading Internal TablesDATA: BEGIN OF LINE,

    COL1 TYPE I,

    COL2 TYPE I,

    END OF LINE.

    DATA ITAB LIKE LINE OCCURS 10.

    DO 30 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX * SY-

    INDEX.

    APPEND LINE TO ITAB.

    ENDDO.LOOP AT ITAB INTO LINE FROM 10 TO 25 WHERE COL2 >

    400.

    WRITE: / SY-TABIX, LINE-COL2.

    ENDLOOP.

  • 8/7/2019 05-Internal Tables

    38/108

    Reading Internal TablesExample:-Output:-

    line-col221 441

    22 484

    23 529

    24 576

    25 625

  • 8/7/2019 05-Internal Tables

    39/108

    Reading Internal Tables

    Reading single lines using the index

    To read a single line from an internal table using

    its index, we use theREAD statement as follows:

    Syntax:-

    READ TABLE [INTO ] INDEX .

  • 8/7/2019 05-Internal Tables

    40/108

    Reading Internal Tables

    DATA: BEGIN OF ITAB OCCURS 10,

    COL1 TYPE I,

    COL2 TYPE I,

    END OF ITAB.

    DO 20 TIMES.

    ITAB-COL1 = SY-INDEX.

    ITAB-COL2 = 2 * SY-INDEX.

    APPEND ITAB.

    ENDDO.READ TABLE ITAB INDEX 7.

    WRITE: / ITAB-COL1, ITAB-COL2.

  • 8/7/2019 05-Internal Tables

    41/108

    Reading Internal Tables

    Reading single lines using self-defined keys:-

    Syntax:-

    READ TABLE [INTO ] WITH

    KEY [BINARY SEARCH].

    To read a single line from an internal table with a self-

    defined key, we use the WITH KEY option of the

    READ statement as follows:

  • 8/7/2019 05-Internal Tables

    42/108

  • 8/7/2019 05-Internal Tables

    43/108

    Reading Internal Tables

    Searching Internal tables for character strings

    We can search internal tables for a specific

    pattern with the statement SEARCH as follows:

    Syntax :-

    SEARCH FOR .

  • 8/7/2019 05-Internal Tables

    44/108

    Reading Internal Tables

    Searching Internal tables for character strings

    Options :-

    STARTING AT

    ENDING AT

    AND MARK

  • 8/7/2019 05-Internal Tables

    45/108

    Reading Internal Tables

    Determining the attributes of Internal tables

    If we want to find out how many lines are

    contained in our internal table or how large we

    have defined our OCCURS parameter ,we use

    the DESCRIBE statement as follows:-

    Syntax:-

    DESCRIBE TABLE [LINES ]

    [OCCURS ].

  • 8/7/2019 05-Internal Tables

    46/108

    Reading Internal Tables

    Example:-

    DATA: BEGIN OF LINE,

    COL1 TYPE I,COL2 TYPE I,

    END OF LINE.

    DATA ITAB LIKE LINE OCCURS 10.

    DATA: LIN TYPE I, OCC TYPE I.

    DESCRIBE TABLE ITAB LINES LIN OCCURS OCC.WRITE: / LIN, OCC.

    This produces the following output:0 10

  • 8/7/2019 05-Internal Tables

    47/108

    Changing and Deleting Lines

    of Internal TablesChanging Lines:-

    Changing lines with MODIFY.

    Deleting Lines:-

    Deleting Lines in a Loop

    Deleting Lines Using the Index

    Deleting Adjacent Duplicate Entries

    Deleting Selected Lines

  • 8/7/2019 05-Internal Tables

    48/108

    Changing and Deleting Lines

    of Internal TablesChanging lines with MODIFY:-

    Syntax :-

    MODIFY [FROM ] [INDEX ]

    [TRANSPORTING ... [WHERE

    ]].

  • 8/7/2019 05-Internal Tables

    49/108

    Changing and Deleting Lines

    of Internal TablesExample:-DATA: BEGIN OF LINE,

    COL1 TYPE I,

    COL2 TYPE I,

    END OF LINE.

    DATA ITAB LIKE LINE OCCURS 10.

    DO 3 TIMES.

    LINE-COL1 = SY-INDEX.

    LINE-COL2 = SY-INDEX ** 2.

    APPEND LINE TO ITAB.

    ENDDO.LINE-COL1 = 10. LINE-COL2 = 10 ** 2 .

    MODIFY ITAB FROM LINE INDEX 2.

    LOOP AT ITAB INTO LINE.

    WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

    ENDLOOP.

  • 8/7/2019 05-Internal Tables

    50/108

    Changing and Deleting Lines

    of Internal Tables

    Example:-

    Output:-

    sy-tabix line-col1 line-col2

    1 1 12 10 100

    3 3 9

  • 8/7/2019 05-Internal Tables

    51/108

    Changing and Deleting Linesof Internal Tables

    Deleting Lines in a Loop :-

    To delete lines from an internal table in a loop, we usethe DELETE statement

    Syntax :-

    DELETE .

    The system can process this statement only within a

    LOOP - ENDLOOPblock

  • 8/7/2019 05-Internal Tables

    52/108

    Changing and Deleting Linesof Internal Tables Deleting Lines Using the Index :-

    To delete lines using the index, weuse the INDEXoption with the DELETE statement

    Syntax :-

    DELETE INDEX .

  • 8/7/2019 05-Internal Tables

    53/108

    Changing and Deleting Lines

    of Internal Tables Deleting Adjacent Duplicate Entries :-

    Syntax :-

    DELETE ADJACENT DUPLICATE ENTRIES

    FROM [COMPARING ].

  • 8/7/2019 05-Internal Tables

    54/108

    Changing and Deleting Lines

    of Internal Tables Deleting Adjacent Duplicate Entries :-

    Options:- Without the COMPARING option, the contents of the

    standard key fields must be the same

    With the COMPARING optionCOMPARING ... ,

    With the COMPARING option

    COMPARING ALL FIELDS ,

  • 8/7/2019 05-Internal Tables

    55/108

    Changing and Deleting Lines

    of Internal Tables Deleting Selected Lines :-

    Syntax :-

    DELETE [FROM ] [TO ] [WHERE

    ].

  • 8/7/2019 05-Internal Tables

    56/108

    Sorting Internal Tables

    Sorting

    SORT table_name.

    SORT table_name BY field1 field2.

    SORT table_name BY field1 DESCENDING. Default sorting is always ascending.

    SORT without field names sorts the complete record (only

    non-numeric fields are considered as sort criteria)

  • 8/7/2019 05-Internal Tables

    57/108

    Loop Processing

    Calculating Totals:-

    To add up the contents of numeric fields in an

    internal table during loop processing,we use the

    SUM statement.

    Syntax :-

    SUM.

    The system can process this statement only within a

    LOOP - ENDLOOP block.

  • 8/7/2019 05-Internal Tables

    58/108

    DATA: BEGIN OF LINE,

    COL1 TYPE I,COL2 TYPE I,

    END OF LINE.

    DATA ITAB LIKE LINE OCCURS 10.

    DO 3 TIMES.

    LINE-COL1 = SY-INDEX.

    LINE-COL2 = SY-INDEX ** 2.

    APPEND LINE TO ITAB.

    ENDDO.

    LOOP AT ITAB INTO LINE.WRITE: / LINE-COL1, LINE-COL2.

    SUM.

    WRITE: / LINE-COL1, LINE-COL2.

    ENDLOOP.

  • 8/7/2019 05-Internal Tables

    59/108

    Loop ProcessingThe Output is as follows:-

    line-col1 line-col21 1

    6 14

    2 4

    6 14

    3 9

    6 14

  • 8/7/2019 05-Internal Tables

    60/108

  • 8/7/2019 05-Internal Tables

    61/108

  • 8/7/2019 05-Internal Tables

    62/108

    Using Control Levels

  • 8/7/2019 05-Internal Tables

    63/108

    Using Control Levels

    Example:

    TYPES

    : BEGIN OF LINE,COL1 TYPE C,

    COL2 TYPE I,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.

    LINE-COL1 = A. LINE-COL2 = 2.APPEND LINE TO TAB1.

    LINE-COL1 = B. LINE-COL2 = 1.

    APPEND LINE TO TAB1.

    LINE-COL1 = A. LINE-COL2 = 3.

    APPEND LINE TO TAB1.LINE-COL1 = C. LINE-COL2 = 4.

    APPEND LINE TO TAB1.

    SORT TAB1 BY COL1.

    Using Control Levels

  • 8/7/2019 05-Internal Tables

    64/108

    Using Control Levels

    Cont..

    LOOP AT TAB1.AT FIRST.

    WRITE:/ HEADING.ENDAT.

    NEW COL1.WRITE:/ TAB1-COL1.ENDAT.

    AT END OF COL1.SUM.WRITE:/ TAB1-COL1, TAB1-COL2.

    ENDAT.LAST.

    WRITE:/ TAB1-COL1, TAB1-COL2. ENDENDLOOP.

  • 8/7/2019 05-Internal Tables

    65/108

    Initializing Internal Tables

    Clear

    initialize header line fields.Syntax

    CLEAR . Refresh

    delete all lines of a table

    does NOT initialize the header line

    Syntax

    REFRESH .

  • 8/7/2019 05-Internal Tables

    66/108

    Initializing Internal Tables

    Free

    deletes all lines of a table and frees the

    corresponding memorySyntax

    FREE .

    Initializing Internal Tables

  • 8/7/2019 05-Internal Tables

    67/108

    Example:

    TYPES

    : BEGIN OF LINE,COL1 TYPE C,

    COL2 TYPE I,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.

    LINE-COL1 = A. LINE-COL2 = 2.APPEND LINE TO TAB1.

    LINE-COL1 = B. LINE-COL2 = 1.

    APPEND LINE TO TAB1.

    CLEAR TAB1.

    REFRES

    H TAB1.IF TAB1 IS INITIAL.

    WRITE:/ TAB1 IS EMPTY.

    FREE TAB1.

    ENDIF.

    Initializing Internal Tables

  • 8/7/2019 05-Internal Tables

    68/108

  • 8/7/2019 05-Internal Tables

    69/108

    Types of internal tables

  • 8/7/2019 05-Internal Tables

    70/108

    There are two kinds of internal tables in ABAP/4:

    Internal tables with header line

    If you create an internal table with header line, the system

    automatically creates a work area with the same data type as

    the lines of the internal table.

    Note : Work area has the same name as the internal table.

    Internal tables without header line

    Internal tables without a header line do not have a table work

    area which can be used implicitly you must specify a workarea explicitly.

    yp

    Creating Internal Tables

  • 8/7/2019 05-Internal Tables

    71/108

    There are two ways by which you can create internal tables

    First create an internal table data type using the 'TYPES'

    statement and then create a data object referring that data

    type.

    Syntax : TYPE

    S OCC

    URS

    Eg: TYPES : BEGIN OF LINE,

    COLUMN1 TYPE I,

    COLUMN2 TYPE I,

    COLUMN3 TYPE I,

    END OF LINE.

    TYPES ITAB TYPE LINE OCCURS 10.

    g

    Creating Internal Tables

  • 8/7/2019 05-Internal Tables

    72/108

    Create an internal table data object by referring to a

    structure.

    Syntax: DATA [WITH HEADER LINE]

    Eg: TYPES : BEGIN OF LINE,COLUMN1 TYPE I,

    COLUMN2 TYPE I,

    COLUMN3 TYPE I,

    END OF LINE.

    TYPES ITAB TYPE LINE OCCURS 10.

    DATA ITAB1 TYPE ITAB.DATA ITAB2 LIKE ITAB1 WITH HEADER LINE.

    g

    Creating Internal Tables

  • 8/7/2019 05-Internal Tables

    73/108

    Creating Internal Tables with header line

    In this method an internal table is created with reference to

    existing structure ( another internal table or Dictionary

    object).

    Eg: DATA ITAB LIKE SFLIGHT OCCURS 10 WITH HEADER

    LINE.

    g

    Creating Internal Tables

  • 8/7/2019 05-Internal Tables

    74/108

    Create an internal table data object directly with the 'DATA'

    statement.

    Eg: DATA : BEGIN OF ITAB OCCURS 0,

    COLUMN1 TYPE I,COLUMN2 TYPE I,

    COLUMN3 TYPE I,

    END OF ITAB.

    Note : In this method a header line with same name as theinternal table is created automatically.

    g

    Filling Internal Tables

  • 8/7/2019 05-Internal Tables

    75/108

    Appending Lines:To append a line to an internal table, use the APPEND

    statement as follows:

    Syntax

    APPEND [ TO] .

    Eg: TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.

    LINE-COL1 = A. LINE-COL2 = 1.

    APPEND LINE TO TAB1.

    g

  • 8/7/2019 05-Internal Tables

    76/108

    Filling Internal Tables

  • 8/7/2019 05-Internal Tables

    77/108

    Eg: TYPES : BEGIN OF ITAB1 OCCURS 0,

    COL1 TYPE C,COL2 TYPE I,

    END OF ITAB1.

    ITAB1-COL1 = A. ITAB1-COL2 = 1.

    COLLECT TAB1.

    ITAB1-COL1 = B. ITAB1-COL2 = 2.COLLECT TAB1.

    ITAB1-COL1 = A. ITAB1-COL2 = 1.

    COLLECT TAB1.

    This Produces the output as follows:A 2

    B 2

    g

    Filling Internal Tables

  • 8/7/2019 05-Internal Tables

    78/108

    Inserting lines :

    To insert a new line before a line in an internal table, you usethe INSERT statement as follows:

    Syntax

    INS

    ERT [ INTO] [INDEX ].

    Eg: TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.LINE-COL1 = A. LINE-COL2 = 1.

    INSERT LINE INTO TAB1 INDEX 2.

    g

    Filling Internal Tables

  • 8/7/2019 05-Internal Tables

    79/108

    COPYING INTERNAL TABLES :

    To copy the entire contents of one internal table into another,

    use theMOVE statement.

    Syntax:

    MOVE to

    Eg: TYPES : BEGIN OF LINE,COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.DATA TAB2 LIKE LINE OCCURS 10.

    LINE-COL1 = A. LINE-COL2 = 1.

    APPEND LINE TO TAB1.

    MOVE TAB1[ ] TO TAB2.

    g

    Filling Internal Tables

  • 8/7/2019 05-Internal Tables

    80/108

    Filling Internal Table from Database Table:

    ExampleTABLESSPFLI.

    DATA ITAB LIKE SPFLI OCCURS 10 WITH HEADER

    LINE.

    SELECT * FROMSPFLI INTO TABLE ITAB

    WHERE CARRID = 'LH'.

    LOOP AT ITAB.

    WRITE: / ITAB-CONNID, ITAB-CARRID.

    ENDLOOP.In this example, all lines from the database tableSPFLI in which

    CARRID field contains "LH" are read into the internal table ITAB,

    where they can be processed further.

    Filling Internal Tables

  • 8/7/2019 05-Internal Tables

    81/108

    To read data component by component into the internal table.

    TABLESSPFLI.

    DATA: BEGIN OF ITAB OCCURS 0,

    NUMBER TYPE I VALUE 1,

    CITYFROM LIKE SPFLI-CITYFROM,

    CITYTO LIKE SPFLI-CITYTO,

    END OF ITAB.

    SELECT * FROMSPFLI WHERE CARRID = 'LH'.

    MOVE-CORRESPONDINGSPFLI TO ITAB.

    APPEND ITAB.

    ENDSELECT.

    In this example, all lines from the database tableSPFLI in which

    CARRID field contains "LH" are read into the internal table ITAB

    one by one, where they can be processed further.

    Reading Internal Tables

  • 8/7/2019 05-Internal Tables

    82/108

    To read the contents of internal tables for further processing,

    you can use either the LOOP or the READ statement.

    Reading Internal Tables Line by Line

    You use the the LOOP statement to read internal tables line by

    line.

    Reading Internal Tables

  • 8/7/2019 05-Internal Tables

    83/108

    LOOP AT [INTO ] [WHERE ].

    .....

    ENDLOOP.

    Eg: DO 3 TIM

    ES

    .LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX *

    SY-INDEX.

    APPEND LINE TO TAB1.

    ENDDO.

    LOOP AT TAB1 WHERE COL1 > 2.

    WRITE : / TAB1-COL1.ENDLOOP.

    Reading Internal Tables

  • 8/7/2019 05-Internal Tables

    84/108

    You can select a single line by the READ statement:

    Syntax:

    READ TABLE [INTO ] WITH KEY [BINARY

    SEARCH].

    Reading Internal Tables

  • 8/7/2019 05-Internal Tables

    85/108

    Eg:

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.DATA TAB1 LIKE LINE OCCURS 10.

    DO 3 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX ** 2.

    APPEND LINE TO TAB1.

    ENDDO.

    READ TABLE TAB1 INTO LINE WITH KEY COL2 = 4.

    Reading Internal Tables

  • 8/7/2019 05-Internal Tables

    86/108

    The COMPARING addition, the specified table fields of

    the structured line type are compared with the corresponding

    fields of the work area before being transported. If the

    contents of the compared fields are the same, SY-SUBRC is

    set to 0. If the contents of the compared fields are not thesame, it returns the value 2.

    Syntax:

    READ TABLE [INTO ] INDEX COMPARING

    .

    Reading Internal Tables

  • 8/7/2019 05-Internal Tables

    87/108

    Eg:

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.DO 3 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX ** 2.

    APPEND LINE TO TAB1.

    ENDDO.

    READ TABLE TAB1 INTO LINE INDEX 2 COM

    PARING COL1COL2.

    Modifying Internal tables

  • 8/7/2019 05-Internal Tables

    88/108

    You can modify single line usingMODIFY statement:

    Syntax :

    MODIFY itab [FROM wa] [INDEX idx].

    Modifying Internal tables

  • 8/7/2019 05-Internal Tables

    89/108

    Eg:

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.DATA TAB1 LIKE LINE OCCURS 10.

    DO 3 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX ** 2.

    APPEND LINE TO TAB1.

    ENDDO.

    LINE-COL1 = A.MODIFY TAB1 FROM LINE INDEX 2.

  • 8/7/2019 05-Internal Tables

    90/108

    Modifying Internal tables

  • 8/7/2019 05-Internal Tables

    91/108

    Eg:

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.

    DO 3 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX ** 2.

    APPEND LINE TO TAB1.

    ENDDO.

    LINE-COL1 = A.MODIFY TAB1 FROM LINE TRANSPORTING COL1

    WHERE COL2 = 4.

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    92/108

    To delete lines from an internal table in a loop:

    Syntax:

    DELETE .

    Note: The System can process this statement only within an

    LOOP..ENDLOOP block.

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    93/108

    Eg: DO 3 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX *

    SY-INDEX.

    APPEND LINE TO TAB1.ENDDO.

    LOOP AT TAB1.

    IF TAB1-COL1 > 2.

    DELETE TAB1.

    ENDIF.ENDLOOP.

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    94/108

    TO delete the lines using Index.

    Syntax:

    DELETE INDEX .

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    95/108

    Eg:

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.

    DO 3 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX * SY-INDEX.

    APPEND LINE TO TAB1.

    ENDDO.DELETE TAB1 INDEX 2.

  • 8/7/2019 05-Internal Tables

    96/108

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    97/108

    Eg:

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.LINE-COL1 = A. LINE-COL2 = 1.

    APPEND LINE TO TAB1.

    LINE-COL1 = A. LINE-COL2 = 2.

    APPEND LINE TO TAB1.

    DELETE ADJACENT DUPLICATE ENTRIES FROM TAB1

    COMPARING COL1.

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    98/108

    TO delete the Adjacent Duplicates from the Internal Table.

    Syntax:

    DELETE ADJACENT DUPLICATE ENTRIES FROM [

    COMPARING

    ]

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    99/108

    Eg:TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.

    LINE-COL1 = A. LINE-COL2 = 2.APPEND LINE TO TAB1.

    LINE-COL1 = A. LINE-COL2 = 2.

    APPEND LINE TO TAB1.

    DELETE ADJACENT DUPLICATE ENTRIES FROM TAB1

    COMPARING ALL FIELDS.

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    100/108

    TO delete a set of selected lines from the Internal Table.Syntax:

    DELETE [FROM ] [TO ] [WHERE

    ].

    Deleting Internal tables

  • 8/7/2019 05-Internal Tables

    101/108

    Eg:

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE N,

    END OF LINE.DATA TAB1 LIKE LINE OCCURS 10.

    LINE-COL1 = A. LINE-COL2 = 1.

    APPEND LINE TO TAB1.

    LINE-COL1 = A. LINE-COL2 = 2.

    APPEND LINE TO TAB1.

    DELETE TAB1 WHERE COL2 = 2.

  • 8/7/2019 05-Internal Tables

    102/108

  • 8/7/2019 05-Internal Tables

    103/108

    Loop Processing

  • 8/7/2019 05-Internal Tables

    104/108

    Calculating the totals within loopendloop.

    Syntax: SUM

    Loop Processing

  • 8/7/2019 05-Internal Tables

    105/108

    Eg.

    TYPES : BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE I,

    END OF LINE.

    DATA TAB1 LIKE LINE OCCURS 10.DO 3 TIMES.

    LINE-COL1 = SY-INDEX. LINE-COL2 =SY-INDEX ** 2.

    APPEND LINE TO TAB1.

    ENDDO.

    LOOP AT TAB1.

    SUM.ENDLOOP.

  • 8/7/2019 05-Internal Tables

    106/108

    Using Control Levels

  • 8/7/2019 05-Internal Tables

    107/108

    The line condition , at which the statement block within

    AT-ENDAT. Meaning

    FIRST First line of the internal table

    LAST Last line of the internal tableNEW Beginning of a group of line with same

    contents in the fields & in the fields of .

    END OF End of a group of line with same contents

    in the fields & in the fields of .

    Note: Before working with control breaks, You should sort theinternal table in the same order as its columns are defined.

  • 8/7/2019 05-Internal Tables

    108/108