0% found this document useful (0 votes)
51 views41 pages

ABAP - T05-001 - ABAP Internal Table

Uploaded by

nhinhlse173042
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views41 pages

ABAP - T05-001 - ABAP Internal Table

Uploaded by

nhinhlse173042
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

ABAP Course

ABAP Internal Table


Topic Objectives
» Part 1 - Explain Work Area (Structure)
» Part 2 - Describe Basic Internal Table with/without HEADER LINE
» Part 3 - Define Internal Table Types: Standard
» Part 4 - Define Internal Table Types: Sorted
» Part 5 - Define Internal Table Types: Hashed
» Part 6 - Some techniques for Internal Tables: SORT, COLLECT, etc.
» Part 7 - Some techniques for LOOP: AT END, AT NEW, etc.
» *Coming soon – Advanced Internal Table: using Secondary Keys,
New Features since ABAP 7.40
Pre-requisite
» Understand ABAP basic syntax and data types
» Understand ABAP basic Open SQL
» Understand the basic of Data Dictionary
Course Rules
» Watch the video and WRITE THE CODE.
» You will not remember anything if you only watch the video
» If you have a question please note it down, after watching the
last video of topics, check the questions list again, then send it to
the instructor.
» There usually be a meeting to share your questions about evert
topic at the end of day.
» Complete exercises given by the instructor.
ABAP Internal Table
Part 1 – Structure and Work Area
Declaring Structure and Work Area
TYPE: BEGIN OF <name>,
<field1>, ..., <fieldn>,
END OF <name>.

TYPES: BEGIN OF TY_SPFLI,


CARRID TYPE SPFLI-CARRID,
CONNID TYPE SPFLI-CONNID,
CITYFROM TYPE SPFLI-CITYFROM, Declaration of a
CITYTO TYPE SPFLI-CITYTO, structure in your
COUNTRYFR TYPE SPFLI-COUNTRYFR, program.
COUNTRYTO TYPE SPFLI-COUNTRYTO,
NOTE TYPE TEXT40,
END OF TY_SPFLI.

Allocate Work Area


DATA: GS_SPFLI TYPE TY_SPFLI.
Using Work Area
TYPES: BEGIN OF TY_EMPLOYEE,
PERSNBR(6) TYPE N,
LASTNAME(20) TYPE C,
END OF TY_EMPLOYEE.

DATA: WA_EMPL1 TYPE TY_EMPLOYEE,


WA_EMPL2 LIKE WA_EMPL1.

MOVE '546259' TO WA_EMPL1-PERSNBR.


MOVE 'Salm' TO WA_EMPL1-LASTNAME.

Report Output
MOVE WA_EMPL1 TO WA_EMPL2.

WRITE: / WA_EMPL2-LASTNAME,
Salm
/ WA_EMPL2-PERSNBR. 546259
SKIP.

WRITE: / WA_EMPL1. 546249Salm


Field by Field Transport
MOVE-CORRESPONDING <f1> TO <f2>.
*- Move data between 2 Work areas
DATA: GS_SPFLI TYPE TY_SPFLI.
DATA: GS_SPFLI_TBL TYPE SPFLI.

SELECT * FROM SPFLI INTO GS_SPFLI_TBL UP TO 1 ROWS.


ENDSELECT.

*GS_SPFLI-CARRID = GS_SPFLI_TBL-CARRID.
MOVE-CORRESPONDING GS_SPFLI_TBL TO GS_SPFLI.

WRITE: / GS_SPFLI.

GS_SPFLI CARRID CONNID COUNTRYFR


_TBL AA 0123 US

CARRID CONNID NOTE COUNTRYFR


GS_SPFLI
AA 0123 US
ABAP Internal Table
Part 2 – Basic Internal Table and Header Line
Internal Tables
Internal tables
GT_ADDRESS_LIST

Name First Name City Street


Name1 First name 1 City 1 Street 1
Name2 First name 2 City 2 Street 2
Name3 First name 3 City 3 Street 3
Name4 First name 4 City 4 Street 4
Name5 First name 5 City 5 Street 5
Internal Tables with Header Line (obsolete)
Internal tables with Header Line

Line Type Name First Name City Street


ADDRESS_LIST Header Line
Header Line can Line 1 Name1 First name 1 City 1 Street 1
be used as a
Line 2 Name2 First name 2 City 2 Street 2
Work Area for
LOOP, READ, Line 3 Name3 First name 3 City 3 Street 3
APPEND. But it is
obsoleted and Line 4 Name4 First name 4 City 4 Street 4
not supported in Line 5 Name5 First name 5 City 5 Street 5
OOP Class.
Internal Tables with Header Line (obsolete)

• HEADER LINE is a special work area that have the same name as
the internal table.
• It is created when we declare WITH HEADER LINE.
• It can be used to access internal table data without the need to create a
separate Work Area.
• HEADER LINE not working with ABAP OOP.
• HEADER LINE is obsolete.
• DO NOT use HEADER LINE in your new program.
Internal Table Attributes
PROGRAM ZEXAMPLE.
TYPE: BEGIN OF TY_EMPLOYEE,
PERSNBR(6) TYPE N, Line Type
LASTNAME(20) TYPE C,
END OF TY_EMPLOYEE.
Table
DATA: ITAB_EMPLTAB TYPE STANDARD TABLE Type
OF TY_EMPLOYEE WITH KEY UNIQUE PERSNBR
INITIAL SIZE 10,
WA_EMPLTAB TYPE T_TAB.

Key

* All of the above syntax will be explained in detail later in this section!
Declaring an Internal Table

TYPES: BEGIN OF TY_EMPLOYEE,


PERSNBR(6) TYPE N,
LASTNAME(20) TYPE C,
END OF TY_EMPLOYEE.

DATA: ITAB_EMPLTAB TYPE STANDARD TABLE OF


TY_EMPLOYEE WITH KEY UNIQUE PERSNBR
INITIAL SIZE 10,
WA_EMPLTAB TYPE T_EMPLOYEE.
Accessing Individual Table Entries - Work Areas
TYPES: BEGIN OF TY_EMPLOYEE,
PERSNBR(6) TYPE N,
LASTNAME(20) TYPE C,
END OF TY_EMPLOYEE.

DATA: ITAB_EMPLTAB TYPE STANDARD TABLE


OF TY_EMPLOYEEWITH KEY UNIQUE PERSNBR
INITIAL SIZE 10,
WA_EMPLTAB TYPE TY_EMPLOYEE.

I_EMPLTAB WA_EMPLTAB
546259 SALM 578110 MIDDLEMARK
163927 COSTIGAN
578110 MIDDLEMARK In order to manipulate a row in an
803124 SIEBER internal table, that row must first be put in
804320 SIFFERMAN the work area (commands that put the
. . contents of a table row into the work area
. . will be discussed later on!)
Filling an Internal Table

APPEND <wa> TO <itab>. APPEND <wa> TO <itab>


SORTED BY <field>.
Country Sales

D 400 000 Header line


USA 1 000 000 1 Country Sales
GB 500 000 2 D 400 000 Header line
D 7 800 000 3 D 7 800 000 1
GB 50 000 4 USA 1 000 000 2
A 140 000 5 GB 500 000 3
6 A 140 000 4
7 GB 50 000 5
8
Reading a Single Table Entry
READ TABLE <itab> [ INTO <wa> ] ... .
REPORT <name>.
TABLES: TABNA.
TYPES: BEGIN OF T_TAB,
COUNTRY LIKE TABNA-COUNTRY,
ID LIKE TABNA-ID,
NAME1 LIKE TABNA-NAME1
SALES LIKE TABNA-SALES,
END OF T_TAB.
DATA: I_TAB TYPE STANDARD TABLE OF T_TAB
WITH KEY NON-UNIQUE COUNTRY,
WA_TAB TYPE T_MAT,
TAB_INDEX TYPE I. “or TAB_INDEX LIKE SY-TABIX.
CLEAR TAB.
MOVE: 'D' TO I_TAB-COUNTRY,
'WOLF' TO I_TAB-NAME1.
READ TABLE I_TAB.
IF SY-SUBRC NE 0.
READ TABLE I_TAB WITH KEY ‘K. Salm’.
READ TABLE I_TAB WITH KEY ’CAN' BINARY SEARCH.

TAB-INDEX = SY-TABIX + 1.
READ TABLE I_TAB INDEX TAB_INDEX.
ABAP Internal Table
Part 3 - Internal Table Types: Standard
Outline
» 3 types of Internal Table
1. Creating every types of Internal Table
2. Adding data records to Internal Table using APPEND
3. Adding data records to Internal Table using INSERT
4. Single data accessing: READ TABLE
5. Data accessing: LOOP
6. Single data accessing: LOOP … EXIT …
7. Modifying data records using MODIFY
8. Modifying data records using FIELD-SYMBOL
9. Delete data from Internal Table
10. Clear all data of an Internal Table
Terms
» KEY field: a field to help identify one line of data in table.
» PRIMARY KEY: every table must has at least 1 primary key. A
Primary Key can include one or many KEY fields
» SECONDARY KEY: help increase search performance.
» UNIQUE: not allow duplication of data (E.g. Không thể tồn tại 2
số CCCD trùng nhau)
» NON-UNIQUE: allow duplication of data.
» HASH: https://en.wikipedia.org/wiki/Hash_function
» BINARY SEARCH / LINEAR SEARCH
Internal Table Types

Prior to SAP 4.0, the standard table was the only table type.
STANDARD TABLE
ABAP Internal Table
Part 4 - Internal Table Types: Sorted
Internal Table Types

Prior to SAP 4.0, the standard table was the only table type.
SORTED TABLE
ABAP Internal Table
Part 5 - Internal Table Types: Hashed
Internal Table Types

Prior to SAP 4.0, the standard table was the only table type.
HASHED TABLE
Internal Table Types and Keys
Changing an Internal Table
INSERT <wa> INTO <itab> INDEX <i>.
MODIFY <itab> FROM <wa> INDEX <i>.
DELETE <wa> FROM <itab> INDEX <i>.
REPORT ZEXAMPLE.
TABLES: TABNA.
TYPES: BEGIN OF T_REC,
COUNTRY LIKE TABNA-COUNTRY,
ID LIKE TABNA-ID,
NAME1 LIKE TABNA-NAME1,
END OF T_REC.
DATA: I_TAB TYPE STANDARD TABLE OF T_REC,
WA_TAB TYPE T_REC.
.
.
READ TABLE I_TAB WITH KEY ‘USA’ BINARY SEARCH INTO WA_TAB.
MOVE ’US of A' TO I_TAB-NAME1.
MODIFY I_TAB FROM WA_TAB INDEX SY-TABIX.

READ TABLE I_TAB WITH KEY ‘CAN’ BINARY SEARCH INTO WA_TAB.
MOVE ‘MEX’ TO WA_TAB-COUNTRY.
INSERT WA_TAB INTO I_TAB.

READ TABLE I_TAB WITH KEY ‘IRE’ BINARY SEARCH INTO WA_TAB.
DELETE I_TAB INDEX SY-TABIX.
Deleting an Internal Table
CLEAR <itab>.

For a table defined with a header line, this


statement initializes the header line.
For a table defined without a header line,
this statement works the same as REFRESH.

REFRESH <itab>.

Deletes all table lines except header line if


header line is used. Storage space is not released
Paging is released

FREE <itab>.

Deletes all table lines except header


line if a header line is used.
Storage space is released
System Field SY-TABIX

REPORT <name>.
.
.
.
LOOP AT TAB.
WRITE: / SY-TABIX, TAB-COUNTRY, ... .
ENDLOOP.

REPORT <name>.
.
.
.
PARAMETERS: START LIKE SY-TABIX DEFAULT '10',
END LIKE SY-TABIX DEFAULT '20'.
LOOP AT TAB FROM START TO END.
WRITE: / SY-TABIX, TAB-COUNTRY, ... .
ENDLOOP.
ABAP Internal Table
Part 6 - Some techniques for Internal Tables:
SORT, COLLECT, etc.
Outline Part 6
» Compressing Data using COLLECT
» Sorting an Internal Table using SORT
» Delete duplicated data in an Internal Table
» Copy an Internal Table to another Internal Table
» Get information of an Internal Table: Number of line, etc.
Compressing Data with an Internal Table
COLLECT <itab>. Country Sales (Type P)

D 400 000 Header line


USA 1 000 000 1
COLLECT <work_area> TO <itab>.
GB 550 000 2
D 7 800 000 3 8 200 000
REPORT <name>. 4
5
TABLES: ADRC. 6
TYPES: BEGIN OF TY_MAT, 7
COUNTRY LIKE ADRC-COUNTRY, 8
SALES LIKE ADRC-SALES, 9
END OF TY_MAT.

DATA: ITAB_ADRC TYPE STANDARD TABLE OF


TY_MAT WITH KEY NON-UNIQUE COUNTRY
INITIAL SIZE 5
WA_TAB TYPE TY_MAT.
SELECT * FROM ADRC.
MOVE-CORRESPONDING ADRC TO ITAB_ADRC.
COLLECT ITAB_ADRC.
ENDSELECT.
Sorting an Internal Table
SORT <itab> BY <field1> <field2> . . . .

REPORT <name>.
TABLES: ADRC.
TYPES: BEGIN OF TY_MAT,
COUNTRY LIKE ADRC-COUNTRY,
ID LIKE ADRC-ID,
NAME1 LIKE ADRC-NAME1
SALES LIKE ADRC-SALES,
END OF TY_MAT.
DATA: ITAB_ADRC TYPE STANDARD TABLE OF TY_MAT
WITH KEY NON-UNIQUE COUNTRY
INITIAL SIZE 5,
WA_TAB TYPE TY_MAT.
SORT ITAB_ADRC.

SORT ITAB_ADRC BY COUNTRY NAME1.

SORT ITAB_ADRC BY COUNTRY ASCENDING


NAME1 DESCENDING.
Information About an Internal Table
DESCRIBE TABLE <itab>... .

REPORT <name>.
TABLES: TABNA.

TYPES: BEGIN OF T_MAT,


COUNTRY LIKE TABNA-COUNTRY,
ID LIKE TABNA-ID,
NAME1 LIKE TABNA-NAME1
SALES LIKE TABNA-SALES,
END OF T_MAT.
DATA: TAB TYPE STANDARD TABLE OF T_MAT
WITH KEY NON-UNIQUE COUNTRY
INITIAL SIZE 5
WA_TAB TYPE T_MAT,
LINE_COUNT TYPE I,
OCCURS_COUNT TYPE I.
DESCRIBE TABLE TAB
LINES LINE_COUNT
OCCURS OCCURS_COUNT.
ABAP Internal Table
Part 7 - Some techniques for Loop:
AT NEW, AT END, AT FIRST, AT LAST
AT Statement
TYPES: BEGIN OF T_COMPANIES,
AT NEW <field>. NAME(30)TYPE C,
PRODUCT(20)TYPE C,
SALES TYPE I,
AT END OF <field>. END OF T_COMPANIES.
DATA: I_COMPANIES TYPE STANDARD TABLE
OF T_COMPANIES
AT FIRST. INITIAL SIZE 10,
WA_COMPANIES TYPE T_COMPANIES.

AT LAST. LOOP AT I_COMPANIES INTO WA_COMPANIES.
AT NEW NAME.
NEW PAGE.
WRITE: / WA_COMPANIES-NAME.
ENDAT.
EXAMPLE WRITE: / WA_COMPANIES-PRODUCT,
WA_COMPANIES-SALES.
AT END OF NAME.
SUM.
WRITE: / WA_COMPANIES-PRODUCT,
WA_COMPANIES-SALES.
ENDAT.
ENDLOOP.
Changing an Internal Table in a LOOP

REPORT <name>.
.
.
.
LOOP AT <itab> INTO <wa>.
.
.
.
MODIFY <itab> FROM <wa>.
.
.
.
INSERT <wa> INTO <itab>.
.
.
.
DELETE <itab>.
.
.
.
ENDLOOP.
Thank you
Q&A

You might also like