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

Read statement

Former Member
0 Likes
9,059

Hi,

At what particular situations we are using read statement.

Murali

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
7,593

u canuse read statement when u want to read records from an internal table..

regards,

lavanya

10 REPLIES 10
Read only

Former Member
0 Likes
7,593

HI murali,

each READ statement reads from a newline;; the READ statement can transfer any object of intrinsic type from the standard input

Reading Lines of Tables

To read a single line of any table, use the statement:

READ TABLE itab key result.

For the statement to be valid for any kind of table, you must specify the entry using the key and not the index. You specify the key in the key part of the statement. The result part can specify a further processing option for the line that is retrieved.

If the system finds an entry, it sets sy-subrc to zero, if not, it takes the value 4, as long as it is not influenced by one of the possible additions. If the internal table is an index table, sy-tabix is set to the index of the line retrieved. If the table has a non-unique key and there are duplicate entries, the first entry is read.

Specifying the Search Key

The search key may be either the table key or another key.

If you read entries from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search.

Using the Table Key

To use the table key of itab as a search key, enter key as follows:

READ TABLE itab FROM wa result.

or as follows

READ TABLE itab WITH TABLE KEY k1 = f1 ... kn = fn result.

In the first case, wa must be a work area compatible with the line type of itab. The values of the key fields are taken from the corresponding components of the work area.

In the second case, you have to supply the values of each key field explicitly. It is not allowed to specify the same key specifications several times. If you do not know the name of one of the key fields until runtime, you can specify it dynamically as the content of a field n1 ... nn using (n1) = f1 (n2) = f2 If the data types of f1 ... fn are not compatible with the key fields, the system converts them. If the row type of the internal table is not structured, you can specify a comparison with the pseudo-component table_line.

READ TABLE itab WITH TABLE KEY table_line = f result.

The contents of the entire table line are compared with the contents of field f. If f is not compatible with the line type of the table, the value is converted into the line type. The search key allows you to find entries in internal tables that do not have a structured line type, that is, where the line is a single field or an internal table type.

If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points can be specified as key values using comp->attr or table_line->attr.

The system searches for the relevant table types as follows:

· Standard tables

Linear search, where the runtime is in linear relation to the number of table entries.

· Sorted tables

Binary search, where the runtime is in logarithmic relation to the number of table entries.

· Hashed tables

The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.

Using a Different Search Key

READ TABLE itab WITH KEY k1 = f1 ... kn = fn result.

The search key consists of arbitrary table fields k1 ... kn. If you do not know the name of a component until runtime, you can specify it dynamically as the content of a field n1 ... nn using (n1) = f1 (n2) = f2 If n1 ... nn is empty when the statement is executed, the search field is ignored. If the data types of f1 ... fn are not compatible with the components, the system converts them. You can restrict the search to partial fields by specifying offset and length.

If the row type of the internal table is not structured, you can specify a comparison with the pseudo-component table_line. You can also specify the attributes of objects as keys.

The search is linear for all table types. The runtime is in linear relation to the number of table lines.

Specifying the Extra Processing Option

You can specify an option that specifies what the system does with the table entry that it finds.

Using a Work Area

You can write the table entry read from the table into a work area by specifying result as follows:

READ TABLE itab key INTO wa COMPARING f1 f2 ...

[TRANSPORTING f1 f2 ... |ALL FIELDS

|NO FIELDS].

If you do not use the additions COMPARING or TRANSPORTING, the contents of the table line must be convertible into the data type of the work area wa. If you specify COMPARING or TRANSPORTING, the line type and work area must be compatible. You should always use a work area that is compatible with the line type of the relevant internal table.

If you use the COMPARING addition, the specified table fields f1 ... fn of the structured line type are compared with the corresponding fields of the work area before being transported. If you use the ALL FIELDS option, the system compares all components. If the system finds an entry with the specified key key and if the contents of the compared fields are the same, sy-subrc is set to 0. If the contents of the compared fields are not the same, sy-subrc returns the value 2. If the system cannot find an entry, sy-subrc is set to 4. If the system finds an entry, it copies it into the target work area regardless of the result of the comparison.

If you use the TRANSPORTING addition, you can specify the table fields f1 ... fn of the structured line type that you want to transport into the work area. If you specify ALL FIELDS without TRANSPORTING, the contents of all of the fields are transported. If you specify NO FIELDS, no fields are transported. In the latter case, the READ statement only fills the system fields sy-subrc and sy-tabix. Specifying the work area wa with TRANSPORTING NO FIELDS is unnecessary, and should be omitted.

In both additions, you can also specify the fields f1 ... fn u2026 dynamically using (n1) (n2)u2026 as the contents of a field n1 ... nn. If n1 ... nn is empty when the statement is executed, it is ignored. You can also restrict all fields f1 ... fn to subfields by specifying offset and length.

Using a Field Symbol

You can assign the table entry read from the table to a field symbol by specifying result as follows:

READ TABLE itab key ASSIGNING .

thanks

karthik

reward me if usefull

Read only

Former Member
0 Likes
7,593

Hi,

While looping within an internal table, the other internal tables are read using the READ statement.

Reward points if useful.

Regards,

Mansi.

Read only

0 Likes
4,519

need this t - code

 

Read only

Former Member
0 Likes
7,594

u canuse read statement when u want to read records from an internal table..

regards,

lavanya

Read only

GauthamV
Active Contributor
0 Likes
7,593

hi,

for example u have 2 internal tables.

u selected data into 1 internal table.

if u want to move this data into second internal table, then u have to read the data, first into ur internal table and then move it to other one.

reward points if hlpful.

Read only

Former Member
0 Likes
7,593

Hi,

When ever we have to do any type of processing based on a single record of a internal table we use read statement.

Please go below documentation

READ for Files

Reads a file.

Syntax

READ DATASET <dsn> INTO <f>

[MAXIMUM LENGTH <maxlen>]

[ACTUAL LENGTH <len>].

Reads the contents of the file <dsn> on the application server to the variable <f>. The amount of data can be specified using MAXIMUM LENGTH. The number of bytes transferred can be written to <len> using ACTUAL LENGTH.

READ for any Internal Table

Reads a line of an internal table.

Syntax

READ TABLE <itab> FROM <wa>

|WITH TABLE KEY <k1> = <f1>... <kn> = <fn>

|WITH KEY = <f>

|WITH KEY <k1> = <f1>... <kn> = <fn>

INTO <wa> [COMPARING <f1> <f2>... |ALL FIELDS]

[TRANSPORTING <f1> <f2>... |ALL FIELDS|NO FIELDS]

|ASSIGNING <FS>

|REFERENCE INTO <dref>.

This statement reads either the line of the internal table with the same key as specified in the work area <wa>, the line with the key specified in the TABLE KEY addition, the line that corresponds fully to <f>, or the one corresponding to the freely-defined key in the KEY addition. The contents of the line are either written to the work area <wa>, or the line is assigned to the field symbol <FS>. If you assign the line to a work area, you can compare field contents and specify the fields that you want to transport.

READ for Index Tables

Reads a line of an internal table.

Syntax

READ TABLE <itab> INDEX <idx> INTO <wa>...

| ASSIGNING <FS>

| REFERENCE INTO <dref>.

The line with index 7 is read. The result is specified as with any internal table.

READ for Lists

Reads the contents of a line from a list.

Syntax

READ LINE <n> [INDEX <idx>] [OF CURRENT PAGE|OF PAGE <p>]

|CURRENT LINE

[FIELD VALUE <f1> [INTO <g1>]... <fn> [INTO <gn>]].

Reads either the line <n> on the current or specified list or page, or the last line to have been selected by the user. The addition specifies the fields that you want to read, and the target fields into which they should be placed. The entire line is always placed in the system field SY-LISEL, and the HIDE area is filled for the line.

READ for Programs

Reads ABAP programs into the program library.

Syntax

READ REPORT <prog> INTO <itab>.

Copies the lines of the program <prog> into the internal table <itab>.

Reward some points.

Regards,

Anomitro Guha

Edited by: Anomitro Guha on Jun 16, 2008 4:04 PM

Read only

Former Member
0 Likes
7,593

hiii

IF sy-subrc EQ 0.

LOOP AT i_output INTO wa_output.

READ TABLE i_mard INTO wa_mard WITH KEY matnr = wa_output-matnr.

wa_output-lgort = wa_mard-lgort.

MODIFY i_output FROM wa_output.

CLEAR wa_output.

ENDLOOP. " LOOP AT i_output

ENDIF. " IF sy-subrc EQ 0

Read statement will read internal tables record one by one for the condition you have given.It is also useful to change data in any column of internal table.that we can do by using MODIFY statement after READ statement.

We can do the same thing with loop & condition but here READ statement is good for performance wise.so should use it

rewad if useful

thx

twinkal

Read only

Former Member
0 Likes
7,593

hi to read the data from with one table to another with the key relation..

check this example..

DATA: BEGIN OF INT_TABLE OCCURS 100,

COMP1,

COMP2,

COMP3,

END OF INT_TABLE.

FORM PUT_ENTRY USING ENTRY LIKE LINE OF INT_TABLE.

READ TABLE INT_TABLE WITH KEY COMP2 = ENTRY-COMP2

BINARY SEARCH

TRANSPORTING NO FIELDS.

IF SY-SUBRC <> 0.

INSERT ENTRY INTO INT_TABLE INDEX SY-TABIX.

ENDIF.

ENDFORM.

Read only

Former Member
0 Likes
7,593

Dought is clear

Read only

JaiShankarJ
Discoverer
0 Likes
4,519

Need to knowck of the k1,k2,k3