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

Data retrieval from an internal table

dayakar_sama
Explorer
0 Likes
997

Hi all,

I am having an internal table, in which i had a data for four fields, from which i want to select particular type of data, where data for 3 fields are exactly same and the 4th field data can anything.

Example:

field 1
field 2
field 3field 4
123445568
123445369
1245672356
346778345
3467787898

from this i want to retrieve only 1st two rows and last two rows.

please help.

Thanks,

sama

Moderator message : Requirements dumping not allowed, show the work you have already done, discussion locked.

Message was edited by: Vinod Kumar

1 ACCEPTED SOLUTION
Read only

dayakar_sama
Explorer
0 Likes
962

Hi please check this

DATA: zabctable_tab TYPE SORTED TABLE OF zabctable
                   WITH UNIQUE KEY zf1 ,
       zabctable_wa  LIKE LINE OF zabctable_tab.

DATA subrc TYPE sy-subrc.

SELECT *
        FROM zabctable
        INTO TABLE zabctable_tab
       .

subrc = sy-subrc.
WHILE subrc = 0.
*  zabctable_wa-zf5 = 0.
   READ TABLE zabctable_tab
        INDEX sy-index
        INTO zabctable_wa COMPARING zf3
                               TRANSPORTING
                                     zf2 zf3 zf4 zf5 zf6 zf7.
   CASE sy-subrc.
     WHEN 0.
       WRITE: / zabctable_wa-zf1, zabctable_wa-zf3,
         zabctable_wa-zf4, zabctable_wa-zf5, zabctable_wa-zf6, zabctable_wa-zf7 COLOR = 6.
       subrc = sy-subrc.
     WHEN 2.
       WRITE: / 'sorry daya' COLOR = 5.
       subrc = 0.
     WHEN 4 OR 8.
       EXIT.
   ENDCASE.
ENDWHILE.

this is what i am trying to do

and the table looks like


thanks,

sama

6 REPLIES 6
Read only

Former Member
0 Likes
962

Hi,

you can read the data from table using index.

Read table tabname INDEX 1.

Read table tabname INDEX 2.

DESCRIBE TABLE tabname LINES lv_lines.

Read table tabname INDEX lv_lines. ( for last row.)

Read table tabname INDEX lv_lines-1 ( for 2nd last row)

Regards,

Gaurav.

Read only

Former Member
0 Likes
962

Hi,

First sort the table in any order and then read table with your fields comapring to work area or by index.

SORT ITAB BY field1 field2 field3 ASCENDING.

READ TABLE itab <key_field> INTO wa_itab COMPARING field1 field2 field3.

Regards

Ben

Read only

koolspy_ultimate
Active Contributor
0 Likes
962

you can also use delete.

I.e delete itab where field2 = 45.

loop at itab.

" print your data from internal table " enter full details

endloop.

o/p = you will get data of only 1st two rows and last two rows.

Read only

Former Member
0 Likes
962

Hi Sama,

To retrive data from the internal table you can use REA TABLE and specifying the index to read the table cotents in your work area. Please see the code below to retrieve the first 2 and last 2 lines of the internal table..

DESCRIBE TABLE <itab> LINES lV_LINES.

READ TABLE <itab> INTO wa_final INDEX 1.

APPEND wa_final to it_final.

READ TABLE <itab> INTO WA_FINAL INDEX 2.

APPEND wa_final to it_final.

READ TABLE <itab> INTO WA_FINAL INDEX lv_lines - 1.

APPEND wa_final to it_final.

READ TABLE <itab> INTO WA_FINAL INDEX lv_lines.

APPEND wa_final to it_final.

Read only

dayakar_sama
Explorer
0 Likes
962

Hi please check this

DATA: zabctable_tab TYPE SORTED TABLE OF zabctable
                   WITH UNIQUE KEY zf1 ,
       zabctable_wa  LIKE LINE OF zabctable_tab.

DATA subrc TYPE sy-subrc.

SELECT *
        FROM zabctable
        INTO TABLE zabctable_tab
       .

subrc = sy-subrc.
WHILE subrc = 0.
*  zabctable_wa-zf5 = 0.
   READ TABLE zabctable_tab
        INDEX sy-index
        INTO zabctable_wa COMPARING zf3
                               TRANSPORTING
                                     zf2 zf3 zf4 zf5 zf6 zf7.
   CASE sy-subrc.
     WHEN 0.
       WRITE: / zabctable_wa-zf1, zabctable_wa-zf3,
         zabctable_wa-zf4, zabctable_wa-zf5, zabctable_wa-zf6, zabctable_wa-zf7 COLOR = 6.
       subrc = sy-subrc.
     WHEN 2.
       WRITE: / 'sorry daya' COLOR = 5.
       subrc = 0.
     WHEN 4 OR 8.
       EXIT.
   ENDCASE.
ENDWHILE.

this is what i am trying to do

and the table looks like

thanks,

sama

Read only

dayakar_sama
Explorer
0 Likes
963

Hi please check this

DATA: zabctable_tab TYPE SORTED TABLE OF zabctable
                   WITH UNIQUE KEY zf1 ,
       zabctable_wa  LIKE LINE OF zabctable_tab.

DATA subrc TYPE sy-subrc.

SELECT *
        FROM zabctable
        INTO TABLE zabctable_tab
       .

subrc = sy-subrc.
WHILE subrc = 0.
*  zabctable_wa-zf5 = 0.
   READ TABLE zabctable_tab
        INDEX sy-index
        INTO zabctable_wa COMPARING zf3
                               TRANSPORTING
                                     zf2 zf3 zf4 zf5 zf6 zf7.
   CASE sy-subrc.
     WHEN 0.
       WRITE: / zabctable_wa-zf1, zabctable_wa-zf3,
         zabctable_wa-zf4, zabctable_wa-zf5, zabctable_wa-zf6, zabctable_wa-zf7 COLOR = 6.
       subrc = sy-subrc.
     WHEN 2.
       WRITE: / 'sorry daya' COLOR = 5.
       subrc = 0.
     WHEN 4 OR 8.
       EXIT.
   ENDCASE.
ENDWHILE.

this is what i am trying to do

and the table looks like


thanks,

sama