ABAP 动态内表
一、基础知识
REPORT ZTEST018.
DATA : BEGIN OF gs_out ,
day01 type c LENGTH 10,
day02 type c LENGTH 10 ,
END OF gs_out.
gs_out-day01 = '10'.
gs_out-day02 = '20'.
FIELD-SYMBOLS: <fs> TYPE any .
FIELD-SYMBOLS: <fs2> TYPE any .
DATA : p_fieldname type c LENGTH 20 .
DATA : p_fieldname2 type c LENGTH 20 .
p_fieldname = 'gs_out-day' && '01' .
ASSIGN (p_fieldname) TO <fs>.
<fs> = <fs> && '08' .
WRITE : <fs> .
p_fieldname2 = 'day' && '02' .
ASSIGN COMPONENT p_fieldname2 OF STRUCTURE gs_out TO <fs2>.
<fs2> = <fs2> &&'09' .
WRITE <fs2> .
二、动态内表
FORM frm_creat_inner_table .
PERFORM frm_add_field_to_table USING :
'SEL' 'CHAR' 1,
'BWKEY' 'CHAR' 4,
'BDATJ' 'CHAR' 4,
'POPER' 'CHAR' 3,
'KALNR' 'CHAR' 12,
'MATNR' 'CHAR' 40,
'MAKTX' 'CHAR' 40,
'LOSGR_S' 'QUAN' 13,
'PEINH' 'CHAR' 5,
'MEINS' 'CHAR' 3,
'BWTAR' 'CHAR' 10,
'BZCB' 'CURR' 15,
'SJCB' 'CURR' 15,
'CBCY' 'CURR' 15,
'WAERS' 'CHAR' 5.
SELECT
a~elehk,
a~elemt,
a~el_hv,
a~el_hf,
b~txele
FROM tckh3 AS a
LEFT JOIN tckh1 AS b
ON a~elehk EQ b~elehk
AND a~elemt EQ b~elemt
AND b~spras EQ @sy-langu
INTO CORRESPONDING FIELDS OF TABLE @gt_tckh3
WHERE a~elehk = @p_zjjg.
SORT gt_tckh3 BY elemt.
LOOP AT gt_tckh3 INTO gs_tckh3.
DATA lv_fieldname TYPE string.
lv_fieldname = 'KST' && gs_tckh3-el_hv && '_ST'.
PERFORM frm_add_field_to_table USING lv_fieldname 'CURR' 15."ST 该成本组件的“标准成本”并且是“完全成本”
lv_fieldname = 'KST' && gs_tckh3-el_hv && '_VT'.
PERFORM frm_add_field_to_table USING lv_fieldname 'CURR' 15."VT 该成本组件的“实际成本”并且是“完全成本”
lv_fieldname = 'KST' && gs_tckh3-el_hf && '_SF'..
PERFORM frm_add_field_to_table USING lv_fieldname 'CURR' 15."SF 该成本组件的“标准成本”并且是“固定成本”
lv_fieldname = 'KST' && gs_tckh3-el_hf && '_VF'..
PERFORM frm_add_field_to_table USING lv_fieldname 'CURR' 15."VF 该成本组件的“实际成本”并且是“固定成本”
ENDLOOP.
*此方法用于构建动态内表,输入=构建的结构,输出=dy_table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = gt_fcat
IMPORTING
ep_table = gt_table.
ASSIGN gt_table->* TO <gt_table>.
CREATE DATA wa_table LIKE LINE OF <gt_table>.
ASSIGN wa_table->* TO <wa_table>.
ENDFORM.
案例 (底数有的是1000,有的是1026 有的是 999 全部处理成1000)
FIELD-SYMBOLS: <gt_table> TYPE table, "动态内表
<wa_table> TYPE any, "动态内表指针
<fs> TYPE any,
<fs0> TYPE any.
PERFORM frm_set_value2 USING 'BZCB' CHANGING <wa_table>. "标准成本
PERFORM frm_set_value2 USING 'SJCB' CHANGING <wa_table>. "实际
FORM frm_set_value2 USING p_fieldname
CHANGING p_wa_table.
ASSIGN COMPONENT 'LOSGR_S' OF STRUCTURE p_wa_table TO <fs0>. "底数
ASSIGN COMPONENT p_fieldname OF STRUCTURE p_wa_table TO <fs>.
IF sy-subrc = 0.
<fs> = <fs> / <fs0> * 1000 .
ENDIF.
ENDFORM.
三 动态内表的利与弊
动态内表看起来是挺好的,但是也有一些弊端。
一 、性能的问题,我记得有个工单排程的程序,要看90内的工单。用了一个动态内表。但是速度比较慢,后来用静态的(写90个字段)
二、字段是否预知。
如果预知的话,有规律的话也可以在EXCEL表把ABAP代码处理好,贴到编辑器里面更容易些
三、字段不可预知
从其他内表取字段。只能用动态内表了
本文地址:https://blog.csdn.net/ityangjia/article/details/107658040
上一篇: 千元时代,中兴红牛V5 4G手机
下一篇: 热衷星星的你 别错过4G中兴星星一号