Internal table: Allocating extra memory for a structure is called as Internal table. An internal table will have a header area and a body area. The Header area can store only one record and the body area can store more no. of records depending upon the size of the table defined during declaration.
Types of Internal tables:
STRUCTURED INTERNAL TABLES:
Syntax for Internal table based on LIKE option.
DATA <ITAB> LIKE <DB TABLE> OCCURS <SIZE> [WITH HEADER LINE]
- Any variable defined for a table in ABAP using LIKE statement is by default a structure. It can hold only one record.
- If OCCURS <size> specification is added, SAP allocates Body area memory with out header line.
- If <size> is specified as '0', it adds 8 KB of data to the body area and if any other integer is given, memory equal to those no. of records is added.
- The memory size is not fixed with the size specification. It increases in the same width as specified in the OCCURS <size> specification.
- For defining Internal table with header line, we need to use the option WITH HEADER LINE.
NOTE: Internal table without header line is required in LOV concept and ALV reports.
- Internal tables with header line is used all SAP applications to program DML operations. These concepts are used in MPP and BDC also.
- If we need to populate data into internal table without header line, we need to define an explicit structure and populate data into that structure and append data from that into the body area of the internal table using APPEND or COLLECT statements.
- There are two statements for loading data into internal tables.
- Append statement is used to add header data always a new record. Whereas Collect statement verifies whether the current header data is available in the body area. If found, Integer fields are added up with the current header data value. Else, adds a new record. If there are no internal fields, no records are added.
- For displaying Internal Table data we can use the Data Base loops.
- Select - End select statement is used to fetch data from DB tables into Application Server memory. Memory created by ABAPer in form of structure or internal tables is called as Explicit memory.
- Loop - End loop is used to display data in internal tables.
- Example for using Loop - End loop and Append statements:
Wa–kunnr = 'customer 1'.
Wa–land1 = 'in'.
Wa-name1 = 'Indian'.
Append wa.
Wa–kunnr = 'customer 2'. Wa–land1 = 'in'. Wa-name1 = 'indian2'. Append wa. Loop at wa. Write: / wa-kunnr, wa-land1, wa-name1. Endloop.
- In the above example, if the internal table doesn't have the WITH HEADER LINE option, it would have generated an error.
- For such internal tables, we need populate using an work area also display using an work area.
- In the above example if header line is not specified, define another wa (wa2) of the same type, add the values into that wa2 instead of wa. and in place of append statement, use "Append WA2 to WA".
- For displaying, in Loop statement, use "Loop at wa into wa2". Then in Write statement, use the values in wa2.
- When we give Select - End Select statement, the data is retrieved from the data base table record by record. For displaying all the records in one hit to the data base, use the option INTO TABLE <INT_TAB> in the SELECT statement. In this case, there is no need of any ENDSELECT.
- By using the above method, the performance of the program increases.
EXIT:This statement is used for coming out of the loop with out checking for any condition. SORT: This statement is used to sort data of the internal table based on the field selected for sorting in the manner required.
Syntax: SORT <INTAB> [BY <FIELD[S]>] [ASCENDING / DESCENDING]
- If sorting is not specified, the sorting is done in Ascending order based on Key field.
- If no sorting mode is specified, then also sorting is done in the Ascending order based on the fields selected.
First syntax for declaring internal tables.
Data:begin of itab occurs 0,
mandt like kna1-mandt,
kunnr like kna1-kunnr,
.
.
end of itab.
Second syntax for declaring internal tables.
Data begin of <itab> occurs 0.
include structure <dbtab>.
data end of <itab>.
This syntax is used to add the same structure of data base table into current internal table or workarea with help of include statement.This syntax is used to insert or modify the data of data base table based on internal table values.
3rd Syntax a of Internal table using like and occurs options.
data <var> like <db_tab>.
ex: data wa like kna1.
wa is called as structure, number of fields in wa is same as kna1 field fields. wa can hold only one record memory.
3RD SYNTAX b
DATA : ITAB LIKE <DBTAB> [OCCURS <N>] [WITH HEADER LINE].
OCCURS <N> IS THE OPTION WHICH MAKES THE VARIABLE AS INTERNAL TABLE, BY DEFAULT THIS STATEMENT CAN ALLOCATE MEMORY FOR ONLY BODY AREA. IF ABAPER HAS TO GET BACK HEADER LINE FOR THE INTERNAL TABLE USE "WITH HEADER LINE" OPTION. IF A INTERNAL TABLE IS DECLARED WITHOUT HEADER LINE, THEN HOW TO POPULATE DATA, AFTER POPULATING HOW TO SEE THE DATA..?
IF THE INTERNAL TABLE IS NOT DECLARED WITH HEADER LINE , THEN DECLARE ANOTHER STRUCTURE FOR WITH SAME FIELDS, USING "APPEND <WA> TO <ITAB>" SYNTAX POPULATE ONE ONE RECORD INTO INTERNAL TABLE BODY AREA.
NOTE:
LOOP AT ITAB IS THE SYNTAX TO DISPLAY THE CONTENTS OF INTENRAL TABLE. BUT THIS STATEMENT CAN ALLWAYS CHECKS FOR THE INTERNAL TABLE HAS HEADER LINE, IF ITAB HAS NO HEADER LINE THEN ABAPER CAN USE "INTO" OPTION TO REDIRECT THE DATA TO DESTINATION WORKAREA.
EXAMPLE PROGRAM.
DATA ITAB LIKE MARA OCCURS 0."HAS NO HEADER
DATA WA LIKE MARA. "HAS HEADER LINE.
WA-MATNR = '1780'.
WA-MBRSH = 'P'.
WA-MTART = 'FERT'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
WA-MATNR = '1781'.
WA-MBRSH = 'P'.
WA-MTART = 'COUP'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
WA-MATNR = '1782'.
WA-MBRSH = 'M'.
WA-MTART = 'BEVR'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
WA-MATNR = '1783'.
WA-MBRSH = 'C'.
WA-MTART = 'DRIN'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
LOOP AT ITAB INTO WA.
WRITE :/ WA-MATNR , WA-MTART, WA-MBRSH, WA-MEINS.
ENDLOOP.
EXAMPLE2.
DATA ITAB LIKE MARA OCCURS 0 WITH HEADER LINE.
ITAB-MATNR = '1780'.
ITAB-MBRSH = 'P'.
ITAB-MTART = 'FERT'.
ITAB-MEINS = 'KG'.
APPEND ITAB.
ITAB-MATNR = '1781'.
ITAB-MBRSH = 'P'.
ITAB-MTART = 'COUP'.
ITAB-MEINS = 'KG'.
APPEND ITAB.
LOOP AT ITAB.
WRITE : ITAB-MATNR , ITAB-MBRSH , ITAB-MEINS , ITAB-NTART.
ENDLOOP.
SORTING THE DATA OF INTERNAL TABLE.
DATA : ITAB LIKE KNA1 OCCURS 0 WITH HEADER LINE.
SELECT * FROM KNA1 INTO TABLE ITAB.
SORT ITAB DESCENDING.
LOOP AT ITAB.
WRITE : / ITAB-KUNNR , ITAB-LAND1 , ITAB-NAME1.
ENDLOOP.
SORTING IS PERFORMED BASED ON ASCCI CHARACTERS
Notes:
data itab like kna1 occurs 0 with header line. itab key field and dababase key fields are same, is not possible to change the key fields.
Syntax 4.
if any case if the data base key field and internal table key field should be changed for report generations then use "standard" internal table.
data : itab like standard table of <dbtab>
[with unique/non-uniqe key <fld>]
[with header line]
[initial size <n>].
example :data: itab like standard table of kna1 with non-unique key name1 with header line initial size 0.
select * FROM KNA1 INTO TABLE ITAB.
SORT ITAB.
LOOP AT ITAB.
WRITE : / ITAB-KUNNR , ITAB-LAND1 , ITAB-NAME1.
ENDLOOP.
STANDARD: This internal table is used in ABAP to change Key field priority in the Internal table. Standard tables have a linear index. You can access them using either the index or the key. If you use the key, the response time is in linear relationship to the number of table entries. The key of a standard table is always non-unique, and you may not include any specification for the uniqueness in the table definition. This table type is particularly appropriate if you want to address individual table entries using the index. This is the quickest way to access table entries. To fill a standard table, append lines using the (APPEND) statement. You should read, modify and delete lines by referring to the index (INDEX option with the relevant ABAP command). The response time for accessing a standard table is in linear relation to the number of table entries. If you need to use key access, standard tables are appropriate if you can fill and process the table in separate steps.
Syntax:
data : <itab> like standard table of <db_table>
with <default/non-unique> key <fld>
with header line
initial size <n>.
example:
data : itab like standard table of kna1
with NON-UNIQUE key name1
with header line
initial size 0.
select * from kna1 into table itab.
sort itab.
loop at itab.
write : / itab-kunnr , itab-land1 , itab-name1.
endloop.
SORTED: Data is sorted into these internal tables at the time of Data population itself. These internal tables can't be resorted by ABAPer using "SORT" statement. They also have a linear key, and, like standard tables, you can access them using either the table index or the key. When you use the key, the response time is in logarithmic relationship to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique, or non-unique, and you must specify either UNIQUE or NON-UNIQUE in the table definition. Standard tables and sorted tables both belong to the generic group index tables. This table type is particularly suitable if you want the table to be sorted while you are still adding entries to it.
syntax:
data : <itab> like sorted table of <db_table>
with <default/non-unique> key <fld>
with header line
initial size <n>.
example code.
data : itab like sorted table of kna1
with non-unique key land1
with header line
initial size 0.
select * from kna1 into table itab.
loop at itab.
write : / itab-kunnr , itab-land1 , itab-name1.
endloop.
Note: For all Above Internal tables Indexing is generated automatically by sap as Row ID. Based on This index number abaper can read the requied data from Internal table body area using "read" statement.
READ STATEMENT SYNTAX.
READ TABLE <ITAB> INDEX <NUM>.
This statement for Internal tables reads Data of Internal table for Specified Index Number and Populates the data into Header area of the Internal table.
example code.
DATA : BEGIN OF ITAB OCCURS 1,
F1(10),
F2(20),
END OF ITAB.
PARAMETERS : NUM TYPE I DEFAULT 1.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'AP'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'TN'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'MH'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'UP'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'KA'.
APPEND ITAB.
READ TABLE ITAB INDEX NUM.
WRITE : ITAB-F1 COLOR 5 , ITAB-F2 COLOR 7.
Note: IF THE INDEX NUMBER IS SPECIFIED MORE THAN SIZE OF INTERNAL TABLE, THEN READ STATEMENT GETS DATA INTO HEADER OF THE LAST RECORD IN THE INTERNAL TABLE.
HASHED: These internal tables are called as Non index internal tables. As this internal body is created in Hashed format, indexing can't be generated for the records. You can only access hashed tables by specifying the key. The response time is constant, regardless of the number of table entries, since the search uses a hash algorithm. The key of a hashed table must be unique, and you must specify UNIQUE in the table definition. This table type is particularly suitable if you want mainly to use key access for table entries. Hashed tables are a useful way of constructing and using internal tables that are similar to database tables.
Syntax:
DATA : ITAB LIKE HASHED TABLE OF <DB_TAB>
WITH UNIQUE KEY <FLD_NAME>
WITH HEADER LINE
INITIAL SIZE <N>.
EXAMPLE:
DATA : ITAB LIKE HASHED TABLE OF VBAK
WITH UNIQUE KEY VBELN
WITH HEADER LINE
INITIAL SIZE 0.
SELECT * FROM VBAK INTO TABLE ITAB.
LOOP AT ITAB.
WRITE : / 'VBELN' , ITAB-VBELN.
WRITE : / 'ERDAT' , ITAB-ERDAT.
WRITE : / 'ERNAM' , ITAB-ERNAM.
ULINE.
ENDLOOP.
No comments:
Post a Comment