Date post: | 07-Nov-2014 |
Category: |
Technology |
Upload: | mkpatil |
View: | 1,004 times |
Download: | 6 times |
Lecture 6Internal Tables
BCO5647 Applications Programming Techniques (ABAP)
2BCO5647
Readings & Objectives
Readings
Keller & Kruger Chapter 4Section 4.7.1 – 4.7.3
Objectives
This lecture will
Introduce the structure of an Internal Table
Explore ways of defining an Internal Table in ABAP
Explore ways of filling an Internal Table in ABAP
Examine how to sort an Internal Table
Examine how to retrieve lines form an Internal Table
3BCO5647
What is an internal table ? Internal tables (arrays) are data objects that allow you to retain several data records
within the same structure in working memory.
Internal tables have the same general structure as a database table, but are held inmemory, and initially contain no records.
The number of data records stored in an internal table is only restricted by the capacitylimits the computer system.
An internal table consists of a body and an optional header line.
The header line holds the current line of the table.
Internal tables are used to process large data sets in a structured manner: Temporarily storing data from database tables for future processing; Structuring and formatting data for output; Formatting data for the use of other services.
4BCO5647
Defining an Internal TableBasic ways of defining internal tables with and without header lines.
data: itabsbook like sbook occurs 0. (no header)
data: itabsbook like sbook occurs 0 with header line. (header)
data: begin of itabsbook occurs 0,f1,f2,f3,end of itabsbook. (header)
data: itabsbook type table of sbook,wa_sbookinfo like line of itabsbook.
(user-defined work area in place of header)
5BCO5647
Filling an internal table
Tables can be filled with data by:
Reading data from a database table e.g
tables: vendor.data vendtab like vendor occurs 0 with header line.
select * from vendor into table vendtab. Orselect * from vendor appending table vendtab.
Appending lines e.g
data: begin of vendtab occurs 0,no(6) type n,name(20) type c, end of vendtab.
select * from vendor. vendtab-no = vendor-no. vendtab-name = vendor-name. append vendtab.endselect.
6BCO5647
Filling an internal table – Example 1
A table customers has the following fields :cnum customer numbercname customer namecaddress customer addresscphone customer phone no.
report intabex.tables: customers.data: begin of itabcust occurs 0,
cnum like customers-cnum, cname like customers-cname,end of itabcust.
select * from customers. move-corresponding customers to itabcust. append itabcust.endselect.
7BCO5647
Filling an internal table – Example 2
A table customers has the following fields :cnum customer numbercname customer namecaddress customer addresscphone customer phone no.
report intabex.tables: customers.data: begin of itabcust occurs 0,
cnum like customers-cnum, cname like customers-cname,end of itabcust.
select cnum cname from customers into table itabcust.
8BCO5647
Filling an internal table (cont’d)
Inserting lines at a specified position.
Single lines or a block of lines can be inserted in a table before a specified line number. All subsequent entries are moved down.
Examples :
insert vendtab index 3.
The contents of the header line will be inserted before line 3.
insert lines of new_vend from 2 to 5 into vendtab index 3.
Lines 2 to 5 of the internal table new_vend will be inserted before line 3 of vendtab.
9BCO5647
Filling an internal table (cont’d)
Moving complete tables
An internal table can be filled with the contents of another one in a single step by using the move command.
Examples :
move new_vend to vendtab.
Note :If an internal table with a header line is involved, this header line (but not the internal table itself) is copied by move.
10BCO5647
Sorting an Internal Table
Data in an internal table can be sorted in a number of ways:
Examples:
data stud_tab like student occurs 10.
1)sort stud_tab.
2)sort stud_tab by studname, studid.
3)sort stud_tab by stud_id descending.
11BCO5647
Retrieving lines
Once an internal table has been filled, data can be retrieved by reading each line of the table using a loop, or reading individual lines.
Examples :
1) loop at stud_tab.write / stud_tab-studid.write stud_tab-studname.
endloop.
2) loop at stud_tabwhere studname = ‘Smith’.write / stud_tab-studid.
endloop.
12BCO5647
Retrieving lines (cont’d)
3) read table stud_tab index 5.if sy-subrc = 0.
write / stud_tab-studid.else.
write / ‘Record not found’.endif.
4) read table stud_tab with key studid = 3064537.
if sy-subrc ……….
5) read table stud_tabwith key studname = ‘Smith’
course = ‘BGCC’.
6) read table stud_tab with key studid= 3165432 binary search.
13BCO5647
Changing an internal table
The contents of a given line in an internal table can be updated using the modify command. For example :
read table stud_tab with key studid = 3354631.
if sy-subrc = 0.stud_tab-course = ‘BBBC’.modify stud_tab index sy-tabix.
endif.
Lines can also be inserted into a table and deleted from a table.
14BCO5647
Changing an internal table - Example.
report chgtabex.tables: customers.data: cust_info like customers occurs 0
with header line.select * from customers into table cust_info.sort cust_info by cnum.read table cust_info
with key cnum = 3456755binary search.
if sy-subrc = 0.cust_info-cphone = ‘9654-2345’.modify cust_info index sy-tabix.
endif.
loop at cust_info. write: / cust_info-cnum, cust_info-cname.
endloop.
15BCO5647
Other table commands
clear tablename.Clears the header record of the internal table.
refresh tablename.Removes all the line records in a table (not the header).
describe tablename.Provides information on attributes of the internal table such as number of lines used.
free tablename.Deletes the internal table and releases the storage.