问题
Lets say I have a variable (char30) which contains a name of a Datatype and I would like to create another variable of this Datatype.
Example:
lv_type = 'BU_PARTNER'
data: rt_value type range of ( lv_type ).
Any tips how to achieve this in ABAP?
Thanks!
回答1:
RANGE
table is just a STANDARD
table with component like 'LOW', 'HIGH', 'EQ' and 'OPTION'
.
Using RTTS related API to create such a STANDARD
table.
data:
lr_data type ref to data,
lt_ra_string type range of string,
ls_ra_string like line of lt_ra_string,
ls_component type line of abap_component_tab,
lt_component type abap_component_tab,
lt_ra_components type abap_component_tab,
lo_struc_descr type ref to cl_abap_structdescr,
lo_table_descr type ref to cl_abap_tabledescr,
lo_data_descr type ref to cl_abap_datadescr.
field-symbols:
<lv_value> type any,
<lt_ra_type> type standard table,
<ls_ra_type> type any.
data(lv_type) = 'BU_PARTNER'.
create data lr_data type (lv_type).
lo_struc_descr ?= cl_abap_structdescr=>describe_by_data( p_data = ls_ra_string ).
lt_component = lo_struc_descr->get_components( ).
lo_data_descr ?= cl_abap_elemdescr=>describe_by_name( lv_type ).
lt_ra_components = value #( for comp in lt_component (
name = comp-name
type = cond #(
when comp-name eq 'SIGN'
or comp-name eq 'OPTION'
then comp-type
else lo_data_descr )
) ).
lo_struc_descr ?= cl_abap_structdescr=>create( lt_ra_components ).
lo_table_descr ?= cl_abap_tabledescr=>create( lo_struc_descr ).
create data lr_data type handle lo_table_descr.
assign lr_data->* to <lt_ra_type>.
create data lr_data like line of <lt_ra_type>.
assign lr_data->* to <ls_ra_type>.
assign component 'SIGN' of structure <ls_ra_type> to <lv_value>.
<lv_value> = 'I'.
assign component 'OPTION' of structure <ls_ra_type> to <lv_value>.
<lv_value> = 'EQ'.
assign component 'LOW' of structure <ls_ra_type> to <lv_value>.
<lv_value> = 'DUMMY1'.
assign component 'HIGH' of structure <ls_ra_type> to <lv_value>.
<lv_value> = 'DUMMY2'.
* <lt_ra_type> is your range table
append <ls_ra_type> to <lt_ra_type>.
回答2:
A range data type is nothing but a structure with four fields :
Sign Option Low High
So you can use RTTS to build such a structure dynamically since the data types for sign and option are fixed and the ones for 'low" and "high" you can set dynamically . Then once you have the structure object reference, you can assign it to a field symbol.
回答3:
As far as I know, there's no built-in command for creating range tables dynamically. You have to use RTTC to build up the range table:
TYPE-POOLS: abap.
DATA:
g_dataelement TYPE rollname,
go_structdescr TYPE REF TO cl_abap_structdescr,
go_tabledescr TYPE REF TO cl_abap_tabledescr,
gt_component TYPE abap_component_tab,
gs_component TYPE LINE OF abap_component_tab,
gr_range TYPE REF TO data,
gr_range_line TYPE REF TO data.
FIELD-SYMBOLS: <gs_range_line> TYPE any,
<gs_range> TYPE table.
START-OF-SELECTION.
g_dataelement = 'BU_PARTNER'.
" Create component table
gs_component-name = 'SIGN'.
gs_component-type ?= cl_abap_elemdescr=>get_c( 1 ).
INSERT gs_component INTO TABLE gt_component.
gs_component-name = 'OPTION'.
gs_component-type ?= cl_abap_elemdescr=>get_c( 2 ).
INSERT gs_component INTO TABLE gt_component.
gs_component-name = 'LOW'.
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( g_dataelement ).
INSERT gs_component INTO TABLE gt_component.
gs_component-name = 'HIGH'.
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( g_dataelement ).
INSERT gs_component INTO TABLE gt_component.
" Create type descriptors
go_structdescr ?= cl_abap_structdescr=>create( gt_component ).
go_tabledescr ?= cl_abap_tabledescr=>create( go_structdescr ).
" Create usable variables
CREATE DATA gr_range TYPE HANDLE go_tabledescr.
ASSIGN gr_range->* TO <gs_range>.
CREATE DATA gr_range_line TYPE HANDLE go_structdescr.
ASSIGN gr_range_line->* TO <gs_range_line>.
来源:https://stackoverflow.com/questions/45652532/dynamically-defined-variable-in-abap