- SAP ABAP - Web Dynpro
- SAP ABAP - Business Add-Ins
- SAP ABAP - User Exits
- SAP ABAP - Customer Exits
- SAP ABAP - SAPscripts
- SAP ABAP - Smart Forms
- SAP ABAP - Dialog Programming
- SAP ABAP - Report Programming
- SAP ABAP - Object Events
- SAP ABAP - Interfaces
- SAP ABAP - Encapsulation
- SAP ABAP - Polymorphism
- SAP ABAP - Inheritance
- SAP ABAP - Classes
- SAP ABAP - Objects
- SAP ABAP - Object Orientation
- SAP ABAP - Deleting Internal Tables
- SAP ABAP - Reading Internal Tables
- SAP ABAP - Copying Internal Tables
- ABAP - Populating Internal Tables
- SAP ABAP - Creating Internal Tables
- SAP ABAP - Internal Tables
- SAP ABAP - Native SQL Overview
- SAP ABAP - Open SQL Overview
- SAP ABAP - Include Programs
- SAP ABAP - Function Modules
- SAP ABAP - Macros
- SAP ABAP - Subroutines
- SAP ABAP - Modularization
- SAP ABAP - Lock Objects
- SAP ABAP - Search Help
- SAP ABAP - Views
- SAP ABAP - Structures
- SAP ABAP - Tables
- SAP ABAP - Data Elements
- SAP ABAP - Domains
- SAP ABAP - Dictionary
- SAP ABAP - Exception Handling
- SAP ABAP - Formatting Data
- SAP ABAP - Date & Time
- SAP ABAP - Strings
- SAP ABAP - Decisions
- SAP ABAP - Loop Control
- SAP ABAP - Operators
- SAP ABAP - Constants & Literals
- SAP ABAP - Variables
- SAP ABAP - Data Types
- SAP ABAP - Basic Syntax
- SAP ABAP - Screen Navigation
- SAP ABAP - Environment
- SAP ABAP - Overview
- SAP ABAP - Home
SAP ABAP Useful Resources
- SAP ABAP - Discussion
- SAP ABAP - Useful Resources
- SAP ABAP - Quick Guide
- SAP ABAP - Questions Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SAP ABAP - Reading Internal Tables
We can read the pnes of a table by using the following syntax of the READ TABLE statement −
READ TABLE <internal_table> FROM <work_area_itab>.
In this syntax, the <work_area_itab> expression represents a work area that is compatible with the pne type of the <internal_table> table. We can specify a search key, but not a table key, within the READ statement by using the WITH KEY clause, as shown in the following syntax −
READ TABLE <internal_table> WITH KEY = <internal_tab_field>.
Here the entire pne of the internal table is used as a search key. The content of the entire pne of the table is compared with the content of the <internal_tab_field> field. If the values of the <internal_tab_field> field are not compatible with the pne type of the table, these values are converted according to the pne type of the table. The search key allows you to find entries in internal tables that do not have a structured pne type, that is, where the pne is a single field or an internal table type.
The following syntax of the READ statement is used to specify a work area or field symbol by using the COMPARING clause −
READ TABLE <internal_table> <key> INTO <work_area_itab> [COMPARING <F1> <F2>...<Fn>].
When the COMPARING clause is used, the specified table fields <F1>, <F2>....<Fn> of the structured pne type are compared with the corresponding fields of the work area before being transported. If the ALL FIELDS clause is specified, the SAP system compares all the components. When the SAP system finds an entry on the basis of a key, the value of the SY-SUBRC variable is set to 0. In addition, the value of the SY-SUBRC variable is set to 2 or 4 if the content of the compared fields is not the same or if the SAP system cannot find an entry. However, the SAP system copies the entry into the target work area whenever it finds an entry, regardless of the result of the comparison.
Example
REPORT ZREAD_DEMO. */Creating an internal table DATA: BEGIN OF Record1, ColP TYPE I, ColQ TYPE I, END OF Record1. DATA mytable LIKE HASHED TABLE OF Record1 WITH UNIQUE KEY ColP. DO 6 Times. Record1-ColP = SY-INDEX. Record1-ColQ = SY-INDEX + 5. INSERT Record1 INTO TABLE mytable. ENDDO. Record1-ColP = 4. Record1-ColQ = 12. READ TABLE mytable FROM Record1 INTO Record1 COMPARING ColQ. WRITE: SY-SUBRC = , SY-SUBRC. SKIP. WRITE: / Record1-ColP, Record1-ColQ.
The above code produces the following output −
SY-SUBRC = 2 4 9
In the above example, mytable is an internal table of the hashed table type, with Record1 as the work area and ColP as the unique key. Initially, mytable is populated with six pnes, where the ColP field contains the values of the SY-INDEX variable and the ColQ field contains (SY-INDEX + 5) values.
The Record1 work area is populated with 4 and 12 as values for the ColP and ColQ fields respectively. The READ statement reads the pne of the table after comparing the value of the ColP key field with the value in the Record1 work area by using the COMPARING clause, and then copies the content of the read pne in the work area. The value of the SY-SUBRC variable is displayed as 2 because when the value in the ColP field is 4, the value in the ColQ is not 12, but 9.
Advertisements