Skip to Content
1

Getting database table keys via RTTS

Jul 30, 2017 at 07:39 AM

205

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Tomas Buryanek Apr 03 at 06:40 AM
2

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'.
Share
10 |10000 characters needed characters left characters exceeded
Sandra Rossi Jul 30, 2017 at 08:00 AM
8

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.

Show 8 Share
10 |10000 characters needed characters left characters exceeded

Okkay, got it. But how to get key fields of dictionary transparent table?

0

I don't know the FM off the top of my head, but you can query table DD03L to get the key fields of a DB table.

PS. Adding this as a comment because Sandra technically provided the answer to your question as you asked it.

3

I know that FM, it's GET_KEY_FIELDS_OF_TABLE. I'm begging for pure RTTS solution.

0

There is a method GET_DDIC_FIELD_LIST in CL_ABAP_STRUCTDESCR.

2

Nope. Just tested and GET_DDIC_FIELD_LIST always returns ALL fields, not only key ones. Maybe there is some trick?

0
Pavel Astashonok

Use DD03L as Mike said. RTTS doesn't need to know anything about keys/indexes of the database, as RTTS is only the dynamic part of the ABAP typing system.

0

Filter returning table and you will get just key fields :-)

DELETE lt_field_list WHERE keyflag <> 'X'.
2

You should put that as answer :)

1