动态内表的创建和使用主要有以下几点,其中方法三最简单。:
1.动态内表的创建,首先要定义动态结构,然后再根据定义的动态结构利用系统一个标准的method:”cl_ALV_table_create=>create_dynamic_table“生成动态内表。
2.动态内表的赋值,遍历动态结构,获取指定的字段,然后给指定的字段赋值。
3.动态内表的读取显示,与赋值相似,遍历动态结构,获取指定的字段,然后读取对应字段的值。
2.代码如下
TYPE-POOLS:ABAP.
PARAMETERS P_NAME TYPE TABNAME.
DATA LT_TABLE TYPE TABLE OF DFIES. "字段结构表
DATA LS_TABLE TYPE DFIES.
DATA LR_STRUC TYPE REF TO CL_ABAP_STRUCTDESCR.
DATA LR_TABLE TYPE REF TO CL_ABAP_TABLEDESCR.
DATA LR_TYPE TYPE REF TO CL_ABAP_TYPEDESCR.
DATA LR_DATA TYPE REF TO CL_ABAP_DATADESCR.
DATA LT_COMP TYPE ABAP_COMPONENT_TAB.
DATA LS_COMP LIKE LINE OF LT_COMP.
DATA DYN_WA TYPE REF TO DATA.
DATA DYN_TABLE TYPE REF TO DATA.
DATA L_STRING TYPE STRING.
FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
<DYN_WA> TYPE ANY.
*1.获取表中的字段结构
CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
TABNAME = P_NAME
TABLES
DFIES_TAB = LT_TABLE
EXCEPTIONS
NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LOOP AT LT_TABLE INTO LS_TABLE.
CONCATENATE LS_TABLE-TABNAME '-' LS_TABLE-FIELDNAME INTO L_STRING.
LS_COMP-NAME = LS_TABLE-FIELDNAME.
*读取字段类型
CALL METHOD CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME
EXPORTING
P_NAME = L_STRING
RECEIVING
P_DESCR_REF = LR_TYPE
EXCEPTIONS
TYPE_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LS_COMP-TYPE ?= LR_TYPE.
APPEND LS_COMP TO LT_COMP.
CLEAR LS_COMP.
ENDLOOP.
*根据字段目录创建动态结构类型
CALL METHOD CL_ABAP_STRUCTDESCR=>CREATE
EXPORTING
P_COMPONENTS = LT_COMP
RECEIVING
P_RESULT = LR_STRUC.
*根据动态结构创建动态内表类型
CALL METHOD CL_ABAP_TABLEDESCR=>CREATE
EXPORTING
P_LINE_TYPE = LR_STRUC
RECEIVING
P_RESULT = LR_TABLE.
*参照动态结构类型和动态内表类型创建内表与工作区
CREATE DATA DYN_WA TYPE HANDLE LR_STRUC.
CREATE DATA DYN_TABLE TYPE HANDLE LR_TABLE.
*指定内表与工作区到字段符号
ASSIGN DYN_WA->* TO <DYN_WA>.
ASSIGN DYN_TABLE->* TO <DYN_TABLE>.
*从动态表中取数到动态内表中
SELECT * INTO CORRESPONDING FIELDS OF TABLE <DYN_TABLE> UP TO 100
ROWS FROM (P_NAME).
*显示内表中的数据
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = P_NAME
TABLES
T_OUTTAB = <DYN_TABLE>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
3.代码如下
这种方式得确简单,但是对日项目中因为过分谨慎,any table这种定义方式是不可以的
PARAMETERS P_NAME TYPE TABNAME.
DATA: DYN_TABLE TYPE REF TO DATA.
DATA: DYN_WA TYPE REF TO DATA.
FIELD-SYMBOLS: <DYN_TABLE> TYPE TABLE,
<DYN_WA> TYPE ANY.
*创建动态表结构
CREATE DATA DYN_TABLE TYPE TABLE OF (P_NAME).
*创建动态内表
ASSIGN DYN_TABLE->* TO <DYN_TABLE>.
*创建动态工作区结构
CREATE DATA DYN_WA LIKE LINE OF <DYN_TABLE>.
*创建动态工作区
ASSIGN DYN_WA->* TO <DYN_WA>.
*从动态表中取数到动态内表中
SELECT * INTO CORRESPONDING FIELDS OF TABLE <DYN_TABLE> UP TO 100
ROWS FROM (P_NAME).
*对取出数据进行处理
LOOP AT <DYN_TABLE> ASSIGNING <DYN_WA>.
"内表数据处理
ENDLOOP.
*显示内表中的数据
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = P_NAME
TABLES
T_OUTTAB = <DYN_TABLE>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
网友评论