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: 

Regarding select statements

Former Member
0 Kudos
87

Hello experts,

Can we fetch data from two different database tables into one one internal table.

regards,

raman

4 REPLIES 4

Former Member
0 Kudos
46

Hi Prakash,

<b>Yes, You can.</b>

Declare the internal table with the fields required from data base tables and get the data into interntal table using SELECT statement with JOINS/FOR ALL ENTRIES.

DATA:

BEGIN OF ITAB OCCURS 0,

MATNR TYPE MARA-MATNR,

WERKS TYPE MARC-WERKS,

END OF ITAB.

SELECT AMATNR BWERKS

FROM MARA AS A

INNER JOIN MARC AS B

INTO TABLE ITAB

ON AMATNR = BMATNR.

Thanks,

Vinay

Former Member
0 Kudos
46

Hello,

Check this.


data: begin of itab occurs 0,
         matnr like mara-matnr,
         maktx like makt-maktx,
        end of itab.
select a~matnr b~maktx into table itab from mara as a inner join makt as b on a~matnr = b~matnr where b~spras = sy-langu.

Vasanth

Former Member
0 Kudos
46

Hi,

Yes.

You can use SELECT statement with an inner join.

Please check this link for sample code.

http://www.sap-img.com/abap/inner-joins.htm

SELECT afield1 afield2

bfield3 cfield4 INTO TABLE itab

FROM dbtab1 AS a INNER JOIN

dbtab2 AS b ON bcodeno = acodeno

INNER JOIN

dbtab3 AS c ON ccodeno = bcodeno

WHERE <whereclause>.

Regards,

Priyanka.

Former Member
0 Kudos
46

<b>Below is the example of 3 table into one internal table .... using Joins</b>

REPORT  zdemo_alvgrid                 .

TABLES:   mara , mard , makt .

type-pools: slis.                                 "ALV Declarations
*Data Declaration
*----------------
TYPES: BEGIN OF t_Mara,
matnr   like   mara-matnr  ,
meins   like  mara-meins,
werks   like mard-werks  ,
lgort   like mard-lgort ,
labst   like mard-labst ,
maktx  like makt-maktx ,
 END OF t_Mara.

DATA: it_Mara TYPE STANDARD TABLE OF t_Mara INITIAL SIZE 0,
      wa_Mara TYPE t_Mara.

select-options  : p_mat  for  mara-matnr   ,
 p_werks  for   mard-werks ,
 p_lgort  for  mard-lgort.
************************************************************************
*Start-of-selection.
START-OF-SELECTION.

perform data_retrieval.


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_Mara
*----------------------------------------------------------------------*
form data_retrieval.
select mara~matnr mara~meins  mard~werks mard~lgort  mard~labst  makt~maktx  into  corresponding fields of  table  it_Mara
  from ( ( mara  join  mard  on   mard~matnr  =  mara~matnr   ) join
       makt  on   makt~matnr  =  mard~matnr )  where  mara~matnr in  p_mat
       and  mard~werks  in  p_werks
       and  mard~lgort  in p_lgort  .

loop  at  it_Mara  into  wa_Mara .

write : /  wa_Mara-matnr  ,  wa_Mara-maktx ,  wa_Mara-lgort  ,  wa_Mara-labst ,  wa_Mara-meins .

endloop .


endform.                    " DATA_RETRIEVAL

reward points if it is usefull ....

Girish