05-internal tables.ppt
Post on 26-Oct-2015
152 Views
Preview:
DESCRIPTION
TRANSCRIPT
Internal TablesInternal Tables
Internal Tables
Objective:
The following section explains :
• Defining Internal Tables
• Processing Internal Tables
• Accessing Internal Tables
• Initializing Internal Tables
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
Internal Tables
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.
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.
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.
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
Accessing Internal Tables
Example:-
Internal Tables
• Creating Internal Tables
•Creating Internal Table Data Types
•Creating Internal Table Data Objects.
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.
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.
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>.
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.
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.
Internal Tables
•Working with Internal Tables
• Filling Internal Tables
•Reading Internal Tables
•Changing and Deleting Lines of Internal Tables
•Sorting Internal Tables
Filling Internal Tables
• Filling an Internal table:
•Append lines using APPEND command
•Insert lines using INSERT command
•Transport tables using MOVE command
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>.
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.
Filling Internal Tables
COLLECT Statement :- To fill an internal table with lines which have unique standard keys.
Syntax:-
COLLECT [<wa> INTO] <itab>.
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>].
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.
Filling Internal Tables
Output :-
line-col1 line-col2
1 0 0 2 1 1 3 11 22 4 2 4
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>.
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.
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
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.
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.
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>.
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.
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:
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.
.
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>].
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
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
Changing and Deleting Lines of Internal Tables
Changing lines with MODIFY:-
Syntax :-
MODIFY <itab> [FROM <wa>] [INDEX <idx>] [TRANSPORTING <f1> ... <f2> [WHERE <condition>]].
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.
Changing and Deleting Lines of Internal Tables
Example:-
Output:-
sy-tabix line-col1 line-col2 1 1 1 2 10 100 3 3 9
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
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>.
Changing and Deleting Lines of Internal Tables
• Deleting Adjacent Duplicate Entries :-
Syntax :-
DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> [COMPARING <comp>].
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 ,
Changing and Deleting Lines of Internal Tables
• Deleting Selected Lines :-
Syntax :-
DELETE <itab> [FROM <n1>] [TO <n2>] [WHERE <condition>].
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)
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>
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.
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
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.
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>.
Initializing Internal Tables
• Free
– deletes all lines of a table and frees the corresponding memory
Syntax
FREE <itab>.
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
top related