Monday, June 11, 2007

SAP ABAP implicit inner join

select from tab1 into table itab for all entries in itab2 where f1 = itab2-f1

The statement gets clear when one understands the concept of inner join.
inner join creates a cross product of all entries from the tables specified.
It then checks the where condition and holds only those records for which the condition is satisfied.

Lets first take an example for cross product using sets.
We'll then move on to tables.

Consider two sets S1 and S2.

S1 = { 1, 2 , 3 }.
S2 = { 4 , 5 }

S1 Cross S2 = { 1, 4 } { 1,5} {2,4} {2,5} {3,4} {3,5}.

We will now apply the same concept to tables.
When it comes to tables , S1 , S2 will be the tables with records {1,2,3} and {4,5} respectively.
Lets take an example.

Tab1. - has two fields f1 and f2
Sory for the poor table representation. This is the best i could muster.

f1 f2
1 a
2 b


Tab2 has two fields f1 and f3

f1 f3
1 Rec1
1 Rec2
2 Rec3

The cross product is
where first two columns belong to tab1 , last two belong to tab2.

f1 f2 tab2-f1 tab2-f3

1 a 1 Rec1
1 a 1 Rec2
1 a 2 Rec3
2 b 1 Rec1
2 b 1 Rec2
2 b 2 Rec3


from the above only rows which satisfy the condition f1=tab2-f1 are selected

so only records 1 , 2 6 are selected.
f1 and tab2-f1 is merged as they are equal.

This is how select with for all entries works.

No comments: