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: 

fetch data from 2 table with same field

former_member844813
Participant
0 Kudos

Hello experts!,

i have this sample program to fetch data from 2 table but with different fields taken, is there a way to make it fetch data from 2 table with the same field and then store the output to my Ztable ?

here's the sample program

REPORT ZGETFBL.<br><br>TABLES : MARA.<br><br>DATA : ITAB TYPE TABLE OF ZDATAGL,<br>       WA TYPE ZDATAGL.<br><br>SELECT-OPTIONS : S_MATNR FOR MARA-MATNR.<br><br>START-OF-SELECTION.<br><br>PERFORM GET_DATA.<br>PERFORM DISPLAY_DATA.<br>*&---------------------------------------------------------------------*<br>*&      Form  GET-DATA<br>*&---------------------------------------------------------------------*<br>*       text<br>*----------------------------------------------------------------------*<br>*  -->  p1        text<br>*  <--  p2        text<br>*----------------------------------------------------------------------*<br>FORM GET_DATA.<br><br>SELECT A~MATNR<br>       ERSDA<br>       ERNAM<br>       B~PSTAT<br>       B~LVORM <br>INTO TABLE ITAB<br>FROM MARA AS A<br>  INNER JOIN MARC AS B<br>ON B~MATNR = A~MATNR<br>  WHERE MATNR IN S_MATNR.<br><br>ENDSELECT.<br>ENDFORM.
1 ACCEPTED SOLUTION

FredericGirod
Active Contributor

Yes, you could fetch the "same" fieldname from two different tables (or same table), you just have to rename the fields : (better is to give the same name of the internal table, but your code is hard to read)

SELECT mara~matnr as my_first_material, makt~matnr as my_second_material, .... 
11 REPLIES 11

TarunTakshak
Participant

Hi

Please refer below code to fetch data from two different tables

TYPES :
    "structure to hold customer details
        BEGIN OF st_cust,
        kunnr TYPE kna1-kunnr,
        land1 TYPE kna1-land1,
        vbeln TYPE vbak-vbeln,
        erdat TYPE vbak-erdat,
        END OF st_cust.

DATA :
    "internal table and workarea to hold customer details
       wa_cust TYPE st_cust,
       it_cust TYPE TABLE of st_cust.

 SElect  kna1~kunnr
         kna1~land1
         vbak~vbeln
         vbak~erdat
 INTO TABLE it_cust
 from kna1
INNER JOIN vbak
   on kna1~kunnr = VBAK~kunnr.

   IF sy-subrc = 0 and it_cust is NOT INITIAL.

  cl_demo_output=>display( it_cust ).

   ENDIF.<br>

Thanks

Tarun

0 Kudos

Hi Tarun

are those 2 table have the same field ?, if not what should i do it if i want to fetch same field from 2 different table ?

Thanks

Dema

FredericGirod
Active Contributor

Yes, you could fetch the "same" fieldname from two different tables (or same table), you just have to rename the fields : (better is to give the same name of the internal table, but your code is hard to read)

SELECT mara~matnr as my_first_material, makt~matnr as my_second_material, .... 

0 Kudos

this is the sample code i found, but this one fetching different field with 2 different table

REPORT ZGETFBL.
TABLES : MARA.
DATA : ITAB TYPE TABLE OF ZDATAGL,
WA TYPE ZDATAGL.
SELECT-OPTIONS : S_MATNR FOR MARA-MATNR.
START-OF-SELECTION.
PERFORM GET_DATA.
PERFORM DISPLAY_DATA.
*&---------------------------------------------------------------------*
*& Form GET-DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_DATA.
SELECT A~MATNR
ERSDA
ERNAM
B~PSTAT
B~LVORM
INTO TABLE ITAB
FROM MARA AS A
INNER JOIN MARC AS B
ON B~MATNR = A~MATNR
WHERE MATNR IN S_MATNR.
ENDSELECT.
ENDFORM.

0 Kudos

this is a JOIN, so my proposal is a good one SELECT A~MATNR AS ..., B~MATNR AS ....

0 Kudos

as u asked before, yes i want it in one column

im gonna make a table to store the data from that 2 table.

FredericGirod
Active Contributor
0 Kudos

don't use answer to reply to a proposal, use comment

You want the two fields in the same column or in two differnt colums ?

former_member844813
Participant
0 Kudos

in the same column, yes

here's how i want it to work:

1st i use parameter to search data according to G/L account number, with a date range of it

and then its supposed to show the data from the 2 different table with the same field which is G/L account number and the date range.

Sandra_Rossi
Active Contributor
0 Kudos

Please review the formatting of the code in your question, it's currently shown as only ONE line with many <br>. Possibly, pasting code using Ctrl+Shift+V may help you (unformatted paste).

Sandra_Rossi
Active Contributor
0 Kudos

Your code, correctly formatted:

REPORT ZGETFBL.

TABLES : MARA.

DATA : ITAB TYPE TABLE OF ZDATAGL,
       WA TYPE ZDATAGL.

SELECT-OPTIONS : S_MATNR FOR MARA-MATNR.

START-OF-SELECTION.

PERFORM GET_DATA.
PERFORM DISPLAY_DATA.
*&---------------------------------------------------------------------*
*&      Form  GET-DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA.

SELECT A~MATNR
       ERSDA
       ERNAM
       B~PSTAT
       B~LVORM 
INTO TABLE ITAB
FROM MARA AS A
  INNER JOIN MARC AS B
ON B~MATNR = A~MATNR
  WHERE MATNR IN S_MATNR.

ENDSELECT.
ENDFORM.

Sandra_Rossi
Active Contributor
0 Kudos

I'm not sure people understand precisely what you are asking.