09_ABAP – Advanced Internal Tables
-
Upload
varaprasadp -
Category
Documents
-
view
228 -
download
0
Transcript of 09_ABAP – Advanced Internal Tables
-
7/30/2019 09_ABAP Advanced Internal Tables
1/46
ABAP Advanced Internal Tables
-
7/30/2019 09_ABAP Advanced Internal Tables
2/46
Contents
Objectives
Testing and Modifying Internal Table Contents
Obtaining Information about an Internal Table
Copying Data from one Internal Table to Another
Inserting Rows into an Internal Table
Modifying Rows in an Internal Table
Deleting Internal Table Contents
Filling an Internal Table Using COLLECT
Filling an Internal Table from a Database Table
Summary
Exercise / Q&A
-
7/30/2019 09_ABAP Advanced Internal Tables
3/46
Contents
Objectives
-
7/30/2019 09_ABAP Advanced Internal Tables
4/46
Objectives
Recognize the table body operator
to test for the existence of data in an itab
to compare the contents of two itabs for equality
Determine the number of rows in an itab: DESCRIBE and SY-TFILL
Copy the contents from one itab to another:
using the table body operator
the APPEND LINES and INSERT LINES statements
Use the INSERT and MODIFY statements to change the itab contents
Delete rows from an itab using DELETE, DELETE ... WHERE, CLEAR, CLEAR it[],
REFRESH, and FREE
Fill an itab using COLLECT
Fill an internal table from a database table using the most efficient constructs
-
7/30/2019 09_ABAP Advanced Internal Tables
5/46
Contents
Testing and Modifying Internal
Table Contents
-
7/30/2019 09_ABAP Advanced Internal Tables
6/46
Testing and Modifying Internal Table Contents
The table body operator: it[] DESCRIBE TABLE APPEND LINES INSERT LINES
INSERT MODIFY FREE DELETE
CLEAR REFRESH COLLECT
-
7/30/2019 09_ABAP Advanced Internal Tables
7/46
The table body operator
itab it: the header & the body
it[]:means "the body of the internal table it.
to perform table operations that do not require
the header line
if itab does not have a header line:
it~ it[]: represent the body
-
7/30/2019 09_ABAP Advanced Internal Tables
8/46
Contents
Obtaining Information about an
Internal Table
-
7/30/2019 09_ABAP Advanced Internal Tables
9/46
Obtaining Information
Information:
Whether the internal table contains data?
How many rows it contains?
Determining Whether an itab Is Empty
IF it[] IS INITIAL.
Determining the Number of Rows in an itab
use DESCRIBE TABLE statement
-
7/30/2019 09_ABAP Advanced Internal Tables
10/46
DESCRIBE TABLE Statement
Syntax: DESCRIBE TABLE it [LINES i] [OCCURS j].
where:
itis the name of an internal table.
iandjare numeric variables.
Effected system variables:
SY-TFILL: Number of rows (~ LINES i)
SY-TLENG: Length of a row in bytes
-
7/30/2019 09_ABAP Advanced Internal Tables
11/46
Obtaining Information - example
REPORT y_vu_1201.DATA: BEGIN OF it OCCURS 3,
f1 VALUE 'X',
END OF it,
n TYPE i,
m TYPE i.
IF it[] IS INITIAL.
WRITE: / 'it is empty'.
ENDIF.
APPEND: it, it.
IF NOT it[] IS INITIAL.
WRITE: / 'it is not empty'.
ENDIF.
WRITE: / 'number of rows from sy-tabix:', sy-tabix.
DESCRIBE TABLE it LINES n OCCURS m.
WRITE: / 'number of rows from sy-tfill:', sy-tfill,
/ 'length of a row from sy-tleng:', sy-tleng,
/ 'occurs value :', m.
-
7/30/2019 09_ABAP Advanced Internal Tables
12/46
Contents
Copying Data from one Internal
Table to Another
-
7/30/2019 09_ABAP Advanced Internal Tables
13/46
Copying Data
it2[] = it1[].
to duplicate the contents of one itab in another
it1 & it2: same structure
any existing contents in it2 are overwritten
the header lines remain unchanged
Copying a Portion of an itab
APPEND LINES Statement
append rows to the end
INSERT LINES Statement
insert rows at a place
-
7/30/2019 09_ABAP Advanced Internal Tables
14/46
APPEND LINES Statement
Syntax:
APPEND LINES OF it1 [FROM nf] [TO nt] TO it2.
where:
it1 and it2 are itabs with or without header lines (same structure)
nfand ntare numeric variables, literals, or constants
nf: index of the first row to be copied
nt: index of the last row to be copied
After APPEND LINES, SY-TABIX number of rows in the table
-
7/30/2019 09_ABAP Advanced Internal Tables
15/46
INSERT LINES Statement
Syntax:
INSERT LINES OF it1 [FROM nf] [TO nt] INTO it2 [INDEX nb].
where:
it1 and it2 are itabs with or without header lines (samestructure)
nf, ntand nb are numeric variables, literals, or constants
nf: index of the first row to be inserted
nt: index of the last row to be inserted
nb: insert before row number nb
-
7/30/2019 09_ABAP Advanced Internal Tables
16/46
Copying Data - examples
it1
APPEND LINES OF it1 FROM 2 TO 5 TO it2. it2
it2
it2
it2
INSERT LINES OF it1 FROM 8 INTO it2 INDEX 2.
LOOP AT it2.
IF it2-f1 >= 'E'.
INSERT LINES OF it1 TO 1 INTO it2.
ENDIF.
ENDLOOP.
it2[] = it1[].
-
7/30/2019 09_ABAP Advanced Internal Tables
17/46
Comparing the Contents of Two Internal Tables
IF it1[] = it2[].
it1, it2: same structure
TRUE:
the same number of rows
and the contents of each row are the same
-
7/30/2019 09_ABAP Advanced Internal Tables
18/46
Contents
Inserting Rows into an Internal
Table
-
7/30/2019 09_ABAP Advanced Internal Tables
19/46
INSERT statement
Syntax: INSERT [wa INTO] it[INDEX n]
where: wa: a work area (same structure as it)
n: a numeric literal, variable, or constant
NOTES: ifwa is not specified insert the header line it
insert before row n
INSERT statement can be used inside or outside of LOOPAT it
outside: INDEX addition must be specified
inside: INDEX is optional; if not specified insert current row
-
7/30/2019 09_ABAP Advanced Internal Tables
20/46
INSERT statement - examples
it-f1 = -99.
INSERT it INDEX 3.
LOOP AT it WHERE f1 >= 4.
it-f1 = -88.
INSERT it.
ENDLOOP.
-
7/30/2019 09_ABAP Advanced Internal Tables
21/46
Contents
Modifying Rows in an Internal
Table
-
7/30/2019 09_ABAP Advanced Internal Tables
22/46
MODIFY statement
Syntax:
MODIFY it[FROM wa] [INDEX n] [WHERE exp]]
where:
itis the name of an itab with or without a header line.
wa is a work area with the same structure as a row in thebody ofit.
n is a numeric literal, variable, or constant.
exp is a logical expression involving components ofit.
WHERE exp: cannot be used inside LOOP AT or with INDEX
-
7/30/2019 09_ABAP Advanced Internal Tables
23/46
MODIFY statement - examples
it-f1 = 5.
it-f2 = 'Z'.
MODIFY it INDEX 4.
LOOP AT it.
it-f1 = it-f1 * 2.
MODIFY it.
ENDLOOP.
-
7/30/2019 09_ABAP Advanced Internal Tables
24/46
Contents
Deleting Internal Table
Contents
-
7/30/2019 09_ABAP Advanced Internal Tables
25/46
Deleting Internal Table Contents
FREE statement delete all rows free the associated memory
REFRESH statement delete all rows leave the memory allocated
CLEAR statement CLEAR it[]
delete all rows leave the memory allocated
CLEAR it clear the header line
DELETE statement delete one or more rows
-
7/30/2019 09_ABAP Advanced Internal Tables
26/46
FREE and REFRESH
FREE statement Syntax: FREE it. NOTES:
All rows are deleted and all memory used by the body of the itab isfreed.
The header line, if it exists, remains unchanged.
REFRESH statement Syntax: REFRESH it. NOTES:
All rows are deleted, all memory used by the body of the itab remainsallocated.
The header line, if it exists, remains unchanged. FREE REFRESH:
Use FREE when finished using an itab before program ends Use REFRESH when delete all rows but intend to fill again
-
7/30/2019 09_ABAP Advanced Internal Tables
27/46
FREE and REFRESH examplesREPORT y_vu_1207.
DATA: BEGIN OF it OCCURS 3,f1 LIKE sy-index,
END OF it,
i LIKE sy-index.
DO 3 TIMES.
i = sy-index.DO 3 TIMES.
it-f1 = i * sy-index.
APPEND it.
ENDDO.
WRITE: / ''.
LOOP AT it.
WRITE it-f1.ENDLOOP.
REFRESH it.
ENDDO.
FREE it.
-
7/30/2019 09_ABAP Advanced Internal Tables
28/46
CLEAR Statement
Syntax: CLEARit| CLEARit[]
CLEARit[] same as REFRESH it
-
7/30/2019 09_ABAP Advanced Internal Tables
29/46
DELETE Statement
Syntax
DELETE it (a) [INDEX n]
(b) [FROM i] [TOj]
(c) [WHERE exp] where:
n, i, andjare numeric literals, variables, or constants.
exp is a logical expression involving components of it.
NOTES:
DELETE itwithout any additions can only be usedinside LOOP AT it. deletes the current row.
-
7/30/2019 09_ABAP Advanced Internal Tables
30/46
DELETE Statement - examples
DELETE it INDEX 5.
DELETE it FROM 6 TO 8.
DELETE it WHERE f1 BETWEEN 'B' AND 'D'.
LOOP AT it WHERE f1 BETWEEN 'E' AND 'J'.
DELETE it.
ENDLOOP.
READ TABLE it WITH KEY f1 = 'K' BINARY SEARCH.
IF sy-subrc = 0.
DELETE it INDEX sy-tabix.
ENDIF.
-
7/30/2019 09_ABAP Advanced Internal Tables
31/46
Contents
Filling an Internal Table Using
COLLECT
-
7/30/2019 09_ABAP Advanced Internal Tables
32/46
COLLECT Statement
collect totals within an itab while filling it
Syntax:
COLLECT [wa INTO] it.
where:
itis an internal table.
wa is a work area that has the same structure as it.
NOTES: add up the numeric fields (types I, P, and F) with the
same default key fields (type C, N, D, T, and X)
-
7/30/2019 09_ABAP Advanced Internal Tables
33/46
COLLECT Statement - example
DATA: BEGIN OF it OCCURS 10,
date like sy-datum, " part of default keytot_sales TYPE p DECIMALS 2, "not part of default key
name(10), " part of default key
num_sales TYPE i VALUE 1, "not part of default key
END OF it.it-date = '20050901'.
it-tot_sales = 100.
it-name = 'Jack'.
COLLECT it.
it-date = '20050901'.
it-tot_sales = 200.
it-name = 'Jim'.
COLLECT it.
it-date = '20050901'.
it-tot_sales = 300.
it-name = 'Jack'.
COLLECT it.
it-date = '20050901'.
it-tot_sales = 400.
it-name = 'Jack'.
COLLECT it.
it-date = '20050901'.
it-tot_sales = 500.
it-name = 'Jim'.
COLLECT it.
it-date = '20050901'.
it-tot_sales = 600.
it-name = 'Jane'.
COLLECT it.
it-date = '20050902'.
it-tot_sales = 700.
it-name = 'Jack'.
COLLECT it.
-
7/30/2019 09_ABAP Advanced Internal Tables
34/46
Contents
Filling an Internal Table from a
Database Table
-
7/30/2019 09_ABAP Advanced Internal Tables
35/46
Filling an Internal Table from a Database Table
Selecting multiple rows directly into the body
of an internal table
multiple rows at 1 time
Selecting single rows into a work area and
then appending
Adding Rows one by one Using SELECT
-
7/30/2019 09_ABAP Advanced Internal Tables
36/46
Selecting Multiple Rows Directly
use the INTO TABLE addition of the SELECT statement selected rows are placed in a single operation (array operation)
array operation: statement performs an operation on multiple rows of an itab
more efficient than single row operations
no work areas are used or needed
Syntax: (a) SELECT *
(b) SELECTf1 f2 . . .
FROM dbtab INTO [CORRESPONDING FIELDS OF] TABLE it.
where: dbtab is the name of a database table.
f1 andf2 are fields within dbtab.
itis the name of an itab.
NOTES: ENDSELECT is not used with INTO TABLE
-
7/30/2019 09_ABAP Advanced Internal Tables
37/46
Example
REPORT y_vu_1301.
TABLES lfa1.
DATA it LIKE lfa1 OCCURS 23 WITH HEADER LINE.
SELECT * FROM lfa1 INTO TABLE it.
LOOP AT it.
WRITE: / it-lifnr, it-name1.
ENDLOOP.
-
7/30/2019 09_ABAP Advanced Internal Tables
38/46
Some issues
SELECT INTO TABLE and Sorting
SELECT INTO TABLE, then SORT itab faster
SELECT ORDER BY
Selected Fields Must Fit into the Internal Table A row is retrieved from dbtab.
A row is allocated in it.
The fields from dbtab is moved byte-by-byte into the fields in it.
The data types and lengths of each sending field in dbtabshould match the receiving field in it. Any remaining fields in itare filled with initial values (blanks or zeros).
-
7/30/2019 09_ABAP Advanced Internal Tables
39/46
Examples
REPORT y_vu_1302.
TABLES lfa1.
DATA BEGIN OF it OCCURS 2.
INCLUDE STRUCTURE lfa1.
DATA END OF it.
SELECT * FROM lfa1 INTO TABLE it
WHERE lifnr < '0000000010'.LOOP AT it.
WRITE: / it-mandt, it-lifnr, it-land1, it-name1.
ENDLOOP.
SKIP.
SELECT lifnr land1 name1 FROM lfa1 INTO TABLE it
WHERE lifnr < '0000000010'.
LOOP AT it.
WRITE: / it-mandt, it-lifnr, it-land1, it-name1.
ENDLOOP.
-
7/30/2019 09_ABAP Advanced Internal Tables
40/46
Using the CORRESPONDING FIELDS Addition
moves fields with the same name
the same effect as the MOVE-CORRESPONDING
NOTES: The order of the sending and receiving fields does not
matter
Sending fields that do not have a corresponding
receiving field are discarded Receiving fields that do not have a corresponding
sending field are unchanged
-
7/30/2019 09_ABAP Advanced Internal Tables
41/46
ExampleREPORT y_vu_1304.
TABLES lfa1.
DATA: BEGIN OF it1 OCCURS 23,
lifnr LIKE lfa1-lifnr,
lifnr_ext LIKE lfa1-lifnr,
land1 LIKE lfa1-land1,
END OF it1.
SELECT lifnr land1 FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE it1
WHERE lifnr BETWEEN 'V10' AND 'V12'.
-
7/30/2019 09_ABAP Advanced Internal Tables
42/46
Adding Rows one by one Using SELECT
Requires: the use of a work area omitting the TABLE addition
a second statement such as APPEND, INSERT, or COLLECT
TABLES lfa1.
DATA it LIKE lfa1 OCCURS 2 WITH HEADER LINE.
SELECT * FROM lfa1 INTO it "notice 'table' is omitted so the
WHERE land1 = 'DE'. "row goes into the header line of it
APPEND it.
ENDSELECT.
SELECT * FROM lfa1 "no 'into' so the row goes into theWHERE land1 = 'DE'. "default table work area
APPEND lfa1 TO it. "and then is appended to it
ENDSELECT.
Summing Up: Various Forms of
-
7/30/2019 09_ABAP Advanced Internal Tables
43/46
Summing Up: Various Forms ofSELECT
Statement(s) Writes To
SELECT INTO TABLE it Body
SELECT INTO CORRESPONDINGFIELDS OF TABLE it
Body
SELECT INTO it Header line
SELECT INTO CORRESPONDINGFIELDS OF it
Header line
-
7/30/2019 09_ABAP Advanced Internal Tables
44/46
Contents
Summary
-
7/30/2019 09_ABAP Advanced Internal Tables
45/46
Summary
The table body operator IF it[] IS INITIAL: test for existence of data IF it1[] = it2[]: to compare two itabs for equality it2[] = it1[]: to duplicate an itab
DESCRIBE TABLE it[LINES i] [OCCURSj]. get attributes of an itab
APPEND LINES OF or INSERT LINES OF. To copy a portion of an itab from one to another
INSERT
inserts a row at any position MODIFY
modifies the contents of one or more rows
-
7/30/2019 09_ABAP Advanced Internal Tables
46/46
Summary
DELETE removes one or more rows
CLEAR CLEAR it: clears the header line ofit CLEAR it[]: deletes all rows from itand leaves the memory
allocated REFRESH
deletes all rows and leaves the memory allocated (~ CLEARit[])
FREE deletes all rows and frees the memory
COLLECT to accumulate counts and totals