My requirement is to populate an itab by retrieving data from three diff db tables,
ekko,ekpo and Ekbe.
below is the code for data retrieval .
SELECT EBELN INTO TABLE IT_EKKO FROM EKKO WHERE EBELN IN S_EBELN.
IF NOT IT_EKKO IS INITIAL.
SELECT EBELP TXZ01 NETWR BUKRS INTO CORRESPONDING FIELDS OF TABLE IT_EKPO FROM EKPO FOR ALL ENTRIES IN IT_EKKO
WHERE EBELN EQ IT_EKKO-EBELN.
IF NOT IT_EKPO IS INITIAL.
SELECT EMATN WAERS WERKS GJAHR BEWTP INTO CORRESPONDING FIELDS OF TABLE IT_EKBE FROM EKBE FOR ALL ENTRIES IN IT_EKPO
WHERE EBELN EQ IT_EKPO-EBELN
AND EBELP EQ IT_EKPO-EBELP
AND BEWTP EQ 'E' OR BEWTP EQ 'Q'.
please tell me how to populate data from it_ekko,it_ekpo and it_ekbe into an internal table ITAB.
IF IT_EKBE IS NOT INITIAL. SORT IT_EKBE BY EBELN EBELP. SORT IT_EKPO BY EBELN EBELP. SORT IT_EKKO BY EBELN. LOOP AT IT_EKBE INTO WA_EKBE. " WA_FINAL = WA_EKBE - ADD THE RELEVANT FIELDS IN WA_FINAL WORKAREA. READ TABLE IT_EKPO INTO WA_EKPO WITH KEY EBELN = WA_EKBE-EBELN EBELP = WA_EKBE-EBELP BINARY SEARCH. IF SY-SUBRC = 0. " WA_FINAL = WA_EKPO - ADD THE RELEVANT FIELDS IN WA_FINAL WORKAREA. READ TABLE IT_EKKO INTO WA_EKKO WITH KEY EBELN = WA_EKPO-EBELN BINARY SEARCH. IF SY-SUBRC = 0. " WA_FINAL = WA_EKKO - ADD THE RELEVANT FIELDS IN WA_FINAL WORKAREA. APPEND WA_FINAL TO IT_FINAL. ENDIF. ENDIF. CLEAR WA_FINAL. CLEAR WA_EKKO. CLEAR WA_EKPO. ENDLOOP. ENDIF.
You take one final structure & final internal table, & within that structure take all the fields which is required to be displayed as a final output.
for data fetching from different table, you take three different structures & three internal table for that, because into corresponding fields of table is not good for performance wise. Use into table syntax.
follow the below example:(similarly)
sort it_klah by key fields.
LOOP AT IT_KSSK INTO WA_KSSK.
READ TABLE IT_KLAH INTO WA_KLAH WITH KEY CLINT = WA_KSSK-CLINT
KLART = WA_KSSK-KLART.
IF SY-SUBRC EQ 0.
MOVE: WA_KLAH-OBJEK to WA_final-OBJEK.
Read another table........(2nd one)
IF SY-SUBRC EQ 0.
APPEND WA_final TO IT_final.
finally display it_final records.
Hope this can solve your problems.
The best way is to either go with iner joining the 3 tables..
1) As you are just retrieving the ebeln from ekko rather than 2 diffrent selects go with inner join of ekko and ekpo on ebeln.
2) slect ebeln also from ekbe.
3)loop at the first table and read the it_ekbe with key ebeln.
fill the data into the final table...
dont use move corresponding fields directly use the field reference...
Edited by: imran khan on Oct 23, 2009 8:55 AM
Even though you already got an answer, I will point you to this code sample, showing how to join three database tables in one query.