cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Getting database table keys via RTTS

Astashonok
Participant
4,647

Hi,

I cannot get table keys via the RTTS technique. I use following code:

    DATA: the_table TYPE REF TO data.
    CREATE DATA the_table TYPE TABLE OF ('MARA').
    DATA typedescription TYPE REF TO cl_abap_tabledescr.
    typedescription ?= cl_abap_tabledescr=>describe_by_data_ref( the_table ).
    DATA keys TYPE abap_table_keydescr_tab.
    keys = typedescription->get_keys( ).

It always returns full list of fields, not only primary key, which I don't want.

I also tried the detection of keys from static internal table, but got the same result.

DATA: itab LIKE mara OCCURS 0 WITH HEADER LINE,
      lt_tabdescr     TYPE abap_keydescr_tab,
      ref_table_descr TYPE REF TO cl_abap_tabledescr.

ref_table_descr ?=  cl_abap_typedescr=>describe_by_data( itab[] ).
lt_tabdescr[] = ref_table_descr->key[].<br>

Detecting the key fields via describe_by_name( ) doesn't work either.

Any suggestions? System release is SAP ECC 7.40 SP09

View Entire Topic
Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert

Well, as suggested by horst.keller in comments, you can use method get_ddic_field_list of class CL_ABAP_STRUCTDESCR.

And filter its returning table by KEYFLAG field value 🙂 For example:

DELETE lt_field_list WHERE keyflag <> 'X'.