on ‎2017 Jul 30 8:39 AM
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
Request clarification before answering.
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'.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
There is a method GET_DDIC_FIELD_LIST in CL_ABAP_STRUCTDESCR.
Filter returning table and you will get just key fields 🙂
DELETE lt_field_list WHERE keyflag <> 'X'.
You should put that as answer 🙂
| User | Count |
|---|---|
| 15 | |
| 9 | |
| 6 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.