2017-08-12 45 views
2

動態定義的變量,可以說我有一個變量(char30),其中包含一個數據類型的名字,我想創建此數據類型的另一個變量。在ABAP

例子:

lv_type = 'BU_PARTNER' 
data: rt_value type range of (lv_type). 

任何提示如何在ABAP來實現這一目標?

謝謝!

回答

2

RANGE表只是一個STANDARD表像'LOW', 'HIGH', 'EQ' and 'OPTION'組件。

使用RTTS相關的API來創建這樣一個STANDARD表。

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>.  
1

一定範圍的數據類型是什麼,但結構有四個領域:

註冊 選項 低 高

所以,你可以使用RTTS建立動態這樣的結構,因爲數據類型標誌和選項是固定的,對於那些「低」和‘高’,您可以動態設置。然後,一旦你有結構對象的引用,你可以把它分配給字段符號。

1

據我所知,沒有建-in命令創建範圍表dynamica LLY。你必須使用RTTC建立範圍表:

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>.