internal table final
TRANSCRIPT
-
7/31/2019 Internal Table Final
1/31
2009 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Introduction to Internal Tables- Kunal Behary
-
7/31/2019 Internal Table Final
2/31
2 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Objectives
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 from an Internal Table
-
7/31/2019 Internal Table Final
3/31
3 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
What is an internal table ? Internal tables provide a replica of tables or structure we working on
and then modifying, changing or creating values in the database tablein SAP.
-
7/31/2019 Internal Table Final
4/31
4 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Obsolete Version Of Internal Table i.e. with headerline
Example:
DATA :
BEGIN OF itab1 OCCURS 10,
f1,
f2,
f3,
END OF itab1.
DATA itab2 LIKE ztxlfa1 OCCURS 100.
DATA itab3 LIKE ztxlfa1 OCCURS 100 with header line.
BEGIN OF creates header LIKE does not create a header line LIKE add WITH HEADER LINE creates a header
line.
-
7/31/2019 Internal Table Final
5/31
5 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Internal Table without a Header Line
Separate Internal Table Work Area
Performance Issues
Nested Internal Tables
WHY???
-
7/31/2019 Internal Table Final
6/31
6 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Filling an internal table Example 1Tables can be filled with data by:
Reading data from a database table e.g
-
7/31/2019 Internal Table Final
7/31
7 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Filling an internal table Example 2
-
7/31/2019 Internal Table Final
8/318
2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Filling an internal table (contd)
Inserting lines at a specified position.
Examples :
insert vendtab index 3.
The contents of the header line will be inserted before line 3.
insert lines of new_vendfrom 2 to 5into vendtab index 3.
Lines 2 to 5 of the internal table new_vend will be inserted before line 3 ofvendtab.
-
7/31/2019 Internal Table Final
9/319 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Filling an internal table (contd)
Moving complete tables
Examples :
move new_vend to vendtab.
Note :
If an internal table with a header line is involved, this header line (but not theinternal table itself) is copied by move.
-
7/31/2019 Internal Table Final
10/3110 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
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.
-
7/31/2019 Internal Table Final
11/3111 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Retrieving lines
Once an internal table has been filled, data can be retrieved by reading eachline 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_tab
where studname = Smith.
write / stud_tab-studid.endloop.
-
7/31/2019 Internal Table Final
12/3112 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Retrieving lines (contd)
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 = Smithcourse = BGCC.
6) read table stud_tab with key studid
= 3165432 binary search.
-
7/31/2019 Internal Table Final
13/3113 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Changing an internal table
The contents of a given line in an internal table can be updated using the
modifycommand. For example :
read table stud_tabwith key studid = 3354631.
if sy-subrc = 0.
stud_tab-course = BBBC.modify stud_tab index sy-tabix.endif.
-
7/31/2019 Internal Table Final
14/3114 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Changing an internal table - Example.
TABLES: zcustomers.
DATA:
t_cust_info LIKE
STANDARD TABLE
OF zcustomers.
SELECT *
FROM zcustomersINTO TABLE cust_info.
SORT cust_info BY cnum.
READ TABLE cust_infoWITH 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.
-
7/31/2019 Internal Table Final
15/3115 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Other table commands
clear fs_area.
Clears the work area of the internal table.
refresh tablename.Removes all the line records in a table (not the header) the storage remains.
freetablename.
Deletes the internal table and releases the storage space.
-
7/31/2019 Internal Table Final
16/31
Proprietary Information. DCDM Consulting.
Not be disclosed without express written consent.
Advanced Internal Tables
-
7/31/2019 Internal Table Final
17/3117 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Objectives
Revise the structure of internal tables
Review the attributes of internal tables
Describe the three internal table kinds: standard, sorted, hashed
Examine the features and application of STANDARD internal tables
Examine the features and application of SORTED internal tables
Examine the features and application of HASHED internal tables
Distinguish between the use of work areas Vs header lines when
processing internal tables
-
7/31/2019 Internal Table Final
18/3118 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Internal Tables as Dynamic Data Objects
f1 f2
1 A XX
2 B YY
3 C YY
Itab
data: BEGIN OF itab OCCURS 10,
f1,f2,
f3,
END OF itab.
LOOP AT itab.
write: / sy-tabix, itab-f1, itab-f2.
ENDLOOP.
WRITE: / 'done. sy-tabix =', sy-tabix,
/ ' sy-subrc =', sy-subrc.OUTPUTS:
1 A XX
2 B YY
3 C YY
sy-tabix = 99
sy-subrc = 0
-
7/31/2019 Internal Table Final
19/3119 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Attributes of an Internal Table
-
7/31/2019 Internal Table Final
20/3120 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Internal Table Kinds
Internal Tables can be divided into three table kinds, depending on the
possible access type required:
-
7/31/2019 Internal Table Final
21/3121
2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Defining Internal Tables - Standard
DATA:
itab TYPE
STANDARD TABLE
OF sbookWITH NON-UNIQUE KEY bookid.
DATA: itab TYPE TABLE OF scarr.
-
7/31/2019 Internal Table Final
22/3122
2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
-
7/31/2019 Internal Table Final
23/3123
2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Defining Internal Tables: Sorted & Hashed
-
7/31/2019 Internal Table Final
24/3124
2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Work Area or Header Line
-
7/31/2019 Internal Table Final
25/3125
2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Hashed Internal Table
-
7/31/2019 Internal Table Final
26/3126
2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
-
7/31/2019 Internal Table Final
27/31
27 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
-
7/31/2019 Internal Table Final
28/31
28 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Internal Table Restricting Rows
Restricting rows read from an internal table.
Using FROM, TO and WHERE
- WHERE returns a subset but always performs a full table scan
LOOP AT itab WHERE f2= YY
LOOP AT itab from 2 to 3.
Use EXIT, CONTINUE or CHECK to control LOOP AT.
-
7/31/2019 Internal Table Final
29/31
29 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Internal Table single-record access
-
7/31/2019 Internal Table Final
30/31
30 2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.
Internal Table Kinds - Summary
Standard table
Standard tables are best when you access data using an index, that is, the order ofthe data records is important but the sorting and the uniqueness are not crucial. If you
decide you need to sort the table or access it using the key or a binary search, you
can always program these functions by hand.
Sorted table
When you choose to use a sorted table, it will normally be because you want to defineand use a unique key.
When a sorted table is created the data is first sorted and then inserted.
If you have a table with few entries but lots of accesses that change the contents, asorted table may be more efficient than a standard table in terms of runtime because
of binary search access.
-
7/31/2019 Internal Table Final
31/31
Internal Table KindsSummary (contd)
Hashed table
The hash algorithm calculates the address of an entry based on the key. This means
that, with larger tables, the access time is reduced significantly in comparison with a
binary search. But when you use a loop with a hashed table, the whole table has to
be scanned. Because the data records are usually totally unsorted, a sorted table
may be more useful if you have a loop over the beginning of a key. Alternatively, you
could also sort the hashed table.
Hashed table is only beneficial if you want to keep large amounts of data locally inthe program and you mostly access it only to read it. You must ensure that youdesign your hashed table so that it is possible to specify the full key when you access
it from your program.
Typical use for hashed tables is for buffering or bundling large amounts of data from
several database tables when an ABAP Dictionaryview or a nested SELECTstatement is not possible.