代码:
REPORT ZPPRTEST. "定义任意类型指针 FIELD-SYMBOLS: <f1> TYPE ANY. FIELD-SYMBOLS: <f2> TYPE c. DATA: l_txt(10) VALUE '2020-02-02'. "注意ASSING只能是同类型变量 ASSIGN l_txt TO <f1>. ASSIGN l_txt TO <f2>. "定义含结构类型指针 TYPES: BEGIN OF ty_data, matnr TYPE mara-matnr, maktx TYPE makt-maktx, END OF ty_data. FIELD-SYMBOLS: <f3> TYPE ty_data. DATA: ls_data TYPE ty_data. ls_data-matnr = '物料1'. ls_data-maktx = '物料1说明'. ASSIGN ls_data TO <f3>. "定义动态内表 DATA: lt_table TYPE REF TO data, l_line TYPE REF TO data, it_fieldcat TYPE lvc_t_fcat, is_fieldcat TYPE lvc_s_fcat. FIELD-SYMBOLS: <table> TYPE STANDARD TABLE, <line> TYPE ANY, <field> TYPE ANY. is_fieldcat-fieldname = 'MATNR' . is_fieldcat-col_pos = 1. is_fieldcat-ref_field = 'MATNR'. is_fieldcat-ref_table = 'MARA'. APPEND is_fieldcat TO it_fieldcat. CLEAR is_fieldcat. is_fieldcat-fieldname = 'MAKTX' . is_fieldcat-col_pos = 2. is_fieldcat-ref_field = 'MAKTX'. is_fieldcat-ref_table = 'MAKT'. APPEND is_fieldcat TO it_fieldcat. CLEAR is_fieldcat. CALL METHOD cl_alv_table_create=>create_dynamic_table"创建内表 EXPORTING it_fieldcatalog = it_fieldcat IMPORTING ep_table = lt_table. ASSIGN lt_table->* TO <table>. CREATE DATA l_line LIKE LINE OF <table>. ASSIGN l_line->* TO <line>. ASSIGN COMPONENT 'MATNR' OF STRUCTURE <line> TO <field>. <field> = '物料1'. ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <line> TO <field>. <field> = '物料1说明'. APPEND <line> TO <table>. CLEAR <line>. ASSIGN COMPONENT 'MATNR' OF STRUCTURE <line> TO <field>. <field> = '物料2'. ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <line> TO <field>. <field> = '物料2说明'. APPEND <line> TO <table>. CLEAR <line>. cl_demo_output=>write( <f1> ). cl_demo_output=>write( <f2> ). cl_demo_output=>write( <f3> ). cl_demo_output=>write( <table> ). cl_demo_output=>display( ).
运行效果: