Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Diff: Read table && Loop at

Former Member
0 Likes
5,081

Hi,

I would like to know the diff between "Read table itab" and "Loop at itab."

Kindly help me out.

Regards,

Kalai

11 REPLIES 11
Read only

former_member189059
Active Contributor
0 Likes
2,541

With loop at itab, every row of your internal table is read

with read table, only a single row that matches the condition is read

Read only

Former Member
0 Likes
2,541

HI ,

if your intension to to read JUST only one record from the internal table,

you can use

READ TABLE ITAB WITH KEY F1 = <VALUE> ...

BINARY SEARDCH.

this statement can ONLY return 1 record. so if you know,only 1 record will be there for the given condition,you can use this statement.

else.

you must use LOOP AT and ENDLOOP statements to process more than 1 record.

LOOP AT ITAB WHERE F1 = <VALUE>.

ENDLOOP.

Case 1:

=======

If table has lot of data first of all loop at using field symbols,

LOOP AT ITAB ASSIGNING <fs>.

ENDLOOP.

This would definitely improve the performance.

Case 1.1:

=========

If you have single loop and dependsing up on the previous value you want to select data. Then first sort that internal table based on primary key and read the table using READ statement with BINARY SEARCH.

SORT ITAB by f1.

READ TABLE ITAB INTO <WA> WITH KEY f1 = <Variable>

BINARY SEARCH.

If sy-subrc EQ 0.

ENDIF.

Case 3:

=======

Always ignore Loop inside loop. Instead go for READ if you have to play around 2 loops.

LOOP AT ITAB1 INTO <WA>.

READ TABLE ITAB2 INTO <WA2> WITH KEY

f1 = <WA>-f1.

if sy-subrc EQ 0.

ENDIF.

ENDLOOP.

LOOP: looop will read each and evry record in the inetrnal table.

Loop doesnt return Sy-subrc value.

READ : statement will read only single record at at time and not more than that.

Read statement will return Sy-subrc value.

if we want to compare 2 internal tables (itab1 & itab2) then we will loop one internal table and read another internal table.

ex: loop at itab1 into w_itab1.

read table itab2 into e_iatb2 with key f1 = w_itab1-f1.

If sy-subrc = 0.

....

....logic...

endif.

endloop.

Regards,

Jayant

Read only

varma_narayana
Active Contributor
0 Likes
2,541

Hii

Both these statements are used to Read the Records from internal Table.

Read Table : is used to read only a single record from itab.

Eg:

READ TABLE ITAB INTO WA index 10.

Or

READ TABLE ITAB INTO WA with Key Empno = 100.

if sy-subrc = 0. "To check the Status.

LOOP...EndLoop : It is used to Read multiple rows from Itab.

Eg:

Loop at itab into wa where deptno = 10.

write:/ wa-empno, wa-deptno.

endloop.

<b>Reward if Helpful</b>

Read only

Former Member
0 Likes
2,541

Hi

<b>READ -</b>

Syntax

READ TABLE itab { { }

| { WITH KEY dobj }

| { WITH KEY = dobj [BINARY SEARCH] } } result.

Alternatives:

1. ... { }

2. ... WITH KEY dobj

3. ... WITH KEY = dobj [BINARY SEARCH]

Effect:

This statement has the same function as the general READ statement. As well as the additions listed here for specifying the individual rows to be read, outside of classes, the search key can also be specified in three obsolete forms.

Alternative 1

... { }

Effect:

If the search kep is not specified explicitly, the internal table itab must be a standard table with a header line. The first found line in the internal table is read for which the values in the columns of the standard key match the values in the corresponding components of the header line. Key fields in the header line that only contain blank characters are handled as if they match all values. If all the key fields in the header line only contain blank characters, the first entry in the table is read.

Notes:

In Unicode programs, the standard key of the internal table must not contain any byte-type components when the implicit search key is used.

The statement READ TABLE itab ... is not the same as the statement READ TABLE itab FROM itab .... In the latter, the table key and not the search key of the header field is used for the search. Also, with this statement, key fields that contain blank characters do not match all fields of the internal table.

Example:

In the following READ statement (in contrast to the example for READ TABLE - table_key) in general no entry is found, as the whole standard key is compared. In particular, the components deptime and arrtime that belong to the standard key of the internal table are of type t and contain the value "000000" instead of blank characters as an initial value in the header line. Only table entries that contain exactly these values are read.

DATA: spfli_tab TYPE STANDARD TABLE OF spfli

WITH NON-UNIQUE KEY carrid connid

WITH HEADER LINE.

FIELD-SYMBOLS <spfli> TYPE spfli.

SELECT *

FROM spfli

INTO TABLE spfli_tab

WHERE carrid = 'LH'.

spfli_tab-carrid = 'LH'.

spfli_tab-connid = '0400'.

READ TABLE spfli_tab ASSIGNING <spfli>.

Alternative 2

... WITH KEY dobj

Effect:

If a single data object is specified directly after the addition WITH KEY, the internal table itab must be a standard table. The first line found in the internal table is read, whose left-aligned content matches the content of the data object dobj. For the data object dobj, only flat data types are allowed. In the search, the start of table rows that are longer than the data object dobj are handled as if they have the same data type dobj (casting).

Example:

To use the addition WITH KEY dobj for evaluating specific key fields, a structure must be created that corresponds to the relevant starting part of the line type. In contrast to the example for zu READ TABLE - table_key, in the following program section, the client column mandt of the table spfli_tab must be taken into account in the search key.

DATA: spfli_tab TYPE STANDARD TABLE OF spfli

WITH NON-UNIQUE KEY carrid connid.

DATA: BEGIN OF key_struc,

mandt TYPE spfli-mandt VALUE '000',

carrid TYPE spfli-carrid VALUE 'LH',

connid TYPE spfli-connid VALUE '0400',

END OF key_struc.

FIELD-SYMBOLS <spfli> TYPE spfli.

SELECT *

FROM spfli

INTO TABLE spfli_tab

WHERE carrid = 'LH'.

READ TABLE spfli_tab WITH KEY key_struc ASSIGNING <spfli>.

Alternative 3

... WITH KEY = dobj [BINARY SEARCH]

Effect:,If the addition WITH KEY is followed by a single data object after an "equals" sign, the first line found in the internal table itab is read, whose whole content corresponds to the content of the data object dobj. It must be possible to convert the data object dobj to the line type of the internal table. If the data type of dobj does not correspond to the line type of the internal table, a conversion is performed for the comparison according to the conversion rules.

Note:

This statement has the same function as specifying the pseudo component table_line as a free key, and is replaced by this.

READ TABLE itab WITH KEY table_line = dobj

[BINARY SEARCH] ...

Example:

Obsolete determination of whether a line in an internal table exists with an elementary line type. The comment lines show the generally valid syntax with the pseudo-component table_line.

DATA itab TYPE TABLE OF i.

DO 10 TIMES.

APPEND sy-index TO itab.

ENDDO.

READ TABLE itab WITH KEY = 4

TRANSPORTING NO FIELDS.

  • READ TABLE itab WITH KEY table_line = 4

  • TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

...

ENDIF.

<b>LOOP AT itab </b>

Syntax

LOOP AT itab result [cond].

...

ENDLOOP.

Effect

The LOOP and ENDLOOP statements define a loop around a statement block. The LOOP statement reads lines from internal table itab sequentially. You can either read all the lines or specify conditions cond to restrict which lines to read. The output result determines when and where the line contents are read. The statement block between LOOP and ENDLOOP is executed once for each line. To exit processing of the statement block , you can use the statements described in Section leave loops.

The sequence in which the lines are read depends on the table type:

Standard tables and sorted tables

The lines are read by ascending table index.

Hashed Tables

The lines are processed in the sequence in which they were inserted in the table, and in the sort sequence following use of the SORT statement.

The loop continues to run until all the table lines that meet conditition cond have been read or until it is exited with a statement. If no appropriate lines are found or if the internal table is blank, the loop is not run at all.

System Fields

The LOOP ATstatement sets system field sy-tabix to the table index of the current table line for Standard tables and sorted Tables, and to 0 for Hashed-Tables, after every loop pass. It leaves sy-subrc unchanged. When the loop is exited with ENDLOOP, sy-tabix is reset to the value it had before the loop was entered, and the following applies sy-subrc: sy-subrc Relevance

0 The loop was run at least once.

4 The loop was not run at all.

The system fields sy-tfill and sy-tleng are also supplied with data.

Changing Internal tables in a loop

If you insert or delete lines in the statement block of a LOOP , this will have the following effects:

If you insert lines behind the current line, these new lines will be processed in the subsequent loop passes. An endless loop can result.

If you delete lines behind the current line, the deleted lines will no longer be processed in the subsequent loop passes.

If you insert lines in front of the current line, the internal loop counter is increased by one with each inserted line. This affects sy-tabix in the subsequent loop pass.

If you delete lines in front of the current line, the internal loop counter is decreased by one with each deleted line. This affects sy-tabix in the subsequent loop pass.

Notes

Within classes, you cannot change table itab in the statement block of the LOOP using statements that access the entire table. Statements such as CLEAR, FREE, LOCAL, REFRESH, SORT and all types of assignments to itab are not allowed.

If you specify the internal table itab through a Reference variable, then the loop is processed completely at the table referenced at entry. Possible changes of the reference variable do not have an effect on the loop. The respective object cannot be deleted from the Garbage Collector, as long as the loop is not completed.

Example

Nested LOOP-loops. The contents of the current row for the outer loop are analyzed in the WHERE-condition for the inner loop.

PARAMETERS p_name TYPE scarr-carrname DEFAULT '*'.

DATA: scarr_tab TYPE SORTED TABLE OF scarr

WITH UNIQUE KEY carrname,

spfli_tab TYPE SORTED TABLE OF spfli

WITH NON-UNIQUE KEY carrid.

FIELD-SYMBOLS LIKE LINE OF scarr_tab.

DATA spfli_line LIKE LINE OF spfli_tab.

SELECT *

FROM scarr

INTO TABLE scarr_tab.

SELECT *

FROM spfli

INTO TABLE spfli_tab.

LOOP AT scarr_tab ASSIGNING

WHERE carrname CP p_name.

LOOP AT spfli_tab INTO spfli_line

WHERE carrid = -carrid.

WRITE: / spfli_line-carrid,

spfli_line-connid.

ENDLOOP.

ENDLOOP.

Example

The following loop deletes all lines of an internal table since - through the short form of the DELETE statement - the current first line is always deleted.

DATA itab TYPE TABLE OF i.

DATA wa LIKE LINE OF itab.

LOOP AT itab INTO wa TO 6.

DELETE itab.

ENDLOOP.

Regards

Ravish

<i>

Reward if useful</i>

Read only

0 Likes
2,541

Why bother just cutting and pasting the help documentation?!

There's this amazing thing in the SAP ABAP editor called online documentation. If you place your cursor on an ABAP command and press the F1 key you will see SAP's help documentation for that command. Or you can just press F1 and search for specific commands.

Please try to use the F1 help before posting questions like this, or at least search the forum as they have been asked countless times before.

Gareth.

Read only

Former Member
0 Likes
2,541

Hi,

Read table will read only a single record at a time, but loop at will run for every record in the internal table

Regards

Sudheer

Read only

Former Member
0 Likes
2,541

hi,

if you have the main data in internal table itab and want to do some calculation for each record..you need to use LOOP AT.

or if you are processing some other internal table and just need to fetch the corresponding record from itab..use should use READ TABLE ...it also advisable to sort the table internal table when you use READ on it and use BINARY SEARCH

LOOP: looop will read each and evry record in the inetrnal table.

Loop doesnt return Sy-subrc value.

READ : statement will read only single record at at time and not more than that.

Read statement will return Sy-subrc value.

also check this link

reward points if it helps

rgds

Read only

Former Member
0 Likes
2,541

well read table gets you ONE record, while loop at itab loops over ALL records.

data lt_vbak type table of vbak,
       ls_vbak type vbak,
       lv_lines type i.

select * from vbak 
into table lt_vbak.

describe table lt_vbak lines lv_lines.

* this do enddo thing does the same than the loop at _lt_vbak.
do lv_lines times.
   read table vbak
   into ls_vbak
   index lv_tabix.
enddo.

loop at lt_vbak into ls_vbak.

endloop.

maybe this example helps you to understand the concept

Read only

Clemenss
Active Contributor
0 Likes
2,541

Hi Kalaivani,

still not clear?

F1 helps:

READ - Reading an Internal Table

Variants:

1. READ TABLE itab FROM wa [additions].

2. READ TABLE itab WITH TABLE KEY k1 = v1 ... kn = vn

[ additions].

3. READ TABLE itab WITH KEY k1 = v1 ... kn = vn

[BINARY SEARCH] [ additions].

4. READ TABLE itab INDEX i [additions].

Obsolete Variants

In an ABAP Objects context, a more severe syntaxcheck is performed that in other ABAP areas. See Short forms of line operations notallowed.

Effect

Reads an entry from an DS:ABEN.ITAB>internal table, using either its key or its index. The return code SY-SUBRCspecifies whether an entry could be read. If you specify a non-uniquekey (the table must have a NON-UNIQUE key for this to be valid), the system returns the entry with the lowest index from the set ofentries that meet the condition.

SY-SUBRC = 0:

An entry was read.

SY-TABIX is set to the index of the entry.

SY-SUBRC = 2:

An entry was read.

SY-TABIX is set to the index of the entry. This return code canonly occur when you use the COMPARING addition. For furtherdetauls, refer to the COMPARING section of theadditions

SY-SUBRC = 4:

No entry was read.

The value of SY-TABIX depends on the table type and whether theBINARY SEARCH addition was specified.

If the table is a SORTED TABLE or STANDARD TABLE with the BINARY SEARCH addition, SY-TABIX refers to thenext-highest index.

Otherwise, SY-TABIX is undefined.

SY-SUBRC = 8:

No entry was read.

This return code only occurs with a SORTED TABLE or a STANDARD TABLE with the BINARY SEARCH addition.SY-TABIX is set to the number of all entries plus 1.

The READ TABLE statement also fills thesystem fields SY-TFILL, SY-TLENG, and SY-TOCCU.

Variant 1

READ TABLE itab FROM wa [ additions].

Variant 2

READ TABLE itab WITH TABLE KEY k1 = v1 ... kn =vn [additions].

Effect

The system uses the specified table key values to locatethe correct line. If there is more than one entry with the same key,the system returns the first. The way in which the system looks fortable entries depends on the table type:

STANDARD TABLE:

The system searches from the start of the table. The response time isin linear relation to the number of table entries.

SORTED TABLE:

The response time is in logarithmic relation to the number of tableentries.

HASHED TABLE:

The response time is constant.

Notes

When you specify the table key using k1 = v1 ... kn =vn you must specify values for all of the key fields. If the type of a value is not compatible with the type of the corresponding keyfield,the system uses MOVE logic to convert itto the type of the key field before reading from the table.

If you do not know the name of a component until runtime, you can usethe expression WITH TABLE KEY ... (ni) = vi ... to specify itdynamically as the contents of the field ni. If ni isempty at runtime, the key specification is ignored.

If the line type of the internal table contains object referencevariables as components, or the entire line type is a referencevariable, you can use the attirbutes of the objects to which thereference is pointing in a particular line as key values (seeAttributes of Objects as the Key of an Internal Table).

If a table has a non-structured line type, you can use thepseudocomponent TABLE_LINE to address the entire line as thetable key (see also PseudocomponentTABLE_LINE in Internal Tables).

If you specify the key implicitly using FROM wa, the valuesfor the table key are taken from the corresponding components of the(structured) field wa. wa must be compatible with theline type of itab. In this way, you can access a table usingREAD without having to know the table key statically. If thetable key is empty, the system reads the first line.

Variant 3

READ TABLE itab WITH KEY k1 = v1 ... kn = vn [BINARY SEARCH] [additions].

Effect

The system uses the specified key to identify thecorrect line. If the type of a value is not compatible with the type ofthe corresponding key field, the system uses MOVElogic to convert the value into the type of the component beforereading the table. This is an asymmetric comparison logic, in which thecomponent type takes precedence over the value type.

The way in which the system looks for an entry in the table depends onits table type:

STANDARD TABLE:

If you use the ... BINARY SEARCH addition, the system uses abinary search. Otherwise, the search is sequential. This assumes thatthe internal table is sorted in ascending order in the sequence of thespecified key fields.

SORTED TABLE:

If the specified key fields form a left-justified extract of the tablekey, the search is binary, otherwise sequential.

HASHED TABLE:

Sequential search.

Notes

The system reads the first entry in which the specifiedcomponents k1 ... kn correspond with the values of v1 ... vn. If the type of a value and the type of the corresponding keyare incompatible, the system uses MOVE logic toconvert the value into the type of the component before reading thetable.

If your table has a non-structured line type, you can use thepseudocomponent TABLE_LINE to address the entire line as the key(see also Pseudocomponent TABLE_LINE with Internal Tables).

If you do not know the name of a component until runtime, you can usethe expression WITH KEY ... (ni) = vi ... to specify itdynamically as the contents of the field ni. If ni isempty at runtime, the system ignores the component. If nicontains an invalid component name, a runtime error occurs. If ni contains an empty name, the system ignores the key specification.

If the line type of the internal table contains object referencevariables as componetns or the entire line type is a referencevariable, you can use the attributes of the object to which thereference is pointing in a particular line as key values (seeAttributes of Objects as the Key of an Internal Tables).

If you specify a completely empty key, the system reads the firstentry from the table.

You restrict your specification using offset and length. This isvalid for both the static and dynamic variant.

Variant 4

READ TABLE itab INDEX i [additions].

Effect

Accessing the table entry with the index i.

Notes

Performance:

The quickest way to access a single line of an internal table isdirect access using an index, because the response time is then notlinked to the size of the table, and is restricted more or less to thetransport costs for a single line.

For hashed tables, the response time is constant. Accessing a tableusing the hash administration makes the response time around 3 timesslower than using index access.

If you use the key to access a table, the response time increase as thenumber of table entries and the size of the search key increase.Searching using a binary search is considerably quicker than using alinear search. Therefore, in many cases it can be quicker to sort thetable and then use the BINARY SEARCH addition.

The runtime required to read a line from a table with 100 entries usingthe index is around ca. 7 msn(standard microseconds), to read a line using a key of 30 bytes using abinary search, around 25 msn, and without binary search, around 100msn.

Using statements that use an explicit work area for internal tableswith a header line can avoid unnecessary value assignments. Zuweisungenvermieden werden.

Note

Runtime errors:

ITAB_EMPTY_KEY: Key missing when reading a table usingREAD ... WITH TABLE KEY.

ITAB_ILLEGAL_BINARY_SEARCH: When you read a SORTERD TABLE using the BINARY SEARCH addition, the specified key fieldsmust make up a left-justified extract of the key.

ITAB_KEY_COMPONENT_MISSING: Key specification missing.

ITAB_KEY_ILLEGAL_COMPONENT: Invalid key specification whenaccessing a key table.

LOOP - Loop Through an Internal Table

Basic form

LOOP AT itab. LOOP AT itab INTO wa.

LOOP AT itab ASSIGNING <fs>.

Additions:

1. ... FROM n1

2. ... TO n2

3. ... WHERE logexp

4. ... TRANSPORTING NO FIELDS

In an ABAP Objects context, a more severe syntaxcheck is performed that in other ABAP areas. See Short forms of line operations notallowed and Modifying internal tables in aloop not allowed.

Effect

Processes an internal table (DATA, TYPES) in a loop, introduced with the LOOPstatement, and concluded with ENDLOOP. All of the entries in the internal table are available forprocessing one after the other.

In "LOOP AT itab", the system uses the header line of theinternal table itab as the work area. If you use "LOOP ATitab INTO wa", it uses the explicit work area wa. In "LOOP AT itabASSIGNING <fs>", the field symbol <fs> points to theselected entry, that is, the line is not copied into a work area. Youcannot reassing the field symbol to another line of the table oranother field within the loop.

If the table is empty, or you use additions that result in no entrybeing read from it, the output area (field symbol) remains unchanged.Similarly, at the end of the loop, the output area contains the lastentry to have been selected from the table (if you used a field symbol,it will point to this entry.

If the table is empty, the system ignores all of the statementsbetween LOOP and ENDLOOP.

The sequence in which the entries are processed depends on thetable type as long as the table has not beensorted (SORT).

STANDARD TABLE:

The entries are processed in the sequence of their logical index. Atthe beginning of each loop pass, SY-TABIX is set to the logicalindex of the current table entry.

SORTED TABLE:

The entries are processed according to the sort sequence of the table.As with standard tables, SY-TABIX contains the logical index ofthe current entry.

HASHED TABLE:

The entries are processed in the order in which they were entered inthe table. Since hashed tables have no logical sequence, SY-TABIX is always 0.

When you leave a LOOP structure, SY-TABIX has the samevalues that it had before the loop.

Inserting and deleting lineswithin a LOOP affects subsequent loop passes.

There are special control level structures forinternal tables that you can use for control level processing withina LOOP structure.

Use the CONTINUE statement toterminate the current loop pass prematurely and start the next pass.Use the EXIT statement to terminate theentire loop processing.

At the end of the loop processing (after ENDLOOP), the returncode field SY-SUBRC indicates whether the loop was processed:

SY-SUBRC = 0:

At least one loop pass was processed.

SY-SUBRC = 4:

The loop was not processed because the tablecontains no entries or no entries satisfied the conditions.

The LOOP AT statement also fills thesystem fields SY-TFILL, SY-TLENG, and SY-TOCCU.

Example

Suppose the table itab is defined as follows:

DATA: BEGIN OF STRUC,

NAME(10) TYPE C,

SALDO TYPE P,

END OF STRUC,

ITAB LIKE TABLE OF STRUC.

The table is filled (APPEND) anddisplayed:

LOOP AT ITAB INTO STRUC.

WRITE: / STRUC-NAME, STRUC-SALDO.

ENDLOOP.

Notes

When you use the ASSIGNING <fs> addition,the system works directly with the entries in the internal table. Ifyou do this, you must not change the key components of a SORTEDor HASHED table, since this invalidates the order of the table.Consequently, all modifications (for example, MODIFY, MOVE, CLEAR), and all potential modifications (for example,passing a table as a CHANGING parameter, to the key componentsof sorted and hashed tables lead to a runtime error.

You cannot change an entire internal table within a loop. To changethe whole table, you use statements such as REFRESH, CLEAR , FREE, MOVE, SORT, or SELECT ... INTO/APPENDING. This also applies when you pass a table as a CHANGINGVALUE parameter.

If you only want to process part of an internal table (using theadditions FROM, TO, or WHERE), you should avoidusing control structures for control level processing , since the interaction between restricted LOOP and ATstatements is currently not precisely defined. ( AT NEW andAT END OF compare with the previous table line, and not with theline actually selected. A restricted LOOP may therefore notrecognize that a control level change has taken place.

If you use SUM in the LOOP anduse an explicit output area wa, the output area must becompatible with the line type of the internal table itab.

Addition 1

... FROM n1

Addition 2

... TO n2

Effect

Places the entries of the internal table from index (SY-TABIX) n1 up to and including index = n2 one byone into the output area.

You cannot use these additions with HASHED tables, since hashedtables do not support index operations.

Note

If one of the additions " FROM n1" or "TO n2 " is missing, the system processes the internal table from thefirst line or to the last line respectively.

Example

Displaying the 7th and 8th table entries:

LOOP AT ITAB INTO STRUC FROM 7 TO 8.

WRITE: / STRUC-NAME, STRUC-SALDO.

ENDLOOP.

Addition 3

... WHERE logexp

Effect

Places all entries of the internal table itabthat satisfy the condition logexp one by one into the outputarea. The condition logexp can be almost anylogical expression. The only restriction is that thefirst field in each comparison must be a component of the linestructure of itab.

Example

LOOP AT ITAB INTO STRUC WHERE SALDO <> 0.

WRITE: / STRUC-NAME, STRUC-SALDO.

ENDLOOP.

is the same as:

LOOP AT ITAB INTO STRUC.

CHECK STRUC-SALDO <> 0.

WRITE: / STRUC-NAME, STRUC-SALDO.

ENDLOOP.

Any operand that is not a component of the line structure of itab can be replaced with a functional method call. Functional methodsare methods in ABAP Objects that canhave any number of IMPORTING parameters and a singleRETURNING parameter.

Depending on the number of IMPORTING parameters, the methodcall can be written in the following ways:

No IMPORTING parameters: meth( )

One IMPORTING parameter: meth( f1 )

meth( p1 = f1 )

n IMPORTING parameters: meth( p1 = f1 ... pn =fn )

Example

CLASS c1 DEFINITION.

PUBLIC SECTION.

METHODS M1 IMPORTING NAME TYPE S_CARRNAME

RETURNING VALUE(CARR) TYPE S_CARR_ID.

ENDCLASS.

CLASS c1 IMPLEMENTATION.

METHOD M1.

SELECT CARRID UP TO 1 ROWS

FROM SCARR

INTO CARR

WHERE CARRNAME = NAME.

ENDSELECT.

ENDMETHOD.

ENDCLASS.

DATA: JTAB TYPE STANDARD TABLE OF SPFLI,

LINE TYPE SPFLI.

DATA: OREF TYPE REF TO C1.

START-OF-SELECTION.

SELECT * FROM SPFLI INTO TABLE JTAB.

CREATE OBJECT oref TYPE c1.

LOOP AT JTAB INTO LINE

WHERE CARRID = OREF-> M1( 'Lufthansa' ).

WRITE: / LINE-CARRID, LINE-CONNID,

LINE-CITYFROM, LINE-CITYTO.

ENDLOOP.

The actual paraemters are passed to the IMPORTING parametersof the method as described under variants 2 and 3 of theCALL METHOD statement.

If the line type of the internal table contains object referencevariables as components, or the entire line type is a referencevariable, you can use the attributes of the object to which thereference in a line is pointing as the first field in a comparison.

Example

See Attributes of objects as the key of an internal table

Since you cannot address components of a table with a single field asthe line type (for example, type I), you can use theTABLE_LINE expression in these cases to address the whole tableline in the WHERE condition (also auch Pseudocomponent TABLE_LINE in internal tables).

Example

WHERE condition for a table with type I

DATA: inttab TYPE STANDARD TABLE OF i WITH NON-UNIQUE

DEFAULT KEY,

wa_inttab TYPE I.

LOOP AT inttab INTO wa_inttab WHERE table_line > 5 AND

table_line <= 10.

...

ENDLOOP.

Notes

When you use the WHERE condition with a STANDARD orHASHED table, a full table scan is always required.

If you are working with a SORTED TABLE, the partial sequentialprocessing can be optimized so that only the entries that actuallysatisfy the WHERE condition are processed. This optimizationrequires that the WHERE condition be specified in the form

WHERE k1 = v1 AND k2 = v2 AND ... AND kn = vn

where the components k1, ..., kn are in the same sequence as thebeginning of the table key.

The operands that occur in the logical expression in the WHEREcondition are only evaluated when the system enters the loop. Changingtheir values within the loop processing itself has no effect on theWHERE condition itself.

... WHERE logexp is an ABAP logicalexpression , that is, a logical expression as used in IF,WHILE, or CHECK, and not as used in a WHERE clause in the SQL sense, as in SELECT/UPDATE/DELETE ... WHERE.

The interaction between LOOP AT ... WHERE andATcontrol level processing statements iscurrently undefined. For this reason, you should avoid using ATNEW/END OF or FIRST/LAST in a LOOP with a WHEREaddition.

The performance of a LOOP AT ... WHERE statement will improvedramatically if the fields in the comparisons have the same types.Ideally, you should define them as follows:

DATA comparison_field LIKE table_field.

Example:

DATA: CMP_NAME LIKE STRUC-NAME.

CMP_NAME = 'Harold'.

LOOP AT ITAB INTO STRUC WHERE NAME = CMP_NAME.

WRITE: / STRUC-NAME, STRUC-SALDO.

ENDLOOP.

Addition 4

... TRANSPORTING NO FIELDS

Effect

The system does not transport any fields from theinternal table to the output area. You can only use this additiontogether with a WHERE condition. Specifying an explicit workarea using INTO wa makes no sense together with TRANSPORTINGNO FIELDS and is therefore not allowed.

You can use the addition to determine a set of line indexes (index set)or the number of lines in a table that satisfy a particular condition.

Example

Determining the number of entries COUNT in aname table TAB containing the name 'John', and the correspondingindex set INDEX_SET.

TYPES: BEGIN OF NAMETAB_TYPE,

NAME(30) TYPE C,

END OF NAMETAB_TYPE.

DATA: NAMETAB TYPE STANDARD TABLE OF NAMETAB_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 100,

COUNT TYPE I,

INDEX_SET TYPE STANDARD TABLE OF SY-TABIX WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 10,

WA_INDEX_SET TYPE SY-TABIX.

LOOP AT NAMETAB TRANSPORTING NO FIELDS WHERE NAME CS 'John'.

WA_INDEX_SET = SY-TABIX.

APPEND WA_INDEX_SET TO INDEX_SET.

ADD 1 TO COUNT.

ENDLOOP.

Related

Loop Structures:

DO, WHILE

Table Processing:

APPEND, COLLECT , INSERT,MODIFY, DELETE , SORT ,AT NEW/END OF/FIRST/LAST,READ TABLE.

Regards,

Clemens

Read only

Former Member
0 Likes
2,541

Why just copy and paste the help again?!

We should be helping people to help themselves - otherwise we will be copying help documentation into these forums forever...

Gareth.

Read only

Clemenss
Active Contributor
0 Likes
2,541

Hi Gareth,

I am sorry.

Admitted: It really makes me angry to read yahoo-groups-like questions in a technical forum.

We have FAQs, Wikis and and still we have google.

Reading (more and more) questions in this forum I just don't believe people really tried to find out the answer by themselves.

We should be helping people to help themselves. That's why I said: F1 helps. If people can't do that, I'll copy and paste.

Regards,

Clemens