Application Development 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: 

Internal tables - is it possible to link a secondary index with the primary index?

0 Kudos

Hi,

I'm trying to use standard internal tables, using a sorted access via a secondary key. This internal table is displayed in an ALV grid.

In certain cases, after finding a record, which is read using the secondary key, I want to highlight that row in the grid, using method SET_SELECTED_ROWS from class CL_GUI_ALV_GRID. After finding the records, the SY-TABIX contains the index from secondary index table. Method SET_SELECTED_ROWS  allows only (if my understanding is correct) to set the the row id as per the primary index table.

If I do the READ TABLE using the primary key in order to be able to tell SET_SELECTED_ROW what is the proper row id, then I'm skipping the benefits of using a secondary sorted key (I would want to avoid SORT and READ with BINARY SEARCH).

I I do the READ TABLE using the secondary sorted key, I'm not able to tell SET_SELECTED_ROW what is the row id I want to highlight.....

Is there any way to get the table's primary index having the secondary one (without doing two READS of course)? ie is there any link between secondary and primary indexes from an internal table?

Thank you. I appreciate your help and time.

Regards,

Luis

1 ACCEPTED SOLUTION

former_member182466
Contributor
0 Kudos

Only option I see to avoid the second read is to add the index as an extra field in the table. You'd need to fill it every time the data is sorted.

7 REPLIES 7

former_member182466
Contributor
0 Kudos

Only option I see to avoid the second read is to add the index as an extra field in the table. You'd need to fill it every time the data is sorted.

0 Kudos

Thank you Gerrit, that could be a valid option.

I'm still curious if the system maintains somewhere the link between primary and secondary keys of an internal table. Appreciate if someone could confirm or invalidate this thought.

Thank you. Regards,

Luis

0 Kudos

HI Luis,

A primary key is a field or group of fields that uniquely identify a record in a table. Primary Key fields cannot be NULL and cannot contain duplicate values. If you want to link two tables, the primary key of one table will be added to another table where primary key of first table will be become the foreign key of second table



http://zevolving.com/2013/03/abap-internal-table-secondary-keys-new-in-abap-7-0-ehp2/



Regards,

Srini.

0 Kudos

Thank you Srini for your time.

Just let me mention that I was not trying to link two internal tables.

I have one standard internal table, defined with a sorted secondary key. I read individual records, with READ TABLE and using the secondary key. If a record is found, SY-TABIX contains the index as per the secondary index table. This does not allow me to highlight this finding in an ALV, unless I can pick up the primary index (the physical row number).

My question was: is it possible to know the primary index (row number), if I have the secondary index?

Gerrit gave a good workaround. I'm just still curious if the system maintains that link or not.

Thanks. Regards,

Luis

0 Kudos

I'm just still curious if the system maintains that link or not.

Hi Luis,

I think SAP documentation pretty much answers your question Read the "Note" section in this ABAP Keyword Documentation.


Note that the sy-tabix system field is always set in relation to the used table key. If the value of sy-tabix is used as an index specification following the execution of the READ statement in another processing statement for the internal table, the same table key should be used there, whereby it is important to note that the primary index is always addressed if there is no explicit key specification.

BR,

Suhas

0 Kudos

Thank you Suas.

I'm afraid I can't completely agree with you with regards to SAP documentation containing the answer for my question. My understanding is that SAP is saying that SY-TABIX will contain the concerning index in the context of the key used during the READ TABLE. And if we want to access the table afterwards using that index, we must keep the same key context. That's why I can't call method SET_SELECTED_ROWS of CL_GUI_ALV_GRID after reading an internal table with a secondary sorted key.

Anyway, if this discussion didn't lead to an immediate YES for my question, I shall now assume that the answer is no.

We may keep in mind the workaround given by Garrit.

Thank you all.

Ragards,

Luis

0 Kudos

I'm afraid I can't completely agree with you with regards to SAP documentation containing the answer for my question.

What i meant to say was, per SAP documentation the answer to your question is NO. Therefore you cannot set the correct row via SET_SELECTED_ROWS( ) using the SY-TABIX set after READ TABLE using secondary key.

This means you have to use somekind of workaround. The one suggested by Gerrit seems legit!