Skip to Content

Getting database table keys via RTTS

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Apr 03 at 06:40 AM

    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'.
    Add comment
    10|10000 characters needed characters exceeded

  • Jul 30, 2017 at 08:00 AM

    You are confusing the primary key of the database table MARA, and the primary key of the internal table, which are completely different.

    In your case, you have created a standard internal table with a default primary key (because you have not defined anything), which is made of all initial fields of the structure (MARA) up to the first non-character-type field (excluded).

    If you want specific key fields, use DATA ... TABLE OF ... WITH [NON-]UNIQUE KEY field1 field2, or CREATE DATA ... TABLE OF ... WITH [NON-]UNIQUE KEY (dynamic list of fields).

    For more information, refer to the ABAP documentation.

    PS: please forget the obsolete notations OCCURS and WITH HEADER LINE.

    Add comment
    10|10000 characters needed characters exceeded