
- SAP ABAP - Home
- SAP ABAP - Overview
- SAP ABAP - Environment
- SAP ABAP - Screen Navigation
- SAP ABAP - Basic Syntax
- SAP ABAP - Data Types
- SAP ABAP - Variables
- SAP ABAP - Constants & Literals
- SAP ABAP - Operators
- SAP ABAP - Loop Control
- SAP ABAP - Decisions
- SAP ABAP - Strings
- SAP ABAP - Date & Time
- SAP ABAP - Formatting Data
- SAP ABAP - Exception Handling
- SAP ABAP - Dictionary
- SAP ABAP - Domains
- SAP ABAP - Data Elements
- SAP ABAP - Tables
- SAP ABAP - Structures
- SAP ABAP - Views
- SAP ABAP - Search Help
- SAP ABAP - Lock Objects
- SAP ABAP - Modularization
- SAP ABAP - Subroutines
- SAP ABAP - Macros
- SAP ABAP - Function Modules
- SAP ABAP - Include Programs
- SAP ABAP - Open SQL Overview
- SAP ABAP - Native SQL Overview
- SAP ABAP - Internal Tables
- SAP ABAP - Creating Internal Tables
- ABAP - Populating Internal Tables
- SAP ABAP - Copying Internal Tables
- SAP ABAP - Reading Internal Tables
- SAP ABAP - Deleting Internal Tables
- SAP ABAP - Object Orientation
- SAP ABAP - Objects
- SAP ABAP - Classes
- SAP ABAP - Inheritance
- SAP ABAP - Polymorphism
- SAP ABAP - Encapsulation
- SAP ABAP - Interfaces
- SAP ABAP - Object Events
- SAP ABAP - Report Programming
- SAP ABAP - Dialog Programming
- SAP ABAP - Smart Forms
- SAP ABAP - SAPscripts
- SAP ABAP - Customer Exits
- SAP ABAP - User Exits
- SAP ABAP - Business Add-Ins
- SAP ABAP - Web Dynpro
SAP ABAP - Reading Internal Tables
We can read the lines of a table by using the following syntax of the READ TABLE statement −
READ TABLEFROM .
In this syntax, the
READ TABLEWITH KEY = .
Here the entire line of the internal table is used as a search key. The content of the entire line of the table is compared with the content of the
The following syntax of the READ statement is used to specify a work area or field symbol by using the COMPARING clause −
READ TABLEINTO [COMPARING ... ].
When the COMPARING clause is used, the specified table fields
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 lines, 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 line 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 line 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.