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: 

how to display data from two db table

Former Member
0 Kudos
163

How to display multiple records from two internal table without using nesting of loop

13 REPLIES 13

Former Member
0 Kudos
135

Hi,

use joins,to get data from different tables.

SELECT vbak~vbeln

vbak~vkorg

vbap~posnr

vbap~matnr

INTO TABLE t_vbak

FROM vbak AS vbak

INNER JOIN vbap AS vbap

ON vbakvbeln = vbapvbeln

WHERE vbak~auart = p_auart.

Regards,

jaya

Edited by: Jayapradha Neeli on Jun 9, 2009 2:13 PM

0 Kudos
135

hi jaya,

you are not getting my point actually i want to show rrecord from two internal tables without using nesting of loop.

is it possible but how?

0 Kudos
135

Hi Alok,

Plz do as following-

Data tabix like sy-tabix.

Sort itab1 by field1.

Sort itab2 by field1.

Transfer the contents of table itab1 to corresponding field of table itab2 according to their key fields.

then use the write statement.

Loop at itab2.

tabix = sy-tabix.

read table itab1 with key field1 = itab2-field1 binary search.

if sy-subrc = 0.

*move the fields from itab1 to itab2.

move-corresponding fields of table itab1 to itab2. " (field1 to field10 belongs to itab1)

endif.

modify itab2 index tabix.

write 😕 itab2-field1, itab2-field2, ........ itab2-field10, itab2-field11, ................ itab20-field.

clear: itab1, itab2.

endloop.

or u can also use third table itab3 which has the fields from both the tables ie. itab1, itab2

Edited by: madan namdeo on Jun 9, 2009 11:26 AM

Former Member
0 Kudos
135

Try to take data(split 2 into 3) in another internal table in such a way that one LOOP statement followed by subsequent READ statements work .

Regards,

Deepthi

0 Kudos
135

ok i agree from u. but when u would show multiple records from two internal tables then how will u display without using nested loop.

Former Member
0 Kudos
135

Hi alok srivastava,

As per the requirements and from what tables we are fetching data will make us to decide whether can be done with out Nesting Loops or Not.

So, try to breif your issue for better solutions.

Regards,

Suneel G

former_member222860
Active Contributor
0 Kudos
135

By using LOOP and READ statements

An ex; here:

loop at tb_STAB.
      read table tb_SOSTAB  WITH KEY ebeln = tb_STAB-ebeln.
      if sy-subrc eq 0.
        write :/2 tB_STAB-ebeln color 4,
                20 lv_msg2 color 5,
                70 tb_SOSTAB-vbeln color 4,
                100 lv_msg3  color 5.
      endif.
    endloop.

Pl. search, you'll find more results here

Former Member
0 Kudos
135

If u dont want to use nested loop, u can do it via READ statement.

Foe example.

Loop at itab1.

write the content of itab1.

read table itab2 with key field1 = itab1-field1.

if sy-subrc = 0.

write the content of itab2.

endloop.

Edited by: Sheelesh on Jun 9, 2009 2:19 PM

0 Kudos
135

but we use read statement for only one record if we want to show multiple records from second table

then what should i do.

0 Kudos
135

better to use for all entries instead of inner joins to add two more tables.

for example.

select <field names> into table <itab1> from <databasetable 1>

where<condition>.

if itab[] is not initial.

select <field names> into table <itab2> from <databasetable 2>

for all entries in <itab1>

where <condition>.

finally need to display in one table.

loo at itab1.

read table <itab2> wiht key <condition>

itab1-fieldnames = itab2-fieldnames

modify itab1 transporting <new fields>

endloop.

0 Kudos
135

If ur second internal table has multiple records then you have to process these records in loop because in a single process you can write multiple records of second table.

You can use do enddo statement here to write the multiple records or process them via using nested loop.

loop at itab1

write records from itab1.

do

read table itab2 with key field1 = itab1-field1.

if sy-subrc = 0.

write ur records.

else.

exit.

endif.

enddo.

endloop.

Former Member
0 Kudos
135

Hi ,

Suppose you want to select records from the SPFLI and SBOOK .To do that - -

1 ) In Join condition select the records from the booth table like - -

PARAMETERS : p_carrid TYPE spfli-carrid.  " parameter that will be used in WHERE condition
DATA : BEGIN OF fs_itab,
         mandt TYPE spfli-mandt,
         carrid TYPE spfli-carrid,
         connid TYPE spfli-connid,
         countryfr TYPE spfli-countryfr,
         customid  TYPE sbook-customid,
      END OF fs_itab,
      t_itab LIKE TABLE OF fs_itab .  " Table to hold the data

SELECT
           spfli~mandt  " Fields from SPFLI
           spfli~carrid
           spfli~connid
           spfli~countryfr
           sbook~customid   " Fields from SBOOK

           FROM spfli INNER JOIN sbook  " Inner join of the tables
           ON spfli~carrid = sbook~carrid AND  " Join condition according to ur requirement
              spfli~connid = sbook~connid         " Join condition
              INTO TABLE t_itab
              WHERE spfli~carrid = p_carrid .  " Where condition

According to your requirement you can use the OUTER JOIN also.

OR ELSE FOLLOW the 2nd -


2) Create a view with the required fields of both the table .

Then use the view in the select .

Regards

Pinaki

Former Member
0 Kudos
135

good