Upload
shankarmnish
View
112
Download
5
Embed Size (px)
Citation preview
Internal TablesInternal tables are data objects that allow you to retain several data records with the same structure in the working memory. In principle, the number of data records is unlimited. It is only restricted by the capacity limits of specific system installations.
The ABAP runtime system dynamically manages the length of internal tables. This removes the necessity for you to carry out any work concerning working memory management.
Internal tables are therefore a simple way of processing large data sets in a structured manner. Typical uses include:
Temporarily storing data from database tables for future processing Structuring and formatting data for output Formatting data for the use of other services
The data type of an internal table is completely specified by the following attributes:
Line type
This is the source of the attributes of the individual columns. You normally specify a structure type but any data types are possible.
Key definition
The key columns and their sequence define the criteria by which the table rows are identified. Depending on the access type, the key can be defined as unique or non-unique. In the case of unique keys, there are no multiple entries in internal tables with identical keys.
Access type
With key access - as with database tables - you access using the field contents.Example: A read access with the search term carrid = 'UA' and connid = '0007' to an internal table using the unique key CARRID and CONNID and the data shown above returns exactly one data record.
Index access: Unlike database tables, the system may number the rows for an internal table. It is therefore possible to access table rows using this index. Example: A read access to a data record with index 5 returns the fifth data record of the internal table.
Another internal table attribute is the table kind. Internal tables can be divided into three table kinds according to the access types that are possible in each case:
With STANDARD TABLES the row numbering is maintained internally. Both index and key accesses are possible.
With SORTED TABLES the data records are always sorted according to key and saved. Here too, the index is maintained internally. Both index and key accesses are possible.
With HASHED TABLES the data records are managed optimized to runtime. A unique key is required. With hashed tables only key accesses are possible.
Which table type you should use in each case depends on how that table's entries are generally going to be accessed:
For index accesses you should normally use standard tables. Sorted tables are best for unique keys and fixed sorting. With hashed tables the runtime optimization is only noticeable if the
accesses are mainly of the read type with a unique key.
Syntax
DATA itab_name TYPE table_kind TABLE OF struc_type WITH key_def.
Steps to Declare Internal Table
1. Declare the structure of internal table I.e. User defined data type with the required fields, according to the fields required from the corresponding Database table(s).
Syntax:-TYPES: BEGIN OF <TY>, F1 TYPE <REF. DATA TYPE>,
F2 TYPE <REF. DATA TYPE>, …….. …….. END OF <TY>.
2. Declare internal table I.e. Array of above structure.DATA <ITAB> TYPE TABLE OF <TY>.
Syntax to select data from Database table into internal table
SELECT <F1>
<F2>
<F3>
……..
INTO TABLE <ITAB>
FROM <DBTABLE>
WHERE <CONDITION IF ANY>.
NOTE : - Make sure that the structure (Order) of fields in the SELECT and Internal table should be same, because content of 1st field in the SELECT is transferred to the 1st field of <ITAB> and similarly 2nd, 3rd…etc.
OPERATIONS ON INTERNAL TABLES
SINGLE RECORD ACCESSES
The following single record operations are available for internal tables: In each case, wa represents a structure that must have the same type as the line type of the internal table itab.
APPEND
Appends the contents of a structure to an internal table. This operation can only be used with standard tables.
INSERT
Inserts the contents of a structure into an internal table. In a standard table these contents are appended, in a sorted table they are inserted in the right place, and in a hashed table they are inserted according to the hash algorithm.
READ
Copies the contents of an internal table row into a structure.
MODIFY
Overwrites an internal table row with the contents of a structure.
DELETE
Deletes a row of an internal table.
COLLECT
Accumulates the contents of a structure in an internal table. This statement can only be used for tables whose non-key fields are all numeric. If the keys are identical, the numeric values are added.
PROCESSING SETS OF RECORDS
The following set operations are available for internal tables: In each case, wa represents a structure that must have the same type as the line type of the internal table itab.
LOOP ... ENDLOOPThe LOOP places the rows of an internal table one by one into the structure specified in the INTO clause. All single record operations can be executed within the LOOP. In this case, for the single recor operations, the system identifies the row to be processed.
DELETEDeletes the rows of the internal table that satisfy the condition <condition>.
INSERTCopies the contents of several rows of an internal table to another internal table.
APPENDAppends the contents of several rows of an internal table to another standard table.
SYNTAX
COPYING (ADDING MULTIPLE RECORDS):
1. COPY AT THE END OF INTERNAL TABLE
APPEND LINES OF <ITAB1> FROM <N1> TO <N2> TO <ITAB2>2. COPY FROM THE GIVEN LOCATION
INSERT LINES OF <ITAB1> FROM <N1> TO <N2> TO <ITAB2> INDEX <N>NOTE: INSERT WITH NO INDEX ACTS AS APPEND ONLY. FROM <N1> TO <N2> IS OPTIONAL, IF WE IGNORE IT ,ALL THE RECORDS ARE TRANSFERRED.
ADDING SINGLE RECORD
1. ADD AT THE END OF INTERNAL TABLE
APPEND <WA> TO <ITAB>2. ADD AT THE GIVEN LOCATION
INSERT <WA> INTO <ITAB> INDEX <N>. (N>0)
FINDING NUMBER OF RECORDS
1. DESCRIBE TABLE <ITAB> LINES <V_LINES> . (V_LINES TYPE I)
SORTING
TO ARRANGE RECORDS INTO ASCENDING /DESCENDING GROUPS
SORT <ITAB> ASCENDING/DESCENDING BY <F1> <F2>.
NOTE:A. SORTING IS ASCENDING BY DEFAULT.B. THE DEFAULT KEY IS MADE UP OF THE NON-NUMERIC FIELDS OF THE TABLE
LINE IN THE ORDER IN WHICH THEY OCCUR.ACCESSING/READING SINGLE RECORD
READ TABLE <ITAB> INTO <WA> INDEX <N>.
OR
READ TABLE <ITAB> INTO <WA> WITH KEY <CONDITION> BINARY SEARCH.
NOTE: MAKE SURE THAT THE INTERNAL TABLE IS SORTED TO APPLY BINARY SEARCH.
ACCESSING MULTIPLE RECORDS
LOOP AT <ITAB> INTO <WA> FROM <N1> TO <N2>.
**** PROCESSING
ENDLOOP.
OR
LOOP AT <ITAB> INTO <WA> WHERE <CONDITION>.
**PROCESSING
ENDLOOP.
DELETING RECORDS
SINGLE RECORD : DELETE <ITAB> INDEX <N>.MULTIPLE RECORDS : DELETE <ITAB> WHERE <CONDITION>.
DELETE <ITAB> FROM <N1> TO <N2>.
DELETE ADJACENT DUPLICATES
NOTE : MAKE SURE THAT THE DUPLICATES SHOULD BE ADJACENT. WHICH CAN BE DONE THROUGH SORTING.
SO THAT SORTING THE INTERNAL TABLE IS MANDATORY
DELETE ADJACENT DUPLICATES FROM <ITAB> COMPARING <F1> <F2>.
MODIFYING
MODIFY <ITAB> FROM <WA> TRANSPORTING <F1> <F2> …. WHERE <CONDITION>
Types of Internal Tables
XANY TABLE
INDEX TABLE HASHED TABLE
STANDARD TABLE SORTED TABLE
STANDARD TABLES:
I. Are the default internal tables.II. The key of a standard table is always non-unique , so duplicates are allowed.
III. DATA <ITAB> TYPE STANDARD TABLE OF <TY>.IV. RECORDS CAN BE ACCESSED THROUGH BOTH INDEX AND CONDITIONV. <ITAB> CAN BE SORTED
VI. ACCESSING/SEARCHING TIME FOR RECORDS DEPENDS ON THE NO OF RECORDS BECAUSE SEARCHING IS EITHER LINER OR BINARY.
SORTED TABLES
I. RECORDS ARE ALWAYS IN SORTED ORDER.II. SORTED INTERNAL TABLES CANNOT BE SORTED AGAIN.
III. THE RESPONSE TIME FOR KEY ACCESS IS LOGARITHIMCALLY PROPOTIONAL TO THE NUMBER OF TABLE ENTRIES , SINCE THE SYSTEM ALWAYS USES A BINARY SEARCH
IV. RECORDS CAN BE ACCESSED THROUGH BOTH INDEX AND KEY.V. DATA <ITAB> TYPE SORTED TABLE OF <TY> WITH UNIQUE/NON-UNIQUE KEY <F1> <F2> ..
NOTE : SORTED INTERNAL TABLES ARE ALWAYS,EITHER UNIQUE/NON-UNIQUE .
HASHED TABLES
I. HASHED TABLES ALWAYS HAVE A UNIQUE KEYII. YOU CANNOT ACCESS A HASHED TABLE USING ITS INDEX
III. DATA <ITAB> TYPE HASHED TABLE OF <TY> WITH UNIQUE/NON-UNIQUE KEY <F1> <F2>..
INITIALIZING INTERNAL TABLES CLEAR <VARIABLE>.NOTE : THE VARIABLE CAN BE A NORMAL VARIABLE , WORK AREA ,INTERNAL TABLE ETC.
CLEAR <WA> - CLEARS THE WORK AREACLEAR <ITAB>-CLEARS INTERNAL TABLE
** IF <ITAB> WITH HEADER LINECLEAR <ITAB>[] – CLEARS THE INTERNAL TABLECLEAR <ITAB> - CLEARS THE WORK AREA
REFRESH – ALWAYS WORKS FOR INTERNAL TABLE ONLY.CLEARS THE ITABFREE – CLEARS THE ITAB AND RELEASE ITS MEMORY SPACE
ADDING SUMMARIZED LINES
COLLECT <WA> INTO <ITAB>.
CONTROL BREAK STATEMENTS
NOTE : FIRST SORT THE TABLE BEFORE USING CONTROL BREAK STATEMENTS.
AT <LEVEL>.
<STATEMENT BLOCK>.
ENDAT.
DIFFERENT STATEMENTS
AT FIRST – FIRST LINE OF INTERNAL TABLE
AT LAST – LAST LINE OF THE INTERNAL TABLE
AT NEW <F> - BEGINNING OF A GROUP OF LINES WITH THE SAME CONTENT IN THE FIELD <F> AND IN THE FIELD LEFT OF <F>.
AT END OF <F> - END OF A GROUP OF LINES WITH THE SAME CONTENTS IN THE FIELD <F> AND IN THE FIELDS LEFT OF <F>.