Application Development 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: 

How to fetch n records at a time from a FM and then fetch next n records

shwetha_h
Discoverer
0 Kudos

I have a report program which is calling a function module . This function module returns all the records from a table. As the table has millions of records, it cant be returned at a time. How can we return N records from a function module and then return Next n records .The Function module and the report program are in different system .

Thanks in Advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Use open cursor and fetch cursor.

Check the program as in SAP Help.

DATA: BEGIN OF count_line, 
        carrid TYPE spfli-carrid, 
        count  TYPE i, 
      END OF count_line, 
      spfli_tab TYPE TABLE OF spfli. 

DATA: dbcur1 TYPE cursor, 
      dbcur2 TYPE cursor. 

OPEN CURSOR dbcur1 FOR 
  SELECT carrid count(*) AS count 
         FROM spfli 
         GROUP BY carrid 
         ORDER BY carrid. 

OPEN CURSOR dbcur2 FOR 
  SELECT * 
         FROM spfli 
         ORDER BY carrid. 

DO. 
  FETCH NEXT CURSOR dbcur1 INTO count_line. 
  IF sy-subrc <> 0. 
    EXIT. 
  ENDIF. 
  FETCH NEXT CURSOR dbcur2 
    INTO TABLE spfli_tab PACKAGE SIZE count_line-count. 
ENDDO. 

CLOSE CURSOR: dbcur1, 
              dbcur2.

Regards

Kannaiah

4 REPLIES 4

Former Member
0 Kudos

Hi,

Use package size in your select query.

Check the syntax by pressing F1.

Regards,

Subramanian

Former Member
0 Kudos

Use open cursor and fetch cursor.

Check the program as in SAP Help.

DATA: BEGIN OF count_line, 
        carrid TYPE spfli-carrid, 
        count  TYPE i, 
      END OF count_line, 
      spfli_tab TYPE TABLE OF spfli. 

DATA: dbcur1 TYPE cursor, 
      dbcur2 TYPE cursor. 

OPEN CURSOR dbcur1 FOR 
  SELECT carrid count(*) AS count 
         FROM spfli 
         GROUP BY carrid 
         ORDER BY carrid. 

OPEN CURSOR dbcur2 FOR 
  SELECT * 
         FROM spfli 
         ORDER BY carrid. 

DO. 
  FETCH NEXT CURSOR dbcur1 INTO count_line. 
  IF sy-subrc <> 0. 
    EXIT. 
  ENDIF. 
  FETCH NEXT CURSOR dbcur2 
    INTO TABLE spfli_tab PACKAGE SIZE count_line-count. 
ENDDO. 

CLOSE CURSOR: dbcur1, 
              dbcur2.

Regards

Kannaiah

0 Kudos

Hi Kavuri,

Out of interst iam asking How does open cursor/fetch works.

Regards,

Naveen

0 Kudos

Hi Naveen,

Open Cursor will get the data from the respective table places that in memory area.

Fetch cursor will fetch the data based on the package size.

Suppose there are 10 sales order and for each sales order there are 10 items and the package size is 10. This means for each header only 10 times can be picked.

Suppose package size is 3. this means it should pick 30 items.

So for the first fetch it will pick 30 items and it will come out of the loop. in the second fetch it will again pick next30 items and continues the same for consecutive loops.

I hope you understood.

Regards

Kannaiah