Skip to Content
author's profile photo Former Member
Former Member

fetching Data from More than 2 tables

Dear All,

I wanted to know, how can I write a Simple ABAP Program, where I have to fetch data from more than 2 tables. I know joining 2 tables but want should I do if I have to fetch data from 3 or more tables.

Any example would be really great.

Thanking you.

Regards

Venkat.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Oct 24, 2006 at 08:04 PM

    Hi,

    Check this example for more than 3 tables...

    DATA: BEGIN OF ITAB OCCURS 0,

    MATNR TYPE MATNR,

    WERKS TYPE WERKS_D,

    VKORG TYPE VKORG,

    VTWEG TYPE VTWEG,

    BWKEY TYPE BWKEY,

    END OF ITAB.

    PARAMETERS: P_MATNR TYPE MATNR.

    SELECT AMATNR BWERKS C~VKORG

    CVTWEG DBWKEY

    INTO TABLE ITAB

    FROM MARA AS A

    INNER JOIN MARC AS B

    ON AMATNR = BMATNR

    INNER JOIN MVKE AS C

    ON AMATNR = CMATNR

    INNER JOIN MBEW AS D

    ON AMATNR = DMATNR

    WHERE A~MATNR = P_MATNR.

    CHECK SY-SUBRC = 0.

    Thanks,

    Naren

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      It is not advisable to join more than three tables but it completely depends on your join and the join conditions..if u are using all primary keys for join etc than it is worthwhile but if not than it is better to use For all enteries in the another select statement.

  • author's profile photo Former Member
    Former Member
    Posted on Oct 24, 2006 at 05:09 PM

    Hi,

    Check this example...

    DATA: BEGIN OF ITAB OCCURS 0,

    MATNR TYPE MATNR,

    WERKS TYPE WERKS_D,

    VKORG TYPE VKORG,

    VTWEG TYPE VTWEG,

    END OF ITAB.

    PARAMETERS: P_MATNR TYPE MATNR.

    SELECT AMATNR BWERKS C~VKORG

    C~VTWEG

    INTO TABLE ITAB

    FROM MARA AS A INNER JOIN MARC AS B

    ON AMATNR = BMATNR

    INNER JOIN MVKE AS C

    ON AMATNR = CMATNR

    WHERE A~MATNR = P_MATNR.

    CHECK SY-SUBRC = 0.

    Thanks,

    Naren

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 24, 2006 at 05:14 PM

    Hi venkat,

    This is one select query which I worte it today so sending you.

    Here I am fetching data from more than 3 tables.

    <b>SELECT STSTLNR STIDNRK STPOSNR STMENGE ST~MEINS

    MK~MAKTX

    AK~AUFNR

    INTO CORRESPONDING FIELDS OF TABLE IT_STPO

    FROM STPO AS ST INNER JOIN MAKT AS MK

    ON STIDNRK = MKMATNR

    INNER JOIN AFKO AS AK

    ON STSTLNR = AKSTLNR

    FOR ALL ENTRIES IN IT_AFRU

    WHERE ST~STLNR = IT_AFRU-STLNR AND

    AK~AUFNR = IT_AFRU-AUFNR AND

    SPRAS = 'E'.</b>

    Let me know if you have any queries.

    Vinod.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Naren,

      This is an example for fetching data from 3 tables.

      Here I am getting data from

      MKPF, MSEG, AFPO, MAKT, and AFRU

      SELECT MKMBLNR MKBUDAT

      MSAUFNR MSBWART MSERFMG MSERFME

      AF~MATNR

      MD~MAKTX

      INTO CORRESPONDING FIELDS OF TABLE IT_MKPF_FIR

      FROM MKPF AS MK INNER JOIN MSEG AS MS

      ON MKMBLNR = MSMBLNR

      INNER JOIN AFPO AS AF

      ON MSAUFNR = AFAUFNR AND

      AFMATNR = MSMATNR

      INNER JOIN MAKT AS MD

      ON AFMATNR = MDMATNR

      FOR ALL ENTRIES IN IT_AFRU

      WHERE MK~BUDAT = IT_AFRU-BUDAT AND

      MS~AUFNR = IT_AFRU-AUFNR AND

      MD~SPRAS = 'E'.

      Vinod.

      <b>Allot points for Valid answers</b>

  • author's profile photo Former Member
    Former Member
    Posted on Oct 25, 2006 at 10:19 AM

    SELECT MSEGMATNR MSEGWERKS MSEGMBLNR MSEGZEILE MSEG~EBELN

    MSEGEBELP MSEGMENGE MSEGBWART MSEGEBELN MSEG~ERFME

    MSEGKOSTL MSEGLGORT MSEGMJAHR MSEGSHKZG EKKO~EKGRP

    EKKOLIFNR EKPOMATKL MKPFBUDAT MKPFUSNAM

    FROM ( MSEG

    INNER JOIN EKKO

    ON MSEGEBELN = EKKOEBELN

    INNER JOIN EKPO

    ON MSEGEBELN = EKPOEBELN

    AND MSEGEBELP = EKPOEBELP

    INNER JOIN MKPF

    ON MSEGMBLNR = MKPFMBLNR

    AND MSEGMJAHR = MKPFMJAHR )

    APPENDING CORRESPONDING FIELDS OF TABLE IT_YMM_ST_TNUOT_MLY

    WHERE MSEG~MBLNR IN SL_MBLNR

    AND MSEG~MATNR IN SL_MATNR

    AND MSEG~WERKS IN SL_WERKS

    AND MSEG~LGORT IN SL_LGORT

    AND MSEG~LIFNR IN SL_LIFNR

    AND MSEG~KOSTL IN SL_KOSTL

    AND EKKO~EKGRP IN SL_EKGRP

    AND EKPO~MATKL IN SL_MATKL

    AND MKPF~BUDAT IN SL_BUDAT .

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 25, 2006 at 09:17 AM

    Hi Venkat,

    U can join multiple tables no prob but performance will be a major issue ...so joining upto 2 tables is ok but for more than 2 u may experience problem. Retrieve all the records by joining two tables in one internal table then using that table as a driver table try to select rest of the records from other tables .

    If it sounds good pl reward.

    Cheers.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 25, 2006 at 09:43 AM

    Hi,

    You can join multiple tables no prob but performance will be a major issue ...so joining upto 3 tables is ok but for more than 3 you may experience problem. Retrieve all the records by joining three tables in one internal table then using that internal table in the command Select for all entries try to get the records from other table.

    Regadrs,

    Govindarajan Umadevi.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.