I currently have a requirement that involves getting the latest Document Number in table BKPF
. As per my analysis, the only filters I can use are the Company Code and Document Type. I tried sorting BKPF
via the Doc Num in Ascending to use the dates present in the BKPF
but the dates aren't sequential, so I can't use a date range as one of the filters. I'm currently stuck if I should use a SELECT
with the filters and then SORT
the itab by Doc Num in Descending order:
SELECT *
INTO TABLE gt_bkpf
FROM bkpf
WHERE bukrs EQ ls_upload-bukrs
AND blart EQ ls_upload-blart.
SORT gt_bkpf ASCENDING BY belnr.
DESCRIBE TABLE gt_bkpf LINES lv_lastdoc.
READ TABLE gt_bkpf INTO gs_bkpf INDEX lv_lastdoc.
or just use SELECT UP TO 1 ROWS
with the filters and ORDER by BELNR
:
SELECT belnr
UP TO 1 ROWS
INTO lv_belnr
FROM bkpf
WHERE bukrs EQ ls_upload-bukrs
AND blart EQ ls_upload-blart
ORDER BY belnr DESCENDING.
ENDSELECT.
I worry about the speed of the statements, will the UP TO 1 ROWS
be faster? (If so, by how much) Is there a better way of getting the latest Document Number (maybe like a Function Module or etc.)? Thank you in advance! :)