‎2007 Jan 21 9:43 AM
I've 2 tables .... table #1 have 4 fields out of which 2 fields only present in the table #2 ...... can I output the data of all the fields that are in both the tables.
thanks in advace.
‎2007 Jan 21 10:08 AM
Hi Shaheen,
Are the two fields in the table 1 and 2 are Key fields?? What are the key fields present in both the tables ? If they are key fields we can retrive the data from the two tables...
Srini
‎2007 Jan 21 10:17 AM
‎2007 Jan 21 10:18 AM
Try using Joining the tables by Inner Join to retrieve the data
‎2007 Jan 21 10:18 AM
‎2007 Jan 21 10:26 AM
if there are no common values to compare between table 1 and table 2 how do you want to combine records? may be first record in tab1 with first rec in tab 2?
‎2007 Jan 21 10:39 AM
no what i ment is that there are common fields between the 2 but not all of the are the key fields.
‎2007 Jan 21 10:26 AM
check this example... combination of fields from two tables....
SELECT PCARRID PCONNID F~FLDATE
INTO TABLE ITAB
FROM SPFLI AS P
INNER JOIN SFLIGHT AS F ON PCARRID = FCARRID AND
PCONNID = FCONNID
<b>WHERE P~CITYFROM = 'FRANKFURT' AND
P~CITYTO = 'NEW YORK'</b>.
in the join give the link between two tables.
regds,
kiran
‎2007 Jan 21 10:42 AM
you give the fields which are common in the two tables in the condition inner join,
as my above example.
in that example carrid and connid are common in both the tables sflight and spfli.
regds,
kiran
‎2007 Jan 21 10:52 AM
‎2007 Jan 21 10:59 AM
declare all the required fields into one internal table.
assume first table as spfli and second table as sflight
write the code in this format.
SELECT PCARRID PCONNID F~FLDATE
INTO TABLE ITAB
FROM SPFLI AS P
INNER JOIN SFLIGHT AS F ON PCARRID = FCARRID AND
PCONNID = FCONNID
WHERE P~CITYFROM = 'FRANKFURT' AND
P~CITYTO = 'NEW YORK'.
1. the pcarrid pconnid f~fldate are the fields which are required from two tables
2. inner join sflight as f on pcarrid on = fcarrid and pconnid = fconnid.
is the condition between two tables.
if you are not yet clear , give me the fields and table names which you want to query.
regds,
kiran
‎2007 Jan 21 11:10 AM
the inner join Kiran is referring is for database tables.
for itabs
let us assume you have
tab1 with 4 fields
tab 2 with 2 fields
field 1 in tab1 and field 1 in tab2 are the connecting values.
and you wan to move them to new itab
loop at itab1.
read table itab2 with key field1 = itab1-field1
if sy-subrc eq 0 .
move: fileds from itab1 and itab2 to new itab.
append new itab.
endif .
endloop .
REgards
Raja
‎2007 Jan 21 11:20 AM
‎2007 Jan 21 11:26 AM
these are database tables.
i do not have access to SAP system right now. if possible can u give the fields of both tables, so that i can make a query.
otherwise do like this....
data itab1 like table of mlgt with header line.
data itab2 like table of mlgn with header line.
(delcare itab3 with all the fields you required).
select * from mlgt into table itab1.
select * from mlgn into table itab2
loop at itab1.
read table itab2 with <col> = itab1-<col>
<col> = itab1-<col>
if sy-subrc eq 0.
move all itab1 and itab2 to itab3(final table).
append itab3.
endif.
endloop.
loop at itab3.
write : / itab3-............
endloop.
in the above example change <col> with the existing columns in two tables.
regds,
kiran
‎2007 Jan 21 11:43 AM
Hi Shaheen,
You can use inner join like this: This is only example you need to use according to your main table & details table.
SELECT MLGNMATNR MLGNLGNUM
INTO TABLE ITAB
FROM MLGN
INNER JOIN MLGT ON MLGNMATNR = MLGTMATNR AND
MLGNLGNUM = MLGTLGNUM
WHERE MLGN~LGNUM= '1000'.
For more information on INNER JOINS I like to tell to you that in real time scenario generally dont use INNER JOINS because of Database performance. Instead of this SELECT FOR ALL ENTRIES is preferable.
Ashven
‎2007 Jan 22 4:09 AM
‎2007 Jan 22 4:16 AM
Hi,
Try this query,
DATA: it_tab type table of mlgn.
SELECT MLGNMATNR MLGNLGNUM
FROM MLGN
INNER JOIN MLGT ON MLGNMATNR = MLGTMATNR AND
MLGNLGNUM = MLGTLGNUM INTO CORRESPONDING FIELDS OF IT_TAB
WHERE MLGN~LGNUM= '1000'.
Regards,
Sesh
‎2007 Jan 22 4:26 AM
‎2007 Jan 22 4:44 AM
‎2007 Jan 22 5:23 AM
Data: begin of it_mlgn occurs 0,
matnr like mlgn-matnr,
lgnum like mlgn-lgnum,
lgbkz like mlgn-lgbkz,
end of it_mlgn.
data: begin of it_mlgt occurs 0,
matnr like mlgt-matnr,
lgnum like mlgt-lgnum,
lgpla like mlgt-lgpla,
end of it_lgpla.
select matnr lgnum gbkz from mlgn into table it_mlgn
where ...
if not it_mlgn[] is initial.
sort it_mlgn by matnr lgnum.
select matnr lgnum lgpla from mlgt from mlgt
into table it_mlgt
for all entries in it_mlgn
where matnr = it_mlgn-matnr
lgnum = it_mlgn-lgnum.
endif.
‎2007 Jan 22 5:33 AM
once you get the data from MLGN into one internal table(itab1).
then you can use for all entries in this way
<b>select * from MLGT into table itab2
for all entries in itab1
where matnr = itab1-matnr and
lgnum = itab1-lgnum.</b>
once you get the data into itab1 and itab2, then
loop at itab1.
read table itab2 with matnr = itab1-matnr
lgnum = itab1-lgnum.
if sy-subrc eq 0.
(here you write all itab1 and itab2 fields)
endif.
endloop.
the one in the bold letters is the code for all entries.
regds,
kiran
‎2007 Jan 22 5:59 AM
read table itab2 with matnr = itab1-matnr
lgnum = itab1-lgnum.
gives an error unable to interpert "MATNR".
‎2007 Jan 22 6:02 AM
i gave you the code yesterday itself
check by previous posting.
read table itab2 with <b>key</b> field1 = itab1-field1
you are missing 'KEY'
‎2007 Jan 22 6:11 AM
read table it_mlgt with key matnr = it_mlgt-matnr and lgnum = it_mlgt-lgnum.
gives an error as well.
‎2007 Jan 22 6:24 AM
read table it_mlgt with key matnr = itab1-matnr
lgnum = itab1-lgnum.
here itab1-matnr & itab1-lgnum, should be outer loop fields i.e,
loop at itab1.
read table it_mlgt with key matnr = itab1-matnr
lgnum = itab1-lgnum.
endloop.
regds,
kiran
‎2007 Jan 22 6:25 AM
read table it_mlgt with key matnr = it_mlgt-matnr and lgnum = it_mlgt-lgnum.
remove the and
it should be
read table it_mlgt with key matnr = it_mlgt-matnr lgnum = it_mlgt-lgnum.
Raja
Assign points for helpful answers