Lecture 6 Internal Tables BCO5647 Applications Programming Techniques (ABAP)
Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Kruger   Chapter 4 Section  4.7.1 – 4.7.3 </li></ul><ul>...
What is an internal table ? <ul><li>Internal tables (arrays) are data objects that allow you to retain several data record...
Defining an Internal Table <ul><li>Basic ways of defining internal tables with and without header lines. </li></ul><ul><li...
Filling an internal table <ul><li>Tables can be filled with data by: </li></ul><ul><li>Reading data from a database table ...
Filling an internal table – Example 1 A table  customers  has the following fields : cnum customer number cname customer n...
Filling an internal table – Example 2 A table  customers  has the following fields : cnum customer number cname customer n...
Filling an internal table  (cont’d) <ul><li>Inserting lines at a specified position. </li></ul><ul><li>Single lines or a b...
Filling an internal table  (cont’d) <ul><li>Moving complete tables </li></ul><ul><li>An internal table can be filled with ...
Sorting an Internal Table <ul><li>Data in an internal table can be sorted in a number of ways: </li></ul><ul><li>Examples:...
Retrieving lines <ul><li>Once an internal table has been filled, data can be retrieved by reading each line of the table u...
Retrieving lines (cont’d) <ul><li>3) read table stud_tab index 5. if sy-subrc = 0. write / stud_tab-studid. else. write / ...
Changing an internal table <ul><li>The contents of a given line in an internal table can be updated using the  modify  com...
Changing an internal table  -  Example. <ul><li>report  chgtabex. tables: customers. data: cust_info  like  customers occu...
Other table commands <ul><li>clear  tablename. Clears the header record of the internal table. </li></ul><ul><li>refresh  ...
Upcoming SlideShare
Loading in...5
×

Lecture06 abap on line

701

Published on

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
701
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
118
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Multitudes of records can be processed using internal tables – a very important part of ABAP programming ! Because internal tables are held in the current program’s memory space, the access time to read and process the data stored inside the internal tables is significantly less than a read of the database table. The trade-off is CPU time. The header record space holds a copy of whichever record has been read or modified, as well as any new record being added to the table. Individual data records in an internal table are known as table rows or table entries. Individual components in a row are referred to as columns of the internal table.
  • To define an internal table body, use occurs n on the definitions of any field string. To define an internal table with a header line, you must include either begin of or with header line in the definition. The only time you would create an internal table without a header line is in the case of a nested internal table. The body of the internal table and the header line have the same name. Where you use that name determines what you are referring to. Example : data: begin of table1 occurs 0, fld1(1), fld2(2), end of table1. The fieldnames in the above example will be prefixed by the table name. Example : table1-fld1. If you cannot estimate the size of a table, set it to zero. The system will increase it as required. A table set to occurs 0 takes up 8-16K,
  • You can read specific fields directly into an internal table using: select no name from vendor into table vendtab. Normally the previous content of the internal table is overwritten by the new data. If appending is used, the new lines are added at the end of the table. The append statement adds the content of the header line to the end of the internal table. The contents of an internal table can be appended to another table using the addition lines of . e.g. append lines of table1 from 10 to 20 to table2.
  • In example 1, the order is implicitly determined by the sequence of fields in the line structure of the record; i.e. the left-most column represents the highest sort criterion. Note that only non-numeric fields are considered as sort criteria in this form of the statement. In example 2, studname takes precedence, within that the table is sorted by studid.
  • Example 1. These statements display the student id and name for all entries of the internal table stud_tab. During the loop, the header line is filled with the current line of the table at each step. Example 2. The where clause can support all conditional operators that are supported for the if statement. During the loop the current line number is held in sy-tabix. Loops in internal tables can be nested. In addition you can specify the range of lines you want to work with. e.g. loop at stud_tab from 10 to 20.
  • Reading single lines. Example 3. This reads the fifth line in the table. If the table does not have at least 5 entries, the return code sy-subrc returns a non-zero result. Example 4. If this read is successful, the index of the line is returned in sy-tabix. Example 5. Use of multiple keys. Example 6. The binary search algorithm requires that the table entries are sorted according to the specified key. This is a much faster way of locating entries in large tables.
  • Insert : e.g. insert stud_tab index sy-tabix. Inserts before the current line. Delete : e.g. delete stud_tab index sy-tabix.
  • Advanced commands - ref. to on-line Help.
  • Lecture06 abap on line

    1. 1. Lecture 6 Internal Tables BCO5647 Applications Programming Techniques (ABAP)
    2. 2. Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Kruger Chapter 4 Section 4.7.1 – 4.7.3 </li></ul><ul><li>Objectives This lecture will </li></ul><ul><li>Introduce the structure of an Internal Table </li></ul><ul><li>Explore ways of defining an Internal Table in ABAP </li></ul><ul><li>Explore ways of filling an Internal Table in ABAP </li></ul><ul><li>Examine how to sort an Internal Table </li></ul><ul><li>Examine how to retrieve lines form an Internal Table </li></ul>
    3. 3. What is an internal table ? <ul><li>Internal tables (arrays) are data objects that allow you to retain several data records within the same structure in working memory. </li></ul><ul><li>Internal tables have the same general structure as a database table, but are held in memory, and initially contain no records. </li></ul><ul><li>The number of data records stored in an internal table is only restricted by the capacity limits the computer system. </li></ul><ul><li>An internal table consists of a body and an optional header line . </li></ul><ul><li>The header line holds the current line of the table. </li></ul><ul><li>Internal tables are used to process large data sets in a structured manner: </li></ul><ul><ul><ul><li>Temporarily storing data from database tables for future processing; </li></ul></ul></ul><ul><ul><ul><li>Structuring and formatting data for output; </li></ul></ul></ul><ul><ul><ul><li>Formatting data for the use of other services. </li></ul></ul></ul>
    4. 4. Defining an Internal Table <ul><li>Basic ways of defining internal tables with and without header lines. </li></ul><ul><li>data: itabsbook like sbook occurs 0. ( no header) </li></ul><ul><li>data: itabsbook like sbook occurs 0 with header line. ( header ) </li></ul><ul><li>data: begin of itabsbook occurs 0, f1, f2, f3, end of itabsbook. ( header) </li></ul><ul><li>data: itabsbook type table of sbook, wa_sbookinfo like line of itabsbook. </li></ul><ul><li>(user-defined work area in place of header) </li></ul>
    5. 5. Filling an internal table <ul><li>Tables can be filled with data by: </li></ul><ul><li>Reading data from a database table e.g </li></ul><ul><li>tables: vendor. </li></ul><ul><li>data vendtab like vendor occurs 0 with header line. select * from vendor into table vendtab. </li></ul><ul><li>Or </li></ul><ul><li>s elect * from vendor appending table vendtab . </li></ul><ul><li>Appending lines e.g </li></ul><ul><li>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. </li></ul>
    6. 6. Filling an internal table – Example 1 A table customers has the following fields : cnum customer number cname customer name caddress customer address cphone 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.
    7. 7. Filling an internal table – Example 2 A table customers has the following fields : cnum customer number cname customer name caddress customer address cphone 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.
    8. 8. Filling an internal table (cont’d) <ul><li>Inserting lines at a specified position. </li></ul><ul><li>Single lines or a block of lines can be inserted in a table before a specified line number. All subsequent entries are moved down. </li></ul><ul><li>Examples : </li></ul><ul><li>insert vendtab index 3. </li></ul><ul><li>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. </li></ul><ul><li>Lines 2 to 5 of the internal table new_vend will be inserted before line 3 of vendtab. </li></ul>
    9. 9. Filling an internal table (cont’d) <ul><li>Moving complete tables </li></ul><ul><li>An internal table can be filled with the contents of another one in a single step by using the move command. </li></ul><ul><li>Examples : </li></ul><ul><li>move new_vend to vendtab. </li></ul><ul><li>Note : If an internal table with a header line is involved, this header line (but not the internal table itself) is copied by move. </li></ul>
    10. 10. Sorting an Internal Table <ul><li>Data in an internal table can be sorted in a number of ways: </li></ul><ul><li>Examples: </li></ul><ul><li>data stud_tab like student occurs 10. </li></ul><ul><li>1) sort stud_tab. 2) sort stud_tab by studname, studid. </li></ul><ul><li>3) sort stud_tab by stud_id descending. </li></ul>
    11. 11. Retrieving lines <ul><li>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 : </li></ul><ul><li>1) loop at stud_tab. write / stud_tab-studid. write stud_tab-studname. endloop. </li></ul><ul><li>2) loop at stud_tab where studname = ‘Smith’. write / stud_tab-studid. endloop. </li></ul>
    12. 12. Retrieving lines (cont’d) <ul><li>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_tab with key studname = ‘Smith’ course = ‘BGCC’. 6) read table stud_tab with key studid = 3165432 binary search. </li></ul>
    13. 13. Changing an internal table <ul><li>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. </li></ul><ul><li>Lines can also be insert ed into a table and delete d from a table. </li></ul>
    14. 14. Changing an internal table - Example. <ul><li>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. </li></ul><ul><li>read table cust_info with key cnum = 3456755 binary search. if sy-subrc = 0. cust_info-cphone = ‘9654-2345’. modify cust_info index sy-tabix. endif. </li></ul><ul><li>loop at cust_info. write: / cust_info-cnum, cust_info-cname. </li></ul><ul><li>endloop. </li></ul>
    15. 15. Other table commands <ul><li>clear tablename. Clears the header record of the internal table. </li></ul><ul><li>refresh tablename. Removes all the line records in a table (not the header). </li></ul><ul><li>describe tablename. Provides information on attributes of the internal table such as number of lines used. </li></ul><ul><li>free tablename. Deletes the internal table and releases the storage. </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×