我需要在一个表中压缩表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值?有没有办法用FOR ALL ENTRIES做到这一点?另一个解决方案是两个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,但AFAIK你不能用群集表(BSEG)来做。
答案 0 :(得分:1)
尝试阅读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.