我需要从表BKPF和BSEG压缩到一个表中的一些数据。例如,假设最终表看起来像这样:
| 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)。
尝试从IT_BKPF
读取SELECT. 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_BKPF
为HASHED 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.