方法一:
DATA: gt_structure TYPE lvc_t_fcat."ALV结构内表全局变量
FIELD-SYMBOLS: <gfs_item> TYPE STANDARD TABLE."行项目内表
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
* I_BUFFER_ACTIVE =
i_structure_name = 'ZTEST_LJR01'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_BYPASSING_BUFFER =
* i_internal_tabname = 'GT_ALV'
CHANGING
ct_fieldcat = gt_structure[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
****动态创建内表
DATA: lv_tab TYPE REF TO data.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = gt_structure
IMPORTING
ep_table = lv_tab
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
TRY.
ASSIGN lv_tab->* TO <gfs_item>.
CATCH cx_root INTO DATA(lv_cx).
MESSAGE lv_cx->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDTRY.
方法二:
DATA: zcl_struc TYPE REF TO cl_abap_structdescr,
zcl_data TYPE REF TO cl_abap_datadescr,
zcl_table TYPE REF TO cl_abap_tabledescr,
dyn_table TYPE REF TO data,
dyn_wa TYPE REF TO data.
DATA:lv_tabname TYPE tabname.
FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE,
<fs_wa> TYPE any.
* SELECT
* SINGLE
* tabname
* FROM dd02l
* INTO @DATA(lv_dd02l)
* WHERE tabname = @lv_tabname.
*
* IF lv_dd02l IS INITIAL.
*
* MESSAGE s002 WITH '所查询的数据库表不存在'(001) DISPLAY LIKE 'E'.
* LEAVE LIST-PROCESSING.
*
* ENDIF.
lv_tabname = 'ZTEST_LJR01'.
zcl_struc ?= cl_abap_typedescr=>describe_by_name( lv_tabname ).
zcl_table ?= cl_abap_tabledescr=>create( p_line_type = zcl_struc ).
CREATE DATA dyn_wa TYPE HANDLE zcl_struc.
CREATE DATA dyn_table TYPE HANDLE zcl_table.
ASSIGN dyn_wa->* TO <fs_wa>.
ASSIGN dyn_table->* TO <fs_table>.
SELECT * INTO CORRESPONDING FIELDS OF TABLE <fs_table>
FROM (lv_tabname).
IF <fs_table> IS INITIAL.
MESSAGE s002 WITH '所查询的数据库表数据为空'(002) DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = lv_tabname
TABLES
t_outtab = <fs_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.