Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Can not dynamically create table type with two keys

former_member190293
Active Contributor
0 Likes
803

Hi!

I need to create table type with two keys. I use  cl_abap_tabledescr=>create_with_keys method for that. But in runtime it causes exception of class CX_SY_TABLE_ATTRIBUTES with text: "No alias is permitted for standard tables and tables with default keys".

I just don't understand what it means. When I use method cl_abap_tabledescr=>create with the same line type it works fine. But this way I can create only one key for the table.

Source code:

data:

           lv_error_text         type string,

           lo_exception          type ref to cx_sy_table_creation,

           lt_linktab_keys       type abap_table_keydescr_tab,

           ls_linktab_key        type abap_table_keydescr,

           lt_outtab_keys        type abap_table_keydescr_tab,

           ls_key_descr          like line of lt_outtab_keys,

           ls_key_element        type abap_table_keycompdescr,

           lr_element_descr      type ref to cl_abap_elemdescr,

           lt_components         type abap_component_tab,

           ls_component          like line of lt_components,

           lr_linktab_line_type  type ref to cl_abap_structdescr,

           lr_outtab_line_type   type ref to cl_abap_structdescr,

           lr_outtab_type_descr  type ref to cl_abap_tabledescr.

lr_outtab_type_descr  ?= cl_abap_tabledescr=>describe_by_data_ref( p_data_ref = mr_outtab ).

lt_outtab_keys        = lr_outtab_type_descr->get_keys( ).

mr_outtab_line_type   ?= lr_outtab_type_descr->get_table_line_type( ).

get_plain_structure( exporting

                                    ir_struct_handle   = mr_outtab_line_type

                          changing

                                    t_components       = lt_components ).

lr_element_descr ?= cl_abap_elemdescr=>describe_by_name( 'TV_NODEKEY' ).

ls_component-name = 'NODE_KEY'.

ls_component-type = lr_element_descr.

append ls_component to lt_components.

ls_component-name = 'RELAT_NODE'.

ls_component-type = lr_element_descr.

append ls_component to lt_components.

lr_element_descr ?= cl_abap_elemdescr=>describe_by_name( 'CHAR01' ).

ls_component-name = 'NODE_TYPE'.

ls_component-type = lr_element_descr.

append ls_component to lt_components.

read table lt_outtab_keys into ls_key_descr with key is_primary = 'X'.

if sy-subrc = 0 or sy-subrc = 2.

       ls_linktab_key-name         = ITAB_KEY_NAME.

       ls_linktab_key-is_primary   = 'X'.

       ls_linktab_key-access_kind  = 'S'.

       ls_linktab_key-key_kind     = 'U'.

       loop at ls_key_descr-components into ls_key_element.

         append ls_key_element to ls_linktab_key-components.

       endloop.

       append ls_linktab_key to lt_linktab_keys.

     endif.

     lr_linktab_line_type  = cl_abap_structdescr=>create( p_components = lt_components ).

     try.

       mr_linktab_type_hndl  = cl_abap_tabledescr=>create_with_keys( p_line_type = lr_linktab_line_type p_keys = lt_linktab_keys ).

     catch cx_sy_table_creation into lo_exception.

       lv_error_text = lo_exception->get_text( ).

     endtry.

1 ACCEPTED SOLUTION
Read only

former_member190293
Active Contributor
0 Likes
586

Solved.

Name can not be assigned to primary key.

ls_linktab_key-name         = ITAB_KEY_NAME.

Hi!

I need to create table type with two keys. I use  cl_abap_tabledescr=>create_with_keys method for that. But in runtime it causes exception of class CX_SY_TABLE_ATTRIBUTES with text: "No alias is permitted for standard tables and tables with default keys".

I just don't understand what it means. When I use method cl_abap_tabledescr=>create with the same line type it works fine. But this way I can create only one key for the table.

Source code:

data:

           lv_error_text         type string,

           lo_exception          type ref to cx_sy_table_creation,

           lt_linktab_keys       type abap_table_keydescr_tab,

           ls_linktab_key        type abap_table_keydescr,

           lt_outtab_keys        type abap_table_keydescr_tab,

           ls_key_descr          like line of lt_outtab_keys,

           ls_key_element        type abap_table_keycompdescr,

           lr_element_descr      type ref to cl_abap_elemdescr,

           lt_components         type abap_component_tab,

           ls_component          like line of lt_components,

           lr_linktab_line_type  type ref to cl_abap_structdescr,

           lr_outtab_line_type   type ref to cl_abap_structdescr,

           lr_outtab_type_descr  type ref to cl_abap_tabledescr.

lr_outtab_type_descr  ?= cl_abap_tabledescr=>describe_by_data_ref( p_data_ref = mr_outtab ).

lt_outtab_keys        = lr_outtab_type_descr->get_keys( ).

mr_outtab_line_type   ?= lr_outtab_type_descr->get_table_line_type( ).

get_plain_structure( exporting

                                    ir_struct_handle   = mr_outtab_line_type

                          changing

                                    t_components       = lt_components ).

lr_element_descr ?= cl_abap_elemdescr=>describe_by_name( 'TV_NODEKEY' ).

ls_component-name = 'NODE_KEY'.

ls_component-type = lr_element_descr.

append ls_component to lt_components.

ls_component-name = 'RELAT_NODE'.

ls_component-type = lr_element_descr.

append ls_component to lt_components.

lr_element_descr ?= cl_abap_elemdescr=>describe_by_name( 'CHAR01' ).

ls_component-name = 'NODE_TYPE'.

ls_component-type = lr_element_descr.

append ls_component to lt_components.

read table lt_outtab_keys into ls_key_descr with key is_primary = 'X'.

if sy-subrc = 0 or sy-subrc = 2.

       ls_linktab_key-name         = ITAB_KEY_NAME.

       ls_linktab_key-is_primary   = 'X'.

       ls_linktab_key-access_kind  = 'S'.

       ls_linktab_key-key_kind     = 'U'.

       loop at ls_key_descr-components into ls_key_element.

         append ls_key_element to ls_linktab_key-components.

       endloop.

       append ls_linktab_key to lt_linktab_keys.

     endif.

     lr_linktab_line_type  = cl_abap_structdescr=>create( p_components = lt_components ).

     try.

       mr_linktab_type_hndl  = cl_abap_tabledescr=>create_with_keys( p_line_type = lr_linktab_line_type p_keys = lt_linktab_keys ).

     catch cx_sy_table_creation into lo_exception.

       lv_error_text = lo_exception->get_text( ).

     endtry.

1 REPLY 1
Read only

former_member190293
Active Contributor
0 Likes
587

Solved.

Name can not be assigned to primary key.

ls_linktab_key-name         = ITAB_KEY_NAME.