Application Development 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 statement

Former Member
0 Kudos
275

what is the exact use of read statement. How it is used in int.table & database table.

8 REPLIES 8

Former Member
0 Kudos
75

Hi,

it's used only with internal table and used to read value in the table, only from key value (Only = statement is accepted)

Ex.:

READ TABLE field

FROM TABLE <itable>

WITH KEY <key1> = <ikey1>.

rgs

former_member386202
Active Contributor
0 Kudos
75

Hi,

It is used to read data from internal table, it reads single record at a time.

ex.

IF NOT it_mkpf[] IS INITIAL.

LOOP AT it_mkpf INTO wa_mkpf.

lv_index = sy-tabix.

*--Clear

CLEAR : wa_vbrp.

READ TABLE it_vbrp INTO wa_vbrp WITH KEY vgbel = wa_mkpf-xblnr

BINARY SEARCH.

IF sy-subrc EQ 0.

wa_mkpf-fkdat = wa_vbrp-fkdat.

wa_mkpf-kunnr = wa_vbrp-kunnr.

ENDIF.

MODIFY it_mkpf FROM wa_mkpf INDEX lv_index TRANSPORTING

fkdat kunnr.

*--Clear

CLEAR : wa_mkpf.

ENDLOOP.

Reagrds,

Prashant

sreeramkumar_madisetty
Active Contributor
0 Kudos
75

Hi

To Read the data from the Database.

Read table Internal Table with key field where condition.

Regards,

Sree

Former Member
0 Kudos
75

hi suganya,

Read stmt is used to read the data in internal table but not for DB table.

it fetches the req. record from internal table which meets the where clause condition at first iteration.

<b><i>Reward points if useful</i></b>

chandra

Former Member
0 Kudos
75

Hi

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.

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. If you do not know the name of one of the key fields until runtime, you can specify it as the content of a field <ni> using the form (<ni>) = <fi>. If the data types of <fi> are not compatible with the key fields, the system converts them. The system searches for the relevant lines 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

To use a key other than the table key as a search key, enter <key> as follows:

READ TABLE <itab> WITH KEY = <f> <result>.

or as follows

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

BC - ABAP Programming SAP AG

In the first case, the whole line of the internal table is used as the search key. 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. In the second case, the search key can consist of any of the table fields <k1>...<kn>. If you do not know the name of one of the components until runtime, you can specify it as the content of a field <ni> using the form (<ni>) = <fi>. If <ni> is empty when the statement is executed, the search field is ignored. If the data types of <fi> are not compatible with the components in the internal table, the system converts them. You can restrict the search to partial fields by pecifying offset and length.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> ...

|ALL FIELDS]

[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 <fi> 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, it 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 of the structured line type that you want to transport into the work area. If you specify ALL FIELDS without RANSPORTING, 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 SYSUBRC and SY-TABIX. Specifying the work area <wa> with TRANSPORTING NO FIELDS is unnecessary, and should be omitted.

In both additions, you can specify a field <fi> dynamically as the contents of a field <ni> in the form (<ni>). If <ni> is empty when the statement is executed, it is ignored. You can restrict the search to partial fields by specifying offset and length.

Using a Field SymbolYou can assign the table entry read from the table to a field symbol by specifying <result> as

follows:

READ TABLE <itab> <key> ASSIGNING <FS>.

After the READ statement, the field symbol points to the table line. If the line type is structured, you should specify the same type for the field symbol when you declare it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.For further information about assigning table lines to field symbols, refer to Access Using Field Symbols.

SORT ITAB.

READ TABLE ITAB WITH KEY VAR1 <= SYDATUM BINARY SEARCH.

OR

READ ITAB WITH KEY VAR1 <= SYDATUM

YOU CAN USE ANY ONE READ STATEMENT

1ST ONE IS BINARY SEARCH IT IS BETTER IN PERFORMANCE

YOU HAVE TO SORT THAT ITAB BEFORE THIS STATEMENT

2ND ONE IS NORMAL SEARCHIN TECH IT IS NOT GOOD IN THE PERFORMANCE POINT OF VIEW

Former Member
0 Kudos
75

Read statement is used to read a record from an internal table.

Read table is used to read the contents of itab inside a loop. the read table is used with binary search option and the itab1 should be sorted befeore using read table

for more info:

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm

Reading records with keys

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm

Reading lines with Index

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3730358411d1829f0000e829fbfe/content.htm

Read table will get the first record that saitisfies the condition specified in the with key statement into the header.

if an itab contains the following data:

field1 field2

1 one

2 two

3 three

4 four

5 five

And if you read the itab like this:

read table itab into WA with key field1 = '3'.

if sy-subrc = 0.

write:/ wa.

endif.

The output will be

3 three.

Regards

Vasu

Former Member
0 Kudos
75

Hi

Read is used to read the internal table record based on the Key fields .

It gets fetches one record into the work area given .

Go through the F1 Help for the Read .

It will be helpful !!!

Thanks

Praveen

mahaboob_pathan
Contributor
0 Kudos
75

If we want to find out particular record ina internal table .we use this.

READ TABLE ITAB INDEX 5.

we can also find out the record by using field name.

READ TABLE ITAB WITH KEY F1 = 3

F2 = 5.

here EQ will not work bcoz EQ is not used for fields name comparision.