英文:
Getting the latest Document Number from BKPF - ABAP Question
问题
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!
英文:
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!
答案1
得分: 0
首先,避免选择不必要的数据,比如 SELECT *
。始终将数据集减少到解决任务所需的绝对最小限度。在ABAP 7.4中,可以使用 SELECT ... INTO TABLE @DATA(it_result)
编写,表类型会根据选择条件自动创建。此外,使用表列存储使得选择整个行数据(例如 SELECT *)甚至更加低效。
其次,尽量避免将大型原始数据集(大量数据,尤其是与 SELECT * 结合使用时)传输到应用服务器进行后处理,如果可以在数据库上高效处理。
您只需要一个文档编号,所以 SELECT belnr UP TO 1 ROWS 是正确的选择,将搜索、排序和减少数据集的任务委托给数据库。在这种情况下,表格已经存在包括列belnr和blart的索引(您可以随时通过事务SE11
、Indexes...
检查表格索引)。
英文:
First, avoid selecting unnecessary data like SELECT *
Aways reduce the dataset to the absolut mininum required to solve the task. It became much more handy with ABAP 7.4, where one can write SELECT ... INTO TABLE @DATA(it_result)
and the table type would be automatically created in accordance with the selection criteria. Moreover, using table column store makes the selection of the whole row data (like SELECT *) even much more inefficient.
Second, try to avoid transferring big raw datasets (large amount of data, especially combined with SELECT * ... ) to the application server for post-processing when it is possible to do efficiently on the dabase.
You just need one document number, so SELECT belnr UP TO 1 ROWS is the right choice, delegating the search, sorting and reducing the dataset to the database. In this case the index including columns belnr and blart already exists for the table (you can always check the table indexes via transaction SE11
, Indexes...
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论