15
Lecture 6 Internal Tables BCO5647 Applications Programming Techniques (ABAP)

Lecture06 abap on line

  • Upload
    mkpatil

  • View
    1.005

  • Download
    6

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Lecture06 abap on line

Lecture 6Internal Tables

BCO5647 Applications Programming Techniques (ABAP)

Page 2: Lecture06 abap on line

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

Page 3: Lecture06 abap on line

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.

Page 4: Lecture06 abap on line

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)

Page 5: Lecture06 abap on line

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.

Page 6: Lecture06 abap on line

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.

Page 7: Lecture06 abap on line

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.

Page 8: Lecture06 abap on line

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.

Page 9: Lecture06 abap on line

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.

Page 10: Lecture06 abap on line

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.

Page 11: Lecture06 abap on line

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.

Page 12: Lecture06 abap on line

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.

Page 13: Lecture06 abap on line

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.

Page 14: Lecture06 abap on line

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.

Page 15: Lecture06 abap on line

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.