创建动态内表
一、根据表名创建内表
-
程序代码:
"复杂方式 SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001. PARAMETERS:p_tab TYPE string. SELECTION-SCREEN END OF BLOCK b1. DATA:lr_struct TYPE REF TO data, lr_table TYPE REF TO data. FIELD-SYMBOLS:<fs_table> TYPE ANY TABLE, <fs_struct> TYPE any. "RTTS DATA(lo_struct) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_name( p_tab ) ). DATA(lo_table) = cl_abap_tabledescr=>create( lo_struct ). "反射机制 CREATE DATA lr_struct TYPE HANDLE lo_struct. CREATE DATA lr_table TYPE HANDLE lo_table. "引用访问 ASSIGN lr_struct->* TO <fs_struct>. ASSIGN lr_table->* TO <fs_table>. "DATA(lv_where) = 'SPRAS EQ 1'. SELECT * FROM (p_tab) INTO TABLE @<fs_table> UP TO 10 ROWS. "WHERE (lv_where) . cl_demo_output=>display_data( <fs_table> ). “简单方式 DATA:ref_table TYPE REF TO data. DATA:lv_where TYPE string. FIELD-SYMBOLS:<fs_table> TYPE ANY TABLE. PARAMETERS:p_tab TYPE dd02l-tabname OBLIGATORY DEFAULT 'TJ02T'. "创建表 CREATE DATA ref_table TYPE TABLE OF (p_tab). ASSIGN ref_table->* TO <fs_table>. lv_where = 'SPRAS EQ 1'. SELECT * FROM (p_tab) INTO TABLE @<fs_table> UP TO 10 ROWS WHERE (lv_where) . cl_demo_output=>display_data( <fs_table> ).
-
展示结果:
二、根据Fieldcat创建内表
-
程序代码:
*&---------------------------------------------------------------------* *& Report ZDMEO_001 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT zdmeo_001. *&------------------------------------------------* * alv陪ボ *&------------------------------------------------* DATA: gt_fcat TYPE lvc_t_fcat, gs_fcat TYPE lvc_s_fcat. DATA: gt_layo TYPE lvc_s_layo. DATA: gt_table TYPE REF TO data. FIELD-SYMBOLS:<fs_tab> TYPE ANY TABLE. FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE, <fs_wa> TYPE any, <fs_field> TYPE any. PERFORM frm_set_fcat."设置Fieldcat PERFORM frm_create_dynamic_tab."创建动态内表 PERFORM frm_set_data."设置值 PERFORM frm_insert_fcat."插入Fieldcat ASSIGN <fs_table> TO <fs_tab>."将动态内表1的数据复制到内表2 PERFORM frm_create_dynamic_tab."创建动态内表 MOVE-CORRESPONDING <fs_tab> TO <fs_table>."将值赋值回来 PERFORM frm_set_data. BREAK-POINT. "设置Fieldcat FORM frm_create_row USING pv_name TYPE c pv_row TYPE i pv_txt TYPE c pv_out TYPE i pv_edit TYPE c pv_no_zero TYPE c pv_tabname TYPE c. gs_fcat-fieldname = pv_name. gs_fcat-col_pos = pv_row. gs_fcat-coltext = pv_txt. gs_fcat-outputlen = pv_out. gs_fcat-edit = pv_edit. gs_fcat-no_zero = pv_no_zero. gs_fcat-tabname = pv_tabname. APPEND gs_fcat TO gt_fcat. CLEAR: gs_fcat. ENDFORM. " FRM_CREATE_ROW FORM frm_create_dynamic_tab . *&------承笆篈ず--------- CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = gt_fcat IMPORTING ep_table = gt_table EXCEPTIONS generate_subpool_dir_full = 1 OTHERS = 2. IF sy-subrc = 0. CASE sy-subrc. WHEN 1. WHEN 2. WHEN OTHERS. ENDCASE. ENDIF. ASSIGN gt_table->* TO <fs_table>. ENDFORM. FORM frm_set_fcat . PERFORM frm_create_row USING 'ZBOX' 1 '' '' '' '' ''. PERFORM frm_create_row USING 'PBDNR' 2 TEXT-006 '10' '' 'X' 'PBIM'." PERFORM frm_create_row USING 'BERID' 3 TEXT-005 '10' '' 'X' ''." PERFORM frm_create_row USING 'Z_CUSNO' 4 TEXT-039 '10' '' 'X' ''." PERFORM frm_create_row USING 'KDMAT' 5 TEXT-013 '35' '' 'X' 'KNMT'." PERFORM frm_create_row USING 'MATNR' 6 TEXT-014 '18' '' 'X' 'MAKT'." PERFORM frm_create_row USING 'MAKTX' 7 TEXT-032 '40' '' 'X' 'MAKT'." PERFORM frm_create_row USING 'PLNMG' 8 TEXT-030 '15' '' 'X' ''." PERFORM frm_create_row USING 'PDATU' 9 TEXT-031 '6' '' 'X' ''." ENDFORM. FORM frm_insert_fcat . PERFORM frm_create_row USING 'C_RESULT' 2 TEXT-008 '1' '' '' ''." PERFORM frm_create_row USING 'Z_SIGN' 3 TEXT-036 '20' '' '' ''." PERFORM frm_create_row USING 'C_MESSAGE' 4 TEXT-009 '40' '' '' ''." ENDFORM. *&---------------------------------------------------------------------* *& Form frm_set_data *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_set_data . DATA: lv_line TYPE REF TO data. CREATE DATA lv_line LIKE LINE OF <fs_table>. ASSIGN lv_line->* TO <fs_wa>. "给字段赋值. ASSIGN COMPONENT 'PBDNR' OF STRUCTURE <fs_wa> TO <fs_field>. CLEAR <fs_field>. <fs_field> = '12394'. APPEND <fs_wa> TO <fs_table>. CLEAR <fs_wa>. ENDFORM.
-
执行结果: