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

single record fetching

Former Member
0 Likes
2,932

Hi all,

I m doing a report in which i have collected the data in 3 internal tables and now i want to disply them

row by row . like only 1 row of 1st table , only 1st row of 2nd table, only 1st row of 3rd table. then 2nd of each...

im bit confused in it. im trying to do it by looping it but its not getting so plzz help me out..

thanx.........

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,910

Hi,

This is easy if u have same number of records in all the tables.

Use the following coding

loop at itab1.

read table itab1 index sy-index.

//write here

read table itab2 index sy-index.

//write here

read table itab3 index sy-index.

//write here

endloop.

Please let me know if the table has differnt number of entries.... I ll give the code for it

Edited by: Bala Shanmuga Priyan on Jan 6, 2009 8:13 AM

37 REPLIES 37
Read only

Former Member
0 Likes
2,910

Hi,

I am getting ur points. Can u show me the code which u did.

Read only

Former Member
0 Likes
2,910

use READ table itab1 index 1

READ table itab2 index 1

READ table itab3 index 1

in index u can use a variable so next time it gets row 2 of all table and then row 3 like that....

Edited by: kartik tarla on Jan 6, 2009 12:33 PM

Read only

0 Likes
2,910

>

> use READ table itab1 index 1

> READ table itab2 index 1

> READ table itab3 index 1

>

> in index u can use a variable so next time it gets row 2 of all table and then row 3 like that....

>

> Edited by: kartik tarla on Jan 6, 2009 12:33 PM

can u tel me this in more brief..?

bcoz im using nested looping but in 3rd table the thngs are going wrong.

thanx

Read only

Former Member
0 Likes
2,910

HI Nilesh,

Prepare one temporaty table, looping first table and append to the temp table with the INDEX 1 and like that do all for other tables also.

hope it will solves your problem.

Thanks

Read only

Former Member
0 Likes
2,910

Hi,

In my understanding of ur problem, u can loop the 1st internal table first and do the write statement and loop the 2nd one and do write and loop the 3rd one and do write the entries.

Thanks.

Read only

Former Member
0 Likes
2,910

YOU CAN DO LIKE THIS.................

DATA : v_index type sy-tabix.
loop at ITAB1 into WA_TAB1.
V_INDEX = SY-TABIX.
WRITE : / "WRITE ROW OF ITAB1
READ ITAB2 INTO WA_TAB2 INDEX V_INDEX.
READ ITAB3 INTO WA_TAB3 INDEX V_INDEX.
WRITE : / "WRITE ROW OF ITAB2
WRITE : / "WRITE ROW OF ITAB3
endloop.

Read only

Former Member
0 Likes
2,910

Hi,

use SELECT SINGEL................

Regards,

Ps

Read only

Former Member
0 Likes
2,911

Hi,

This is easy if u have same number of records in all the tables.

Use the following coding

loop at itab1.

read table itab1 index sy-index.

//write here

read table itab2 index sy-index.

//write here

read table itab3 index sy-index.

//write here

endloop.

Please let me know if the table has differnt number of entries.... I ll give the code for it

Edited by: Bala Shanmuga Priyan on Jan 6, 2009 8:13 AM

Read only

0 Likes
2,910

>

> Hi,

>

> This is easy if u have same number of records in all the tables.

>

> Use the following coding

>

> loop at itab1.

> read table itab1 index sy-index.

> //write here

> read table itab2 index sy-index.

> //write here

> read table itab2 index sy-index.

> //write here

> endloop.

>

> Please let me know if the table has differnt number of entries.... I ll give the code for it

yes,

1st table may have 2 entries. so at start 1st record of 1st tab. 1 or 2 records of 2nd tab. same like 3rd.

again 2nd row of 1st tab. corresponding records from 2nd and then 3rd...

thanx..

Read only

0 Likes
2,910

>

> >

> > Hi,

> >

> > This is easy if u have same number of records in all the tables.

> >

> > Use the following coding

> >

> > loop at itab1.

> > read table itab1 index sy-index.

> > //write here

> > read table itab2 index sy-index.

> > //write here

> > read table itab2 index sy-index.

> > //write here

> > endloop.

> >

> > Please let me know if the table has differnt number of entries.... I ll give the code for it

>

> yes,

> 1st table may have 2 entries. so at start 1st record of 1st tab. 1 or 2 records of 2nd tab. same like 3rd.

> again 2nd row of 1st tab. corresponding records from 2nd and then 3rd...

>

> thanx..

plz reply back with code as u told b4....

Read only

0 Likes
2,910

Hi Nilesh,

Use the following code. It works for all cases!!!!

Data : f1,f2,f3.

while f1 NE 'X' or f2 NE 'X' or f3 NE 'X'.
   
     if f1 NE 'X'.
        read table itab1 index sy-index.
        if sy-subrc <> 0.
            f1 = 'X'.
        else.
           //write contents of table 1!!
        endif.
    else.
      //Leave space for blank table 1 entries!!!

     endif. 
    
      if f2 NE 'X'.
        read table itab2 index sy-index.
        if sy-subrc <> 0.
            f2 = 'X'.
        else.
           //write contents of table 2!!
        endif.
    else
         //Leave space for blank table 2 entries!!!
     endif.

  if f3 NE 'X'.
        read table itab3 index sy-index.
        if sy-subrc <> 0.
            f3 = 'X'.
        else.
           //write contents of table 3!!
        endif.
    else
         //Leave space for blank table 3 entries!!!
     endif.
endwhile.

Hpe this helps!!!

Edited by: Bala Shanmuga Priyan on Jan 6, 2009 8:32 AM

Edited by: Bala Shanmuga Priyan on Jan 6, 2009 8:33 AM

Edited by: Bala Shanmuga Priyan on Jan 6, 2009 8:33 AM

Edited by: Bala Shanmuga Priyan on Jan 6, 2009 8:37 AM

Read only

0 Likes
2,910

Hi i Made a few changes to code please try the new code!!

Read only

0 Likes
2,910

what is the easy way to display o/p like this using loops.

1st row of 1st table.

1st row of 2nd table

1st row of 3rd table

2nd row of 1st table.

2nd row of 2nd table

2nd row of 3rd table.

thats it.

but by nesting the tables its giving prob.

thanx.....

Read only

0 Likes
2,910

>

> Hi i Made a few changes to code please try the new code!!

for that i need some time

thanxx.....

Read only

0 Likes
2,910

>

> Hi i Made a few changes to code please try the new code!!

thanx i need some time to try it...

thanx..

Read only

0 Likes
2,910

>

> Hi i Made a few changes to code please try the new code!!

thanx i need some time to try it...

thanx..

Read only

0 Likes
2,910

>

> Hi i Made a few changes to code please try the new code!!

thanx a lot.

but i need some time to do it..

thanx...

Read only

0 Likes
2,910

Hi Nilesh,

Without nesting the tables also it is possible to achive the desired output unless the data, to be displayed from the various tables, is dependant. With the DO-ENDDO loop i think you will get the same output.

Read only

0 Likes
2,910

>

> Hi i Made a few changes to code please try the new code!!

thanx a lot.

but i need some time to do it..

thanx.

Read only

0 Likes
2,910

>

> Hi i Made a few changes to code please try the new code!!

thanx a lot.

but i need some time to do it..

thanx.

Read only

0 Likes
2,910

try this


describe table itab1 lines idx1.
describe table itab2 lines idx2.
describe table itab3 lines idx3.

v_count = 0.
loop at itab1 into wa1.

if v_count < idx2.
   read table itab2 into wa2 index v_count.
endif.
if v_count < idx3.
   read table itab3 into wa3 index v_count.
endif.
"Here u will get all three work areas so u can work on them accordingly
"suppose u want to print them then just check that they are not INITIAL then proceed with the processing
endloop.

The table which u wud be using for looping shud be the one having most number of records

кu03B1ятu03B9к

Edited by: kartik tarla on Jan 6, 2009 2:25 PM

Read only

0 Likes
2,910

sorry for the copies

Edited by: kartik tarla on Jan 6, 2009 2:23 PM

Read only

0 Likes
2,910

sorry for the copies

Edited by: kartik tarla on Jan 6, 2009 2:23 PM

Read only

0 Likes
2,910

>

> Hi Nilesh,

>

> Use the following code. It works for all cases!!!!

>

>

>

Data : f1,f2,f3.
> 
> while f1 NE 'X' or f2 NE 'X' or f3 NE 'X'.
>    
>      if f1 NE 'X'.
>         read table itab1 index sy-index.
>         if sy-subrc <> 0.
>             f1 = 'X'.
>         else.
>            //write contents of table 1!!
>         endif.
>     else.
>       //Leave space for blank table 1 entries!!!
> 
>      endif. 
>     
>       if f2 NE 'X'.
>         read table itab2 index sy-index.
>         if sy-subrc <> 0.
>             f2 = 'X'.
>         else.
>            //write contents of table 2!!
>         endif.
>     else
>          //Leave space for blank table 2 entries!!!
>      endif.
> 
>   if f3 NE 'X'.
>         read table itab3 index sy-index.
>         if sy-subrc <> 0.
>             f3 = 'X'.
>         else.
>            //write contents of table 3!!
>         endif.
>     else
>          //Leave space for blank table 3 entries!!!
>      endif.
> endwhile.

>

>

> Hpe this helps!!!

>

> Edited by: Bala Shanmuga Priyan on Jan 6, 2009 8:32 AM

why the sy-index value is not changing here, its remains on one (1) each time so its just showing the first record of the table. what could be the problem..??

thanx...

Read only

0 Likes
2,910

Try this code

Data : f1,f2,f3.
Data counter type i value 1.
 
while f1 NE 'X' or f2 NE 'X' or f3 NE 'X'.
   
     if f1 NE 'X'.
        read table itab1 index counter.
        if sy-subrc  0.
            f1 = 'X'.
        else.
           //write contents of table 1!!
        endif.
    else.
      //Leave space for blank table 1 entries!!!
 
     endif. 
    
      if f2 NE 'X'.
        read table itab2 index counter.
        if sy-subrc  0.
            f2 = 'X'.
        else.
           //write contents of table 2!!
        endif.
    else
         //Leave space for blank table 2 entries!!!
     endif.
 
  if f3 NE 'X'.
        read table itab3 index counter.
        if sy-subrc  0.
            f3 = 'X'.
        else.
           //write contents of table 3!!
        endif.
    else
         //Leave space for blank table 3 entries!!!
     endif.
counter = counter + 1.
endwhile.

Regards,

Bala.

Read only

Former Member
0 Likes
2,910

Hi,

define another variable in each internal table (like INDEX).

for each internal table loop pass sy-tabix to index.

now, make final internal table (with entries of all the 3 tables) and SORT the final table with index ascending.

APPEND liens of itab1 to itab.

APPEND liens of itab2 to itab.

APPEND liens of itab3 to itab.

sort itab by index.

LOOP AT itab.

write : itab-...,

itab-....

ENDLOOP.

Hope it works for ur req...

Let me know if you need any further help!!

Regards,

Pavan

Edited by: vishnu Pavan on Jan 6, 2009 8:17 AM

Read only

Former Member
0 Likes
2,910

Hi Nilesh can you tell me whether the number of records in all three tables is same?

then u can just get the number of record for any of the table with DESCRIBE stnt and using a DO-ENDDO loop simply read the table using READ TABLE stmt and pass the loop index.

like follows

data: lv_lin type i, lv_index type i.

DESCRIBE itab1 lines lv_lin.

do lv_lins times.

lv_index = sy-index.

read table itab1 index lv_index.

read table itab2 index lv_index.

read table itab3 index lv_index.

enddo.

if the number of record are dissimilar then get the no of records for each of the itab and use greatest value for the DO loop.

Read only

Former Member
0 Likes
2,910

Hi,

I think the following prg will help u.

tables: spfli, sflight.

data: it_spfli type STANDARD TABLE OF spfli,

wa_spfli like LINE OF it_spfli,

it_sflight TYPE STANDARD TABLE OF sflight,

wa_sflight LIKE LINE OF it_sflight.

types: BEGIN OF x_result,

carrid type spfli-carrid,

connid TYPE sflight-connid,

END OF x_result.

DATA: it_result TYPE TABLE OF x_result,

wa_result LIKE LINE OF it_result.

DATA: count TYPE i,

n_count TYPE i.

START-OF-SELECTION.

select carrid connid from spfli INTO CORRESPONDING FIELDS OF TABLE

it_spfli .

SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE

it_sflight.

LOOP AT it_spfli INTO wa_spfli .

count = count + 1.

ENDLOOP.

LOOP AT it_sflight INTO wa_sflight .

count = count + 1.

ENDLOOP.

n_count = 0.

DO count TIMES.

n_count = n_count + 1.

READ TABLE it_spfli INTO wa_spfli INDEX n_count.

if sy-subrc eq 0.

move: wa_spfli-carrid to wa_result-carrid,

wa_spfli-connid to wa_result-connid.

append wa_result to it_result.

ENDIF.

read TABLE it_sflight INTO wa_sflight INDEX n_count.

if sy-subrc eq 0.

MOVE: wa_result-carrid to wa_result-carrid,

wa_sflight-connid to wa_result-connid.

append wa_result to it_result.

endif.

enddo.

loop at it_result INTO wa_result.

write:/ wa_result-carrid, wa_result-connid.

ENDLOOP.

u can understand it and modify the requirement as per urs.

Give points if it is helpful.

Thanks.

Read only

Former Member
0 Likes
2,910

hi.

if u have same no of rows in all three tables and dnt want to append all three internal tables rows in another table then just do like that.

loop at itab1 into wa_itab1.

do ur work.....

loop at itab2 into wa_itab2.

do ur work....

loop at itab3 into wa_itab3.

do ur work.....

endloop.

endloop.

endloop.

Read only

Former Member
0 Likes
2,910

sorry for the copies

Edited by: kartik tarla on Jan 6, 2009 2:23 PM

Read only

Former Member
0 Likes
2,910
loop at itab1 into wa_itab1.
do ur work.....
clear wa_itab1.
loop at itab2 into wa_itab2.
do ur work....
clear wa_itab2.
loop at itab3 into wa_itab3.
do ur work.....
clear wa_itab3
endloop.
endloop.
endloop.
Read only

Former Member
0 Likes
2,910

plzzz reply back....

thanx.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,910

Hello Nilesh,

Correct me if i am wrong in understanding ur requirement:

You have 3 tables: IT1, IT2 & IT3. You want the output to be something like:

IT1-LINE1

IT2-LINE1

IT3-LINE1

IT1-LINE2

IT2-LINE2

IT3-LINE2

IT1-LINE3

IT2-LINE3

IT3-LINE3

.............. and so on !!!

Try this code:


LOOP AT IT1 INTO WA1.
V_INDEX = SY-INDEX.

" Display IT1 details here

READ TABLE IT2 INTO WA2 INDEX V_INDEX.
IF SY-SUBRC = 0.
" Display IT2 details here
ENDIF.

READ TABLE IT3 INTO WA3 INDEX V_INDEX.
IF SY-SUBRC = 0.
" Display IT3 details here
ENDIF.

ENDLOOP.

I think it should be fine, if your requirement is as mentioned above.

BR,

Suhas

Edited by: Suhas Saha on Jan 9, 2009 7:22 AM

Read only

Former Member
0 Likes
2,910

u r right thats my requirement but for that i need to change the code instead of that just give me the prob of that perticular solution i.e. sy-index.

thanx....

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,910

Hello Nilesh,

I dint get you, whats the issue with sy-index. Sorry, please see my modified code :-((

You got to use sy-tabix. Silly mistake on my part.


LOOP AT IT1 INTO WA1.

V_INDEX = SY-TABIX. "V_INDEX = SY-INDEX.
 
" Display IT1 details here
 
READ TABLE IT2 INTO WA2 INDEX V_INDEX.
IF SY-SUBRC = 0.
" Display IT2 details here
ENDIF.
 
READ TABLE IT3 INTO WA3 INDEX V_INDEX.
IF SY-SUBRC = 0.
" Display IT3 details here
ENDIF.
 
ENDLOOP.

Plz test & let me know.

BR,

Suhas

Edited by: Suhas Saha on Jan 9, 2009 7:39 AM

Read only

Former Member
0 Likes
2,910

actually its like ,

it1 (rec1)

it2 (rec1)

it3 (rec1)

it4 (rec1)

it1 (rec2) (here if it1 dont have second record the it should again display rec1 & rest is same)

it2 (rec2)

it3 (rec2)

it4 (rec2)

it1 (rec3)

................... so on..

Edited by: NILESH S. on Jan 9, 2009 8:04 AM

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,910

Hello Nilesh,

Is the code working for 3 tables? You can extend it to as many tables as you want by adding additional READ TABLE stmt for the table.

As per your current requirement we need to change the code:


DO.

V_INDEX =  V_INDEX + 1.

"V_INDEX = SY-TABIX. "V_INDEX = SY-INDEX.

READ TABLE IT1 INTO WA1 INDEX V_INDEX.
IF SY-SUBRC = 0.
" Display IT1 details here
ELSE.
 L_V_LAST1 = V_INDEX - 1.

" Check other tables have not exhausted
IF L_V_LAST2 = 0 OR L_V_LAST3 = 0.
 READ TABLE IT1 INTO WA1 INDEX L_V_LAST1.
 IF SY-SUBRC = 0.
"  Display last line of IT1
 ENDIF.
ENDIF.
ENDIF.
 
READ TABLE IT2 INTO WA2 INDEX V_INDEX.
IF SY-SUBRC = 0.
" Display IT2 details here
ELSE.
 L_V_LAST2 = V_INDEX - 1.
" Check other tables have not exhausted
IF L_V_LAST3 = 0 OR L_V_LAST1 = 0.
 READ TABLE IT2 INTO WA2 INDEX L_V_LAST2.
 IF SY-SUBRC = 0.
"  Display last line of IT2
 ENDIF.
ENDIF.
ENDIF.
 
READ TABLE IT3 INTO WA3 INDEX V_INDEX.
IF SY-SUBRC = 0.
" Display IT3 details here
ELSE.
 L_V_LAST3 = V_INDEX - 1.
" Check other tables have not exhausted
IF L_V_LAST1 = 0 OR L_V_LAST2 = 0.
 READ TABLE IT3 INTO WA3 INDEX L_V_LAST3.
 IF SY-SUBRC = 0.
"  Display last line of IT3
 ENDIF.
ENDIF.
ENDIF.

" Check all tables have exhausted
IF L_V_LAST1 NE 0 AND 
L_V_LAST2 NE 0 AND 
L_V_LAST3 NE 0.
  EXIT.
ENDIF.
 
"ENDLOOP.
ENDDO.

Try this for IT4 & let us know.

BR,

Suhas

Edited by: Suhas Saha on Jan 9, 2009 8:23 AM