‎2008 May 23 3:22 PM
Hi
Can any one explain me the functionality for read table with example???
Thanks and Regards,
Keny.
‎2008 May 23 3:42 PM
Hi ARUN,
We use READ TABLE to read single table lines from the index internal tables,
syntax:
READ TABLE itab INDEX n INTO wa.
we can use sy-subrc after the read table to know if a line is read or not.
we can use this READ TABLE with addition TRANSPORTING NO FEILDS when we want to know whether or not line exists, which will not read any data, just tell you if line exists or not.
we can use optional condition COMPARING f1 f2 .....fn to ascertain whether or not the line to be read n has specific colum contents.
if we want to compare all feilds we can use COMPARING ALL FIELDS.
syntax:
READ TABLE itab INTO wa COMPARING f1 f2 f3 ....fn TRANSPORTING NO FIELDS.
this above statement is useful for finding whether rows are there in internal table with feilds f1 f2 f3 ...fn with out transporting them, means more efficient than just looping all the internal table.
cheers
Kiran.
Edited by: Kiran Endreddy on May 23, 2008 4:44 PM
‎2008 May 23 4:02 PM
‎2008 May 23 4:32 PM
Hi Arun,
I will try to explain with a simple example
Types: Begin of ty_itab,
x type string,
y type i,
end of itab.
data: itab type table of ty_itab,
wa type ty_itab.
Read is used to read a single record in the itab or to check whether the record is there or not.
wa-x = 'Test1'.
wa-y = 1.
append wa to itab.
wa-x = 'Test2'.
wa-y = 2.
append wa to itab.
Now when you do a read on itab.
read table itab into wa with key x = 'Test1'.
Now wa contains wa-x = test1 and wa-y = 1.
Collect is when you want to sum up all the records based on particular columns. This can be used only when all your columns are numeric except your key columns.
In this example
you already have an itab with 2 records and column X is considered as a Key
1st record
Test1 and 1
2nd record
Test2 and 2
Now you add 3rd record using collect statement.
First fill wa.
wa-x = 'Test1'.
wa-y = 3.
Now collect wa into itab.
Now as there is record whose x value is Test1. It will be added to that record.
Now you will have 2 records with changed values.
1st record
Test1 and 4 (This is because the new record numeric columns are be added to the record as their key is same)
2nd record
Test2 and 2
Now for e.g. you want add one more record
wa-x = 'Test3'
wa-y = 4
collect wa into itab.
Then you will have 3 records as there is no record which matches with the key of the new record ('Test3').
1st record
Test1 and 4
2nd record
Test2 and 2
3rd record
Test3 and 4
I hope my understanding is clear.
Thanks,
kiran