Application Development and Automation 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: 
Read only

Cross Join in ABAP Open SQL

Former Member
0 Likes
3,985

Hi guys!

I need to implement a CROSS JOIN (cartesian product) based on two tables. To my understanding there is no construct to implement a cross join in OPEN SQL. So the question is first is this correct? And secondly in which way would you then implement a cross join of two tables with the constructs at hand in ABAP?

I have come up with the following solution of cross joining two tables, but would like to know if there is a better (not so ugly way).

Example:

SELECT * FROM tab1 AS t1 JOIN tab2 AS t2 ON t1mandt = t2mandt INTO CORRESPONDING FIELDS OF itab.

Note that this only works if the tables tab1 and tab2 is client dependent.

Regards,

Christian

1 ACCEPTED SOLUTION
Read only

sridhar_k1
Active Contributor
0 Likes
2,172

Christian,

There's no cross join construct in abap. Join statement results in a cartesin product rarely because of completely obsolete statistics missing selection or join conditions.

Try the following join to get result similar to cartesian product

select tab1location tab2matnr into itab

from tab1 join tab2 on tab1location ne tab2matnr.

if you really want to implement cross join, use native sql, try the following code,

beware of cartesian products, Basis folks doesn't like em....

data: begin of itab occurs 0,

loc like tab1-sloc

matnr like tab2-matnr,

end of itab.

exec sql performing append_itab.

select a.loc, b.matnr

into :itab

from tab1 a

cross join tab2 b

endexec.

form append_itab.

append itab.

endform.

Regards

Sridhar.

Hi Christian,

The other way is to go for two different select statements.

select * from tab1 into table it_tab1 where <where con>.

select * from tab2 into table it_tab2 where <where con>.

loop at it_itab1.

loop at it_itab2.

move-corresponding it_itab1 to it_final.

move-corresponding it_itab2 to it_final.

append it_final.

clear it_final.

endloop.

endloop.

Where it_final has all the fields from tab1 and tab2.

Regards,

Ravi

P.S: Don't know how ugly is this..;-)..

8 REPLIES 8
Read only

Former Member
0 Likes
2,172

Hi Christian,

What do you exactly mean by cross join?

I don't find anything ugly about your statement..;-)..

Thats the way to do it and moreover it doesn't make any sense to pull the data from two disjoint tables(Which have no field in common(MANDT is one such common field for all client dependent tables).

Regards,

Ravi

Read only

Former Member
0 Likes
2,172

Hi,

Well if you want to join two tables with out any condition (other than MANDT), then I guess you are on the right track.

Else, work around could be to fetch the data from the two tables indivudally into internal tables and then loop at one of the internal table and then loop at the table without any conditions.

Regards,

Ravi

Note :Please mark the helpful answers

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,172

If cross joins are the same as inner joins, then yes you can do that.

Select * into corresponding fields of table itab
             from mara
                  inner join marc
                       on mara~matnr = marc~matnr
                                 where matnr in s_matnr.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
2,172

Hi and thanks alot for the quick answers!

However, what I mean with a cross join is a join without condition. Which will result in the cartesian product (all combinations) being selected from tab1 and tab2.

This would be useful for selecting all possible material / location combinations based on a table with materials and a table with locations.

Is there no better way of doing this than using the mandt column to implement a "always true" condition as I described above?

Many thanks

/ Christian

Read only

0 Likes
2,172

Hi Christian,

The other way is to go for two different select statements.

select * from tab1 into table it_tab1 where <where con>.

select * from tab2 into table it_tab2 where <where con>.

loop at it_itab1.

loop at it_itab2.

move-corresponding it_itab1 to it_final.

move-corresponding it_itab2 to it_final.

append it_final.

clear it_final.

endloop.

endloop.

Where it_final has all the fields from tab1 and tab2.

Regards,

Ravi

P.S: Don't know how ugly is this..;-)..

Read only

Former Member
0 Likes
2,172

Hi ,

i think there no use doing cross join.

what u can do is select data in 2 different internal tables.

in this way u will have all the data but in 2 different internal tables.

it will space effective.

later in ur program u can use read statements.

i dont know why u want cross join.

Regards.

Read only

Former Member
0 Likes
2,172

Hi christian,

declare it_final with all the fiels in tables itab1 and itab2.

select * from itab1 into corresponding fields of table it_final.

if sy-subrc = 0.

select * from itab2 into corresponding fields of table it_final.

endif.

loop at it_final.

write:/ it_final-fd1,...

endloop.

hope this helps.

Do reward if helpful.

regards,

keerthi.

Read only

sridhar_k1
Active Contributor
0 Likes
2,173

Christian,

There's no cross join construct in abap. Join statement results in a cartesin product rarely because of completely obsolete statistics missing selection or join conditions.

Try the following join to get result similar to cartesian product

select tab1location tab2matnr into itab

from tab1 join tab2 on tab1location ne tab2matnr.

if you really want to implement cross join, use native sql, try the following code,

beware of cartesian products, Basis folks doesn't like em....

data: begin of itab occurs 0,

loc like tab1-sloc

matnr like tab2-matnr,

end of itab.

exec sql performing append_itab.

select a.loc, b.matnr

into :itab

from tab1 a

cross join tab2 b

endexec.

form append_itab.

append itab.

endform.

Regards

Sridhar.