1.SELECT/ENDSELECT
首先用*取全部字段,比给定字段取数效率低
其次通过字段取数,给两个关键字要比只给一个效率高
' = '放在 ‘ in '选择界面的前面
2.READ
TRANSPORTING NO FIELDSZ只读取,不赋值,为了确定是否读取成功,就要使用SY-SUBRC
先排序,形成索引表,在二分法读取
使用field_symbols效率高
3.LOOP+SELECT
FOR ALL ENTRIES IN 读多条,可以让MAKT表取数时只取符合条件且内表GT_MARA中已经有的条目,可以避免全部数据取一遍,减少时间
4.判断某个字段的值,CASE语句比IF语句好。如果是多个不确定的条件,再使用IF判断
5.关于清空操作
依次读取,每次都会进行覆盖,所以不需要清空语句
只读一条,如果此时没有第二十条,工作区内是其原来的数据,所以需要提前清空
MAKT中没有MEINS字段,move-corresponding只会把相匹配字段值赋过去,其他的字段保持原来的值,这中间不应该加清空。
MAKT中没有MEINS字段,这种如果每次不需要保留上一行的MEINS值,就需要加上清空
6.LOOP+LOOP
对内层要读的表排序,实现数据分成小块,减少读取次数。
7.对四个表取数
GT_T134T
MATNR SPRAS MTBEZ
FORM get_data.
DATA: BEGIN OF lt_mard OCCURS 0,
werks TYPE mard-werks,
lgort TYPE mard-lgort,
matnr TYPE mard-matnr,
labst TYPE mard-labst,
END OF lt_mard.
DATA: BEGIN OF lt_mara OCCURS 0,
matnr TYPE mara-matnr,
mtart TYPE mara-mtart,
meins TYPE mara-meins,
END OF lt_mara.
DATA: BEGIN OF lt_makt OCCURS 0,
matnr TYPE makt-matnr,
spras TYPE makt-spras,
maktx TYPE makt-maktx,
END OF lt_makt.
DATA: BEGIN OF lt_t134t OCCURS 0,
mtart TYPE t134t-mtart,
spras TYPE t134t-spras,
mtbez TYPE t134t-mtbez,
END OF lt_t134t.
* 取基本表
SELECT *
FROM mard
INTO CORRESPONDING FIELDS OF TABLE lt_mard
WHERE werks IN s_werks
AND lgort IN s_lgort
AND matnr IN s_matnr.
CHECK lt_mard IS NOT INITIAL.
* 取MARA
SELECT *
FROM mara
INTO CORRESPONDING FIELDS OF TABLE lt_mara
FOR ALL ENTRIES IN lt_mard
WHERE matnr = lt_mard-matnr
AND mtart IN s_mtart.
SORT lt_mara BY matnr.
* 取MAKT
SELECT *
FROM makt
INTO CORRESPONDING FIELDS OF TABLE lt_makt
FOR ALL ENTRIES IN lt_mard
WHERE matnr = lt_mard-matnr.
SORT lt_makt BY matnr.
* 取T134T
SELECT *
FROM t134t
INTO CORRESPONDING FIELDS OF TABLE lt_t134t
FOR ALL ENTRIES IN lt_mara
WHERE mtart = lt_mara-mtart.
SORT lt_t134t BY mtart.
* 循环MARD
LOOP AT lt_mard.
MOVE-CORRESPONDING lt_mard TO gs_total.
* 读取MARA
READ TABLE lt_mara WITH KEY matnr = lt_mard-matnr
BINARY SEARCH.
IF sy-subrc = 0.
"赋值.
gs_total-matnr = lt_mara-mtart.
gs_total-meins = lt_mara-meins.
ELSE.
"退出当前循环
CONTINUE.
ENDIF.
* 读MAKT
READ TABLE lt_makt WITH KEY matnr = lt_mard-matnr
BINARY SEARCH.
IF sy-subrc = 0.
"赋值.
gs_total-spras = lt_mara-spras.
gs_total-maktx = lt_mara-maktx.
ENDIF.
* 读T134T
READ TABLE lt_t134t WITH KEY mtart = lt_mard-mtart
BINARY SEARCH.
IF sy-subrc = 0.
"赋值.
gs_total-mtbez = lt_mara-mtbez.
ENDIF.
* 赋给TOTAL
APPEND gs_total TO lt_total.
ENDLOOP.
ENDFORM.
标签:mard,matnr,mara,ABAP,代码优化,lt,SAP,TYPE,mtart
From: https://blog.csdn.net/m0_64077397/article/details/143369980