You are on page 1of 8

HOME

ABAP TUTORIAL

ASK A QUESTION

SUBMIT A TIP

SAPHub
SAP for Beginners

AdChoices

ABAP Sap

Statement

Index Table

Reading

Reading Values from ABAP Internal Table


Posted in ABAP Tutorial on 08/12/2011

Enter your Search Term here

Search

NEWSLETTER
Enter your email address:

Subscribe
Delivered by FeedBurner

Data can be read from an internal table using the following 2 statements.

READ TABLE Used to read single row


LOOP AT / ENDLOOP Used to read multiple rows

The syntax for READ TABLE is as follows.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

READ TABLE [INTO Workarea] [INDEX |WITH KEY]

In READ TABLE statement we need to specify either INDEX or KEY but not both at the same time. If we want to read
the third row from the internal table, then specify the index as 3 in the INDEX clause of the READ statement. Index is a
relative row number of the internal table.

SAPHub
2,319 likes

If the record is found then the found record is copied to work area and SY-SUBRC is set to 0. Otherwise SY-SUBRC is
set to 4 and work area remains unchanged.

Like Page

*--------------------------------------------------------------*

Share

*Data Types
*--------------------------------------------------------------*

Be the first of your friends to like this

TYPES: BEGIN OF ty_student,


id(5)

TYPE n,

name(10) TYPE c,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.

CATEGORIES

DATA: it TYPE TABLE OF ty_student.


ABAP Dictionary
gwa_student-id

= 1.

gwa_student-name

= 'JOHN'.

ABAP General

APPEND gwa_student TO it.

ABAP Tutorial

gwa_student-id

= 2.

ABAP Workbench

gwa_student-name

= 'JIM'.

APPEND gwa_student TO it.

ALE IDoc
ALV

gwa_student-id

= 3.

gwa_student-name

= 'JACK'.

Debugging

APPEND gwa_student TO it.

Enhancements

READ TABLE it INTO gwa_student INDEX 3.

FI

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

IF sy-subrc = 0.
WRITE: gwa_student-id, gwa_student-name.

Files
LSMW

ELSE.
WRITE 'No Record Found'.
ENDIF.

MM
Modularization

Output

Performance
Reports

We can also read a row from the internal table based on the value in a particular field using KEY clause of the READ
statement. If more than one row found in the internal table with the same value specified in the KEY clause then only

SAP Interview Questions and Answers


SD

the first row will be retrieved.


Let us read a record from internal table where name is JIM.

Selection Screen
Smartforms

READ TABLE it INTO gwa_student WITH KEY name = 'JIM'.


IF sy-subrc = 0.
WRITE: gwa_student-id, gwa_student-name.
ELSE.
WRITE 'No Record Found'.

RECENT POSTS
Track Changes Made in ABAP Debugger
Find Saplogon.ini File Location

ENDIF.

Track Changes Made to SAP Table Using &SAP_EDIT

Output

Activate &SAP_EDIT in SE16N

The syntax for LOOP statement is as follows.

LOOP AT [INTO Workarea] [FROM] [TO] [WHERE expression].


ENDLOOP.

Display checkbox in ABAP F4 help using ALV

TAGS
abap ABAP Dictionary

ALV

background-jobs BADI bdc

DDIC debugging disable display


The rows are read from the internal table one at a time and placed in the work area. The lines between LOOP and
ENDLOOP are executed for each record read from the internal table. Once all the rows in the internal table are
processed the control goes to the next line after the ENDLOOP statement.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

download enhancement f1 help f4 help file


FM header how-to icon internal tables
interview questions log logo lsmw messages
pdfcrowd.com

processed the control goes to the next line after the ENDLOOP statement.

MM oops pdf performance popup programmatically


*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_employee TYPE zemployee,
gt_employee

selection
screen smartforms table tcodes
reports sap-note scripting SD
text

TMG total upload variant XML

TYPE TABLE OF zemployee.

*Fill the internal table with database values


SELECT * FROM zemployee INTO TABLE gt_employee.
WRITE:/ 'Display all the records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee.
WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.
SKIP.
WRITE:/ 'Display only first 2 records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee TO 2.
WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.
SKIP.
WRITE:/ 'Dont Display first 2 records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee FROM 3.
WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.
SKIP.
WRITE:/ 'Display only 2nd & 3rd records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee FROM 2 TO 3.
WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

SKIP.
WRITE:/ 'Display records based on condition' COLOR 5.
WRITE:/ 'Display records where place is LONDON' COLOR 5.
LOOP AT gt_employee INTO gwa_employee WHERE place = 'LONDON'.
WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.

Output

Tweet

Like

Share

Related posts:
1. What is an ABAP Internal Table and How to Create it?
2. Deleting Lines from an ABAP Internal Table
3. Inserting Lines into ABAP Internal Tables
4. Changing Lines in ABAP Internal Tables
5. More on ABAP Internal Tables

8 Comments
hesin.nas says:
Dec ember 8, 2011 at 1:37 pm

Thank you for your studies..


Reply

Kannan says:
Oc tober 25, 2012 at 9:58 am

Thanks u very muchits so simple to understand

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Reply

Bas He says:
Marc h 4, 2014 at 9:27 am

Really simple, thanks. Also, of course, theres the SAP documentation on it:
http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm
Reply

Ankit Tyagi says:


April 16, 2014 at 8:41 pm

very very help full ..umhhhhhh


Reply

Pranav Patil says:


August 26, 2014 at 5:27 pm

hey i have question..what can be changed while lopping at internal tablelike which value can be
changed? (e.g. , sum, lead to) pls need this. Thank you.
Reply

Ali Qazi says:


September 15, 2014 at 5:28 am

Best Tutorial, Recommended!


Reply

Rashmita Bhujabal says:


January 28, 2016 at 9:53 am

Its really understanding easily the concept.thank you so much


Reply

Rashmita Bhujabal says:


January 28, 2016 at 9:54 am

its really understanding easily.thank you.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Reply

0 Trackbacks

Leave a Reply
Your email address will not be published. Required fields are marked *
Comment

Name *

Email *

Website

Post Comment

What is an ABAP Internal Table and How to Create it?


SAP ABAP Interview Questions & Answers SAPsripts
Copyright 2010 SAPHub - All rights reserved

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

Powered by WordPress - Theme by TricksDaddy

pdfcrowd.com

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

You might also like