gpt4 book ai didi

abap - 如何访问用于 FOR ALL ENTRIES 语句的表中的数据?

转载 作者:行者123 更新时间:2023-12-02 12:17:00 25 4
gpt4 key购买 nike

我需要压缩在一个表中的 BKPF 和 BSEG 表中的一些数据。例如,假设决赛 table 必须如下所示:

| BKPF-BUKRS | BKPF-GJAHR | BKPF-MONAT | BSEG-DMBTR |
|============|============|============|============|
| 01 | 2014 | 02 | 1,751.55 |
| 01 | 2014 | 02 | 526.45 |
| 02 | 2014 | 02 | 32,112.01 |
| | | | ...... |
|============|============|============|============|

为了使程序高效,我想使用 FOR ALL ENTRIES 语句。所以它看起来像这样:

SELECT BUKRS BELNR GJAHR MONAT 
FROM BKPF
INTO CORRESPONDING FIELDS OF TABLE IT_BKPF
WHERE ....

IF IT_BKPF IS NOT INITIAL.
SELECT DMBTR
FROM BSEG
INTO CORRESPONDING FIELDS OF LS_BSEG
FOR ALL ENTRIES IN IT_BKPF
WHERE BUKRS = IT_BKPF-BUKRS AND
BELNR = IT_BKPF-BELNR AND
GJAHR = IT_BKPF-GJAHR.

LS_RESULT-BUKRS = LS_BSEG-BUKRS.
LS_RESULT-GJAHR = LS_BSEG-GJAHR.
LS_RESULT-MONAT = ???
LS_RESULT-DMBTR = LS_BSEG-DMBTR.

COLLECT LS_RESULT INTO IT_RESULT.
ENDSELECT.
ENDIF.

现在的问题是:如何获取每个条目的 BKPF-MONAT 值?有没有办法对所有条目执行此操作?另一种解决方案是两个 SELECT 循环,例如:

SELECT BUKRS BELNR GJAHR MONAT 
FROM BKPF
INTO CORRESPONDING FIELDS OF LS_BKPF
WHERE ....

SELECT DMBTR
FROM BSEG
INTO CORRESPONDING FIELDS OF LS_BSEG
WHERE BUKRS = LS_BKPF-BUKRS AND
BELNR = LS_BKPF-BELNR AND
GJAHR = LS_BKPF-GJAHR.

LS_RESULT-BUKRS = LS_BKPF-BUKRS.
LS_RESULT-GJAHR = LS_BKPF-GJAHR.
LS_RESULT-MONAT = LS_BKPF-MONAT.
LS_RESULT-DMBTR = LS_BSEG-DMBTR.

COLLECT LS_RESULT INTO IT_RESULT.
ENDSELECT.
ENDSELECT.

但我认为这不是很有效..通常您会为此使用 JOIN,但据我所知,您不能使用簇表 (BSEG) 来做到这一点。

最佳答案

尝试从 SELECT 内的 IT_BKPF 读取条目。 ENDSELECT

例如这样:

FIELD-SYMBOLS: <fs_str_bkpf> LIKE LINE OF it_bkpf.

SELECT.
...
READ TABLE it_bkpf WITH KEY bukrs = ls_bseg-bukrs belnr = ls_bseg-belnr
gjahr = ls_bseg-gjahr ASSIGNING <fs_str_bkpf>.
...
ls_result-monat = <fs_str_bkpf>-monat.
ENDSELECT.

并让表IT_BKPFHASHED TABLE或至少为SORTED TABLE,那么您应该不会遇到更大的性能问题。

这是一些工作示例(忽略CLIENT SPECIFIED,我必须使用它,因为我正在使用的客户端中没有数据)...

DATA: lt_bkpf TYPE HASHED TABLE OF bkpf
WITH UNIQUE KEY mandt bukrs belnr gjahr.
DATA: ls_bseg TYPE bseg.
TYPES: BEGIN OF t_result,
bukrs TYPE bkpf-bukrs,
gjahr TYPE bkpf-gjahr,
monat TYPE bkpf-monat,
dmbtr TYPE bseg-dmbtr,
END OF t_result.
DATA: ls_result TYPE t_result.
DATA: lt_result LIKE TABLE OF ls_result.
FIELD-SYMBOLS: <fs_str_bkpf> LIKE LINE OF lt_bkpf.

SELECT mandt bukrs belnr gjahr monat
UP TO 10 ROWS
FROM bkpf CLIENT SPECIFIED
INTO CORRESPONDING FIELDS OF TABLE lt_bkpf.

IF NOT lt_bkpf IS INITIAL.
SELECT dmbtr
FROM bseg CLIENT SPECIFIED
INTO CORRESPONDING FIELDS OF ls_bseg
FOR ALL ENTRIES IN lt_bkpf
WHERE bukrs = lt_bkpf-bukrs AND
belnr = lt_bkpf-belnr AND
gjahr = lt_bkpf-gjahr AND
mandt = lt_bkpf-mandt.

MOVE-CORRESPONDING ls_bseg TO ls_result.
READ TABLE lt_bkpf WITH KEY bukrs = ls_bseg-bukrs belnr = ls_bseg-belnr
gjahr = ls_bseg-gjahr ASSIGNING <fs_str_bkpf>.

ASSERT sy-subrc = 0 AND <fs_str_bkpf> IS ASSIGNED.

ls_result-monat = <fs_str_bkpf>-monat.
COLLECT ls_result INTO lt_result.
ENDSELECT.
ENDIF.

关于abap - 如何访问用于 FOR ALL ENTRIES 语句的表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com