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

BSEG - Select statement slow

Former Member
0 Likes
4,735

Good day master's!

I'm having a so slow query with this code:

SELECT EBELN BELNR AUGBL GJAHR KOSTL INTO TABLE IT_BSEG FROM BSEG WHERE EBELN EQ LS_EKKO-EBELN AND BUKRS EQ S_BUKRS.

I use ABAP debugger and found out the cause why my program are so slow. Reading the BSEG table take a while to read. Is there alternate way to use.

Thanks, Sorry for my bad english.

24 REPLIES 24
Read only

former_member185613
Contributor
0 Likes
3,990

Hi Kenneth,

BSEG being a cluster table make use of OPEN CURSOR method to get bulk of data at a time. Below is a sample code.

OPEN CURSOR l_cursor FOR

SELECT EBELN BELNR AUGBL GJAHR KOSTL FROM BESG

WHERE EBELN = LS_EKKO-EBELN AND BUKRS = S_BUKRS.

FETCH NEXT CURSOR l_cursor INTO TABLE IT_BSEG.

CLOSE CURSOR l_cursor.

Hope this helps,

~Athreya

Read only

0 Likes
3,990

HI,

Please can you explain ,what will happened internal when we are using open cursor

to improve performance.

Read only

0 Likes
3,990

Hi Mohan,

A SELECT statement internally uses cursor for fetching the data. The main difference is that a SELECT statement directly writes the data into the target memory. Where as a cursor selects the data first and write to the memory as a chunk of data.

Cursors are mainly used when there is chunk of data to be retrieved.

Regards,

~Athreya

Read only

0 Likes
3,990

It didn't changed at all sir.

ABAP debugger take a while to execute "FETCH NEXT CURSOR L_CUR INTO TABLE IT_BSEG."


Anyway is their any other method to get the Accounting Document no of Purchase order w/o using BSEG?

Read only

Rodrigo-Giner
Active Contributor
0 Likes
3,990

Hi Kenneth,

1- Partial solution: Add GJAHR to complete most of the table keys, you should never make a select to BSEG without BUKRS, BELNR and GJAHR.

2- Total Solution: don't use BSEG. BSEG is a Cluster table you have the same info in BSIS, BSID, BSIK, BSAS, BSAD, BSAK.

BSID, BSAD = KUNNR

BSIK, BSAK = LIFNR

BSIS, BSAS = HKONT

for example If you need vendor data just use BSID (partially) BSAD (totally)

Regards

Read only

0 Likes
3,990

Sir I didn't use GJAHR because table EKKO/EKPO don't have that, And their is a high chance that the payment of purchase order might delay or P.O. from December will be pay next year.

What I really need is to get the Accounting Document no. of Purchase order. Uncle google say use BSEG. But BSEG are to slow to use.

Read only

0 Likes
3,990

Then, in your selection criteria you are selecting data by PO number only ?

Read only

0 Likes
3,990

Hi Kenneth ,

Try to select from BKPF ( Accounting Document Header ) as following :

BKPF-XBLNR = EKKO-EBELN

then you will get BELNR ( Document Number )

Read only

0 Likes
3,990

It didn't work sir.

I try to use XBLNR but in Accounting document column doesn't show anything.

I try to do it manually, atleast 10 line by searching P.O. no. to BKPF-XBLNR using SE16 and still no entries found.

- Thanks for the effort sir.

Read only

0 Likes
3,990

Hi Kenneth,

                 try to take the first 2 digits of PO then put them in XBLNR field like this : 45*

You should find data.

Read only

Former Member
0 Likes
3,990

Hi Kenneth,



Please try this


TYPES: BEGIN OF TY_RSEG,

        BELNR TYPE RSEG-BELNR,

        GJAHR TYPE RSEG-GJAHR,

        EBELN TYPE RSEG-EBELN,

        EBELP TYPE RSEG-EBELP,

        AWKEY TYPE BKPF-AWKEY,

        END OF TY_RSEG.

TYPES: BEGIN OF TY_BKPF,

        BUKRS TYPE BKPF-BUKRS,

        BELNR TYPE BKPF-BELNR,

        GJAHR TYPE BKPF-GJAHR,

        AWKEY TYPE BKPF-AWKEY,

        END OF TY_BKPF.

DATA: IT_RSEG TYPE TABLE OF TY_RSEG,

       IT_BKPF TYPE TABLE OF TY_BKPF.

  FIELD-SYMBOLS : <FS_RSEG> TYPE TY_RSEG.

SELECT BELNR GJAHR EBELN EBELP FROM RSEG INTO CORRESPONDING FIELDS OF TABLE IT_RSEG

WHERE  EBELN IN S_EBELN.

LOOP AT IT_RSEG ASSIGNING <FS_RSEG>.

   <FS_RSEG>-AWKEY+0(10) = <FS_RSEG>-BELNR.

   <FS_RSEG>-AWKEY+10(4) = <FS_RSEG>-GJAHR.

ENDLOOP.

IF IT_RSEG[] IS NOT INITIAL.

    SELECT BUKRS BELNR GJAHR AWKEY FROM BKPF INTO TABLE IT_BKPF FOR ALL ENTRIES IN IT_RSEG

    WHERE AWKEY = IT_RSEG-AWKEY

ENDIF.

IF IT_BKPF[] IS  NOT INITIAL.

  

   SELECT * FROM BSEG INTO TABLE IT_BSEG FOR ALL ENTRIES IN IT_BKPF

   WHERE BUKRS = IT_BKPF-BUKRS

   AND   BELNR = IT_BKPF-BELNR

   AND   GJAHR = IT_BKPF-GJAHR.     

  

ENDIF.



I Think , it is helpful.


Regards,

Nilesh Patel

Read only

0 Likes
3,990

Wow this is almost perfect sir, But not all P.O. No. exist in RSEG.

I guest BSEG has everything, But that everything makes it slow.

My problem is how to get the Accounting document no (BELNR) of Purchase order no. (EBELN) and use the BELNR to get the Payments of P.O. (PAYR-CHECT,PAYR-RWBTR).

BSEG has AUGBL and BELNR (in case AUGBL fail).

Read only

0 Likes
3,990

Hi Kenneth,

All PO no exist in RSEG after invoice against PO no .

You please try table EKBE . Pass PO no and get BELNR and GJAHR . And then pass in BKPF-AWKEY.

After that Result of BKPF  pass in BSEG. Get All result.

Regards,

Nilesh Patel

Read only

0 Likes
3,990

WOW!!!
It works like magic.

Thank you so much Sir Nilesh Patel

Read only

0 Likes
3,990

How can I mark you as answer??

Read only

Read only

JL23
Active Contributor
0 Likes
3,990

This can't be closed as it is not posted as a question. The tick from the question box was removed.

This sentence is missing in the original post: This question is Not Answered.(Mark as assumed answered)

The speech bubble in the left corner of the question in your communication stream is white and not blue like it is for a question.

Read only

0 Likes
3,990

Hi Kenneth,


Please try this.




Regards,


Nilesh Patel

Read only

0 Likes
3,990

There is no "Correct Answer" and "Helpful Answer" button for me .

But anayway thread close

Read only

0 Likes
3,990

I see

Can I change it to Question?

If my memory serves me right this thread is question and there is "Correct asnwer"/"Helpful answer" button at first. But I don't know why it's gone.

Read only

JL23
Active Contributor
0 Likes
3,990

No you can't change it yourself to a question, this is only possible within 15 minutes after posting. A space moderator can do it (they can even remove the tick if they think this question has been discussed x-times before and the participants should not get any points for answering recurring questions)

Read only

0 Likes
3,990

aw..

I'm really  sorry Master's and thank you for your effort.

Read only

0 Likes
3,990

Ups, my mistake !

I did not see it was not a question, I answered on my phone and did not realize such detail ...


I'm sorry

Read only

Former Member
0 Likes
3,990

Hi ,

Simply use the key field in select query.

BUKRS

BELNR

GJAHR

BUZEI.