05-internal tables.ppt

51
Internal Tables

Upload: tamilove

Post on 26-Oct-2015

152 views

Category:

Documents


1 download

DESCRIPTION

internal tables in SAP abap

TRANSCRIPT

Page 1: 05-Internal Tables.ppt

Internal TablesInternal Tables

Page 2: 05-Internal Tables.ppt

Internal Tables

Objective:

The following section explains :

• Defining Internal Tables

• Processing Internal Tables

• Accessing Internal Tables

• Initializing Internal Tables

Page 3: 05-Internal Tables.ppt

Internal tables are structured data types provided by ABAP/4.Internal tables cannot be accessed outside the program environment.

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

Page 4: 05-Internal Tables.ppt

Internal Tables

Page 5: 05-Internal Tables.ppt

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.

Page 6: 05-Internal Tables.ppt

Internal Tables

An 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.

Page 7: 05-Internal Tables.ppt

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.

Page 8: 05-Internal Tables.ppt

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

Page 9: 05-Internal Tables.ppt

Accessing Internal Tables

Example:-

Page 10: 05-Internal Tables.ppt

Internal Tables

• Creating Internal Tables

•Creating Internal Table Data Types

•Creating Internal Table Data Objects.

Page 11: 05-Internal Tables.ppt

Creating Internal Table Data Types

• To create an internal table data type, we use the TYPES statement

Syntax:-TYPES <t> <type> OCCURS <n>.

Example:-

TYPES VECTOR TYPE I OCCURS 10.

Page 12: 05-Internal Tables.ppt

Creating Internal Table Data Types

Example:-

TYPES:BEGIN OF LINE, COLUMN1 TYPE I, COLUMN2 TYPE I, COLUMN3 TYPE I, END OF LINE.

TYPES ITAB TYPE LINE OCCURS 10.

Page 13: 05-Internal Tables.ppt

Creating Internal Table Data Objects

• To create an internal table data object, we use the DATA statement

Syntax:-

DATA: BEGIN OF <f> OCCURS <n>, <component declaration>, .............. END OF <f>.

Page 14: 05-Internal Tables.ppt

Creating Internal Table Data Objects

•Example:-

DATA: BEGIN OF ITAB OCCURS 10, COLUMN1 TYPE I, COLUMN2 TYPE I, COLUMN3 TYPE I, END OF ITAB.

• This example creates an internal table and the corresponding table work area ITAB.

Page 15: 05-Internal Tables.ppt

Creating Internal Table Referring to a Structure

Syntax:-

DATA <f> <type> OCCURS <n> [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.

Page 16: 05-Internal Tables.ppt

Internal Tables

•Working with Internal Tables

• Filling Internal Tables

•Reading Internal Tables

•Changing and Deleting Lines of Internal Tables

•Sorting Internal Tables

Page 17: 05-Internal Tables.ppt

Filling Internal Tables

• Filling an Internal table:

•Append lines using APPEND command

•Insert lines using INSERT command

•Transport tables using MOVE command

Page 18: 05-Internal Tables.ppt

Filling Internal Tables

• To append a line to an internal table, use the APPEND statement as follows:

Syntax:-

APPEND [<wa> TO|INITIAL LINE TO] <itab>.

Page 19: 05-Internal Tables.ppt

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.

Page 20: 05-Internal Tables.ppt

Filling Internal Tables

COLLECT Statement :- To fill an internal table with lines which have unique standard keys.

Syntax:-

COLLECT [<wa> INTO] <itab>.

Page 21: 05-Internal Tables.ppt

Filling Internal Tables

• To insert a new line before a line in an internal table, we use the INSERT statement.

Syntax :-

INSERT [<wa> INTO|INITIAL LINE INTO] <itab> [INDEX <idx>].

Page 22: 05-Internal Tables.ppt

Filling Internal TablesExample :- 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.

Page 23: 05-Internal Tables.ppt

Filling Internal Tables

Output :-

line-col1 line-col2

1 0 0 2 1 1 3 11 22 4 2 4

Page 24: 05-Internal Tables.ppt

Filling Internal Tables

• To copy the entire contents of one internal table into another in one execution, use the MOVE statement or the assignment operator (=) as follows:

Syntax:-

MOVE <itab1> TO <itab2>.

Page 25: 05-Internal Tables.ppt

Filling Internal TablesDATA: BEGIN OF LINE, COL1, COL2, END OF LINE.DATA ETAB LIKE LINE OCCURS 10 WITH HEADER LINE.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.

Page 26: 05-Internal Tables.ppt

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

Page 27: 05-Internal Tables.ppt

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 <itab> [INTO <wa>] [FROM <n1>] [TO <n2>] [WHERE <condition>]. .....ENDLOOP.

Page 28: 05-Internal Tables.ppt

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.

Page 29: 05-Internal Tables.ppt

Reading Internal Tables

• Reading single lines using the index

• To read a single line from an internal table using its index, we use the READ statement as follows:

Syntax:-

READ TABLE <itab> [INTO <wa>] INDEX <idx>.

Page 30: 05-Internal Tables.ppt

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.

Page 31: 05-Internal Tables.ppt

Reading Internal Tables

• Reading single lines using self-defined keys:-

Syntax:-

READ TABLE <itab> [INTO <wa>] WITH KEY <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:

Page 32: 05-Internal Tables.ppt

Reading Internal TablesExample:- DATA: BEGIN OF LINE, COL1 TYPE C, COL2 TYPE P DECIMALS 5, COL3 TYPE I, COL4 TYPE I, END OF LINE.DATA ITAB LIKE LINE OCCURS 10.DO 10 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SQRT( SY-INDEX ). LINE-COL3 = SY-INDEX ** 2. LINE-COL4 = SY-INDEX ** 3. APPEND LINE TO ITAB.ENDDO.READ TABLE ITAB INTO LINE WITH KEY COL3 = 9 COL4 = 36.WRITE: / SY-SUBRC, SY-TABIX.READ TABLE ITAB INTO LINE WITH KEY COL3 = 9 COL4 = 27.WRITE: / SY-SUBRC, SY-TABIX.

.

Page 33: 05-Internal Tables.ppt

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 <itab> [LINES <lin>] [OCCURS <occ>].

Page 34: 05-Internal Tables.ppt

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

Page 35: 05-Internal Tables.ppt

Changing and Deleting Lines of Internal Tables

Changing Lines:-

• Changing lines with MODIFY.

Deleting Lines:-

• Deleting Lines in a Loop • Deleting Lines Using the Index • Deleting Adjacent Duplicate Entries • Deleting Selected Lines

Page 36: 05-Internal Tables.ppt

Changing and Deleting Lines of Internal Tables

Changing lines with MODIFY:-

Syntax :-

MODIFY <itab> [FROM <wa>] [INDEX <idx>] [TRANSPORTING <f1> ... <f2> [WHERE <condition>]].

Page 37: 05-Internal Tables.ppt

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.

Page 38: 05-Internal Tables.ppt

Changing and Deleting Lines of Internal Tables

Example:-

Output:-

sy-tabix line-col1 line-col2 1 1 1 2 10 100 3 3 9

Page 39: 05-Internal Tables.ppt

Changing and Deleting Lines of Internal Tables

• Deleting Lines in a Loop :-

• To delete lines from an internal table in a loop, we use the DELETE statement

Syntax :-DELETE <itab>.

• The system can process this statement only within a LOOP - ENDLOOP block

Page 40: 05-Internal Tables.ppt

Changing and Deleting Lines of Internal Tables

• Deleting Lines Using the Index :-

• To delete lines using the index, weuse the INDEX option with the DELETE statement

Syntax :-

DELETE <itab> INDEX <idx>.

Page 41: 05-Internal Tables.ppt

Changing and Deleting Lines of Internal Tables

• Deleting Adjacent Duplicate Entries :-

Syntax :-

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> [COMPARING <comp>].

Page 42: 05-Internal Tables.ppt

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 option COMPARING <f1> <f2> ... ,

• With the COMPARING option COMPARING ALL FIELDS ,

Page 43: 05-Internal Tables.ppt

Changing and Deleting Lines of Internal Tables

• Deleting Selected Lines :-

Syntax :-

DELETE <itab> [FROM <n1>] [TO <n2>] [WHERE <condition>].

Page 44: 05-Internal Tables.ppt

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)

Page 45: 05-Internal Tables.ppt

Loop Processing• Using Control Levels for Groups of Lines:-

<line> Meaning

FIRST First line of the internal table

LAST Last line of the internal table

NEW <f> Beginning of a group of lines with the same contents in the field <f> and in the fields left of <f>

END Of <f> End of a group of lines with the same contents in the field <f> and in the fields left of <f>

Page 46: 05-Internal Tables.ppt

Using Control Levels

Hierarchy of AT-ENDAT statement.

If the internal table has the columns <col1>,<col2>,… and if it is sorted by the columns as they are defined, the loop is to be programmed as follows:

LOOP AT <itab>.

AT FIRST. …..ENDAT.

AT NEW <col1>….ENDAT.

AT NEW <col2>……ENDAT.

…….

<single line processing>

……...

AT END OF <col2>…..ENDAT.

AT END OF <col1>……ENDAT.

AT LAST……ENDAT.

ENDLOOP.

Page 47: 05-Internal Tables.ppt

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.

Page 48: 05-Internal Tables.ppt

Using Control Levels

Cont..

LOOP AT TAB1. AT FIRST.

WRITE:/ ‘HEADING’. ENDAT.AT NEW COL1.

WRITE:/ TAB1-COL1.ENDAT.AT END OF COL1.

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

ENDAT.AT LAST.

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

ENDAT.ENDLOOP.

Page 49: 05-Internal Tables.ppt

Initializing Internal Tables • Clear

– initialize header line fields.Syntax

CLEAR <itab>.

• Refresh

– delete all lines of a table– does NOT initialize the header line

Syntax

REFRESH <itab>.

Page 50: 05-Internal Tables.ppt

Initializing Internal Tables

• Free

– deletes all lines of a table and frees the corresponding memory

Syntax

FREE <itab>.

Page 51: 05-Internal Tables.ppt

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. REFRESH TAB1. IF TAB1 IS INITIAL. WRITE:/ ‘TAB1 IS EMPTY’. FREE TAB1. ENDIF.

Initializing Internal Tables