Sei sulla pagina 1di 6

7/2/2014 how to use inner join and for all entries | SCN

http://scn.sap.com/thread/416290 1/6
Getting Started Newsletters Store

Products Services & Support About SCN Downloads
Industries Training & Education Partnership Developer Center
Lines of Business University Alliances Events & Webinars Innovation
Log On Join Us Hi, Guest Search the Community
Activity Communications Actions
Browse
0 Tweet 0
7 Replies Latest reply: May 22, 2007 1:20 PM by Suresh Babu Aluri
Share 0 Like
3642 Views Topics: abap
Average User Rating
(0 ratings)
how to use inner join and for all entries
This question is Assumed Answered.
hi all experts,
i am new to abap .plz help me by providing details about joining two tables
using inner join to fetch some data into an internal table.
now ,iff we use for all entries in place of inner join for fetching the same relevant data .
what should we do.

please give some sample code for the same example using with inner join and for all enteries.
deepak jaiswal May 22, 2007 1:03 PM
Helpful Answer by Sandeep Ranade
Re: how to use inner join and for all entries
Hi,

Inner Join : here you are going to use two database tables having common fields, on the basis of
which you will fetch the data.

ex. select afield1 bfield2 a~field3 into table itab where DB1 as a join DB2 as b on <specify the
condtion on common field>

For all entries: Here at first you are going to fetch data from Db1 into itab1 for specified condition.
then again you will use one more select query to fetch the values from DB2 for the corresponding
entries in internal table itab1.


Ex select f1 f2 field into table itab1 from DB1 where <condition>
select f3 f4 into tabe itab2 from DB2 for all entries in itab1
where field = itab1-field and <other conditions>

Thanks
sandeep
Reward if helpful
Like (0)
Helpful Answer
Sandeep Ranade May 22, 2007 1:10 PM (in response to deepak jaiswal)
Re: how to use inner join and for all entries
Sarath kumar Chandrasekharan May 22, 2007 1:12 PM (in response to deepak jaiswal)
7/2/2014 how to use inner join and for all entries | SCN
http://scn.sap.com/thread/416290 2/6
Hi Deepak,

To use "for all entries" is as

1. declare internal tables for both the tables you declare.
2.select the values into ur header tables's internal table first from ur internal table.
3.select the value from item table using the header table's internal table like

select *
into table <item table's internal table>
from <item table>
for all entries in <header table's internal table>
where <field of item table = field of header table's internal table>

check this will work.
Like (0)
Re: how to use inner join and for all entries
Hi,

The Inner join can be very performant when you use combined table like vbak/vbap/vbep or
ekko/ekpo/eket or mara/marc/mard, ...

The for all entries statement will be less performant if you have a lot of records ( and don't forget, you
have in most cases to delete duplicates, so ... )

In my opinion, it really depends.
If you have doubt in your report, just do a test ...

Anyway, something sure is that obviously there is no better statement between "for all entries" and
"Inner Join". If there was, everybody would know it.
http://www.sap-img.com/abap/performance-tuning-for-data-selection-statement.htm


Inner join :
This is used to join two tables with matchin fields.
This is a EQUI join type means, record will be fetched if and only if joining condition is is matchin for
both tables.
This is a INNER JOIN type of SQL.
This refers two or more tables with table fields.

For all entries:
This is OUTER JOIN type of SQL.
Means, all record will be fetched from left table on joining condition. and from right table if conditions
satisfies that record will be fetched otherwise null will be retured as data.

FOR ALL ENTRIES is an effective way of doing away with using JOIN on two tables.
You can check the below code -
SELECT BUKRS BELNR GJAHR AUGDT
FROM BSEG
INTO TABLE I_BSEG
WHERE BUKRS = ....

SELECT BUKRS BELNR BLART BLDAT
FROM BKPF
INTO TABLE I_BKPF
FOR ALL ENTRIES IN I_BSEG
WHERE BUKRS = I_BSEG-BUKRS
AND BELNR = I_BSEG-BELNR
AND BLDAT IN SO_BLDAT.


*******************************8

look another example

what is the use of FOR ALL ENTRIES

1. INNER JOIN
DBTAB1 <----
> DBTAB2

It is used to JOIN two DATABASE tables
having some COMMON fields.
Priyanka Neelam May 22, 2007 1:13 PM (in response to deepak jaiswal)
7/2/2014 how to use inner join and for all entries | SCN
http://scn.sap.com/thread/416290 3/6


2. Whereas
For All Entries,
DBTAB1 <----
> ITAB1


is not at all related to two DATABASE tables.

It is related to INTERNAL table.

3. If we want to fetch data
from some DBTABLE1
but we want to fetch
for only some records
which are contained in some internal table,
then we use for alll entries.



*----
1. simple example of for all entries.

2. NOTE THAT
In for all entries,
it is NOT necessary to use TWO DBTABLES.
(as against JOIN)

3. use this program (just copy paste)
it will fetch data
from T001
FOR ONLY TWO COMPANIES (as mentioned in itab)

4

REPORT abc.



DATA : BEGIN OF itab OCCURS 0,
bukrs LIKE t001-bukrs,
END OF itab.

DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

*----

itab-bukrs = '1000'.
APPEND itab.
itab-bukrs = '1100'.
APPEND itab.


*----

SELECT * FROM t001
INTO TABLE t001
FOR ALL ENTRIES IN itab
WHERE bukrs = itab-bukrs.


*----
LOOP AT t001.
WRITE :/ t001-bukrs.
ENDLOOP.


Overall, All records from left table will be selected where as only matching record will be updated in
resultset from right table.
This generally uses ITAB as RIGHT side comparisoin.
While using For all entried Ensure that, ITAB should not be empty and it fetched unique records only.


Regards,
Priyanka.
Like (0)
7/2/2014 how to use inner join and for all entries | SCN
http://scn.sap.com/thread/416290 4/6
Re: how to use inner join and for all entries
Hi deepak

Inner join is used to join two different tables by relating them by comparing one of their common
fields

Here is the sample code for inner join

SELECT ekko~lifnr ekko~ebeln ekko~knumv ekko~bedat
ekpo~werks ekpo~matnr ekpo~txz01 ekpo~werks
ekpo~netwr
* ekpo~aedat
ekbe~ebelp ekbe~belnr
lfa1~name1
INTO CORRESPONDING FIELDS OF TABLE int_outtab
FROM ( ( ( ekko
JOIN ekbe ON ekbe~ebeln = ekko~ebeln AND
ekbe~vgabe = '2' )
JOIN ekpo ON ekpo~ebeln = ekko~ebeln AND
ekpo~ebelp = ekbe~ebelp )
JOIN lfa1 ON lfa1~lifnr = ekko~lifnr )
WHERE ekko~lifnr = P_lifnr AND
ekko~ebeln IN S_ebeln AND
* ekpo~werks IN S_werks AND
ekbe~vgabe = '2'.


Use of FOR ALL Entries
Outer join can be created using this addition to the where clause in a select statement. It speeds up
the performance tremendously, but the cons of using this variation are listed below

Duplicates are automatically removed from the resulting data set. Hence care should be taken that
the unique key of the detail line items should be given in the select statement.
If the table on which the For All Entries IN clause is based is empty, all rows are selected into the
destination table. Hence it is advisable to check before-hand that the first table is not empty.
If the table on which the For All Entries IN clause is based is very large, the performance will go down
instead of improving. Hence attempt should be made to keep the table size to a moderate level.

<b>Not Recommended</b>


Loop at int_cntry.
Select single * from zfligh into int_fligh
where cntry = int_cntry-cntry.
Append int_fligh.
Endloop.


<b>Recommended</b>


Select * from zfligh appending table int_fligh
For all entries in int_cntry
Where cntry = int_cntry-cntry.

rewards if helpfull

Regards
Pavan
Like (0)
Pavan Valluri May 22, 2007 1:15 PM (in response to deepak jaiswal)
Re: how to use inner join and for all entries
Deniz Toprak May 22, 2007 1:17 PM (in response to deepak jaiswal)
7/2/2014 how to use inner join and for all entries | SCN
http://scn.sap.com/thread/416290 5/6
you can join two tables like that:
assume the tables are spfli and sbook(just assume), now we can join these two tables:

select spcarrid spconnid sb~price from spfli as sp
inner join sbook as sb
on
spconnid = sbconnid.
endselect.

I hope That helps..
deniz.
Like (0)
Re: how to use inner join and for all entries
Hi,
Please look at this sample code where there is JOIN on EKKO and EKPO tables
and FOR ALL ENTRIES is used on the table LIPS

IF NOT i_lips[] IS INITIAL.
SELECT k~ebeln
p~ebelp
k~lifnr
k~ekorg
p~werks
p~labnr
FROM ekko AS k INNER JOIN ekpo AS p
ON kebeln EQ pebeln
INTO TABLE i_ekpo
FOR ALL ENTRIES IN i_lips
WHERE k~ebeln = i_lips-vgbel AND
p~ebelp = i_lips-vgpos+1(5).
IF sy-subrc NE 0.
MESSAGE i000 WITH text-i03.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.

Thanks
Pramod
Like (0)
Pramod Kumar Upadhyay May 22, 2007 1:18 PM (in response to deepak jaiswal)
Re: how to use inner join and for all entries
hi,

tables: mara,makt.
types: begin of itab
matnr like mara-matnr,
maktx lik emakt-maktx,
ernam like mara-ernam,
end of itab.
data: itab1 like itab occus 0 with header line.

[for joins]

select matnr maktx ernam from mara as a inner join
makt as b into table itab1
where amatnr = bmatnr and sy-langu = 'EN'.

[for all entries]

select matnr maktx ernam from mara as a for all entries in
makt as b into table itab1
where amatnr = bmatnr and sy-langu = 'EN'.


Suresh Babu Aluri May 22, 2007 1:20 PM (in response to deepak jaiswal)
7/2/2014 how to use inner join and for all entries | SCN
http://scn.sap.com/thread/416290 6/6
Follow SCN
Site Index Contact Us SAP Help Portal
Privacy Terms of Use Legal Disclosure Copyright
0 Tweet 0 Share 0 Like
if helpful reward some points.

with regards,
suresh babu aluri.
Like (0)

Potrebbero piacerti anche