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

Convert nested SQL with Join

0 Likes
1,739

Hi All,

I need to convert below nested SQL with join

select ebeln BELNR GJAHR into CORRESPONDING FIELDS OF is_ekbe1 from ekbe where ebeln = ( select ebeln from ekbe where belnr EQ gs_mkpf-mblnr AND gjahr EQ gs_mkpf-mjahr ).

I have done as below but i am not getting expected output.

select a~ebeln a~BELNR a~GJAHR from ekbe as a INNER JOIN ekbe as b

                    on a~ebeln = b~ebeln

                    into CORRESPONDING FIELDS OF TABLE it_ekbe1

                     where a~belnr EQ gs_mkpf-mblnr AND a~gjahr EQ gs_mkpf-mjahr.

Regards.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,682

You have the wrong alias....

select a~ebeln a~BELNR a~GJAHR from ekbe as a INNER JOIN ekbe as b
                    on a~ebeln = b~ebeln
                    into CORRESPONDING FIELDS OF TABLE it_ekbe1
                     where b~belnr EQ gs_mkpf-mblnr AND b~gjahr EQ gs_mkpf-mjahr.

17 REPLIES 17
Read only

Former Member
0 Likes
1,683

You have the wrong alias....

select a~ebeln a~BELNR a~GJAHR from ekbe as a INNER JOIN ekbe as b
                    on a~ebeln = b~ebeln
                    into CORRESPONDING FIELDS OF TABLE it_ekbe1
                     where b~belnr EQ gs_mkpf-mblnr AND b~gjahr EQ gs_mkpf-mjahr.

Read only

former_member209120
Active Contributor
0 Likes
1,682

Hi Shreenath Bhat,

Try like this

select a~ebeln a~BELNR a~GJAHR from ekbe as a INNER JOIN ekbe as b

                    on a~ebeln = b~ebeln

                    into CORRESPONDING FIELDS OF TABLE it_ekbe1

                     where a~belnr EQ gs_mkpf-mblnr AND a~gjahr EQ gs_mkpf-mjahr.

                   

                    where b~belnr EQ gs_mkpf-mblnr AND b~gjahr EQ gs_mkpf-mjahr.


Read only

Former Member
0 Likes
1,682

It would be better if you split the select as the above query results in 1 output only.

select single ebeln from ekbe into lv_ebeln

                                            where belnr EQ gs_mkpf-mblnr

                                              AND gjahr EQ gs_mkpf-mjahr.

IF sy-subrc is initial.

select ebeln BELNR GJAHR into CORRESPONDING FIELDS OF is_ekbe1

                                             from ekbe where ebeln = lv_ebeln.

Endif.

Read only

0 Likes
1,682

Hi,

I would prefer

select single ebeln belnr gjahr from ekbe into CORRESPONDING FIELDS OF is_ekbe1

                                            where belnr EQ gs_mkpf-mblnr

                                              AND gjahr EQ gs_mkpf-mjahr.

Regards,

Klaus

Read only

0 Likes
1,682

Hi

Read only

0 Likes
1,682

Hi,

1 select is always better than 2.

Cheers,

Arindam

Read only

0 Likes
1,682

The split will work faster ..

Unless you have a for all entries that is the first select resulting in more than 1 PO Number

Read only

0 Likes
1,682

But join will consume more time in this case as the resultant is only 1 record I suppose.

Please correct me sir  if I am mistaken

Read only

0 Likes
1,682
  • Sorry, I have not posted complete block. The block is as
    below.

LOOP AT gt_mkpf INTO gs_mkpf.



select a~ebeln a~BELNR a~GJAHR a~BEWTP from ekbe as a INNER JOIN ekbe as b



on a~ebeln = b~ebeln



into CORRESPONDING FIELDS OF TABLE it_ekbe1



where b~belnr EQ gs_mkpf-mblnr AND b~gjahr EQ gs_mkpf-mjahr.







APPEND LINES OF it_ekbe1 to it_ekbe.



ENDLOOP.

And i need output something like

ROW| EBELN| EBELP| ZEKKN| VGABE| GJAHR|
BELNR |BUZEi| BEWTP|

1| 4500030722| |0 |0 | |2012 |5000028540|
|0| E|

2| 4500030722| |0 |0 | |2012 |5105603280|
|0| Q|

Please let me know in the above case which
is recomended, Whether split or single sql

Regards

Read only

0 Likes
1,682

Hi,

First no sir plz... I would go by the SQL Traces more if I have to compare. But basic steps involved in Oracle query execution is (unless they are cached).

  1. A syntax check - Are all keywords present
  2. A semantic check against the dictionary - Are all table names are correct
  3. The creation of the cost-based decision tree of possible plans
  4. The generation of the lowest cost execution plan
  5. Binding the execution plan - This is where the table--> tablespace --> datafile translation occurs.
  6. Executing the query and fetching the rows.

So it depends if a SELECT SINGLE takes more time when compared to time taken for  the steps from 1 to 5.

Cheers,

Arindam

Read only

0 Likes
1,682

Well if you cannot group MKPF in the query try that and this will kill your system rather use a FOR ALL ENTRIES.

select a~ebeln a~BELNR a~GJAHR a~BEWTP from ekbe as a INNER JOIN ekbe as b

on a~ebeln = b~ebeln

into CORRESPONDING FIELDS OF TABLE it_ekbe

FOR ALL ENTRIES IN gt_mkpf

where b~belnr EQ gt_mkpf-mblnr AND b~gjahr EQ gt_mkpf-mjahr.

This will give in one shot.

I would recommend to add MKPF also to this only with a join if you can... as even FOR ALL ENTRIES is costlier still it is better than a nested loop.

I also came to know this lately by

Read only

0 Likes
1,682

Hi,

from ekbe as a INNER JOIN ekbe as b

INNER JOIN on same table?

Cheers,

Arindam

Read only

0 Likes
1,682

Hi,

The number of gt_mkpf in the real production secenario is in terms of laks or 10 Laks. Still it is recomended to use FOR ALL ENTRIES than the loop ?

Regards

Read only

0 Likes
1,682

You will hit it 10 Lakh times in loop endloop.

If needed create a copy of your program and put for all entries in it.

and run 3 times each program and evaluate the results in SE30 / SAT.

For how to use run time analysis

http://scn.sap.com/docs/DOC-10248

Read only

Former Member
0 Likes
1,682

Hi !

Do you need to write JOIN ??

I guess the output will be same with below query as well.

select ebeln BELNR GJAHR

   into CORRESPONDING FIELDS OF is_ekbe1

   from ekbe where belnr EQ gs_mkpf-mblnr AND gjahr EQ gs_mkpf-mjahr .


Read only

0 Likes
1,682

No it won't be....

Read only

Former Member
0 Likes
1,682

Hi shreenath,

U can try with the below code.

select a~ebeln

          a~BELNR

          a~GJAHR

          B~MJAHR

          into CORRESPONDING FIELDS OF TABLE it_ekbe1

          from ekbe as a INNER JOIN mkpf as b

          on a~GJAHR = b~MJAHR

          where a~belnr EQ mkpf-mblnr

          AND b~MJAHR EQ mkpf-mjahr.

Regards,

Santhosh