Warning when the secondary key is fully specified but not used, but if indicated, then an error

I am puzzled. If I compile the following code snippet

REPORT zzy. CLASS lcl_main DEFINITION FINAL CREATE PRIVATE. PUBLIC SECTION. CLASS-METHODS: class_constructor, main. PRIVATE SECTION. TYPES: BEGIN OF t_record, transid TYPE sy-index, item1 TYPE char20, value1 TYPE p LENGTH 7 DECIMALS 2, value2 TYPE p LENGTH 7 DECIMALS 2, value3 TYPE p LENGTH 7 DECIMALS 2, value4 TYPE p LENGTH 7 DECIMALS 2, END OF t_record, tt_record TYPE STANDARD TABLE OF t_record WITH NON-UNIQUE KEY transid item1 WITH UNIQUE HASHED KEY sec_key COMPONENTS value1 value2 value3. CLASS-DATA: mt_record TYPE tt_record. ENDCLASS. CLASS lcl_main IMPLEMENTATION. METHOD class_constructor. DO 10 TIMES. INSERT VALUE t_record( transid = sy-index item1 = |Item{ sy-index }| value1 = sy-index value2 = sy-index / 2 value3 = sy-index / 4 value4 = 0 ) INTO TABLE mt_record. ENDDO. ENDMETHOD. METHOD main. DATA: l_secs TYPE i, l_millisecs TYPE i, l_start TYPE timestampl, l_end TYPE timestampl, l_diff LIKE l_start. GET TIME STAMP FIELD l_start. LOOP AT mt_record INTO DATA(ls_record) WHERE value1 = '100.00' AND value2 = '150.0' AND value3 = '10.0'. ASSERT 1 = 1. ENDLOOP. GET TIME STAMP FIELD l_end. l_diff = l_end - l_start. WRITE: / l_diff. ENDMETHOD. ENDCLASS. START-OF-SELECTION. lcl_main=>main( ). 

I get the following warning

ZZY Program
The secondary key "SEC_KEY" is fully specified. However, the primary key is used for access. Check for more access with "SEC_KEY" efficiency

but if I specify this key with USING KEY sec_key , then I will get a compile-time error!

 REPORT zzy. CLASS lcl_main DEFINITION FINAL CREATE PRIVATE. PUBLIC SECTION. CLASS-METHODS: class_constructor, main. PRIVATE SECTION. TYPES: BEGIN OF t_record, transid TYPE sy-index, item1 TYPE char20, value1 TYPE p LENGTH 7 DECIMALS 2, value2 TYPE p LENGTH 7 DECIMALS 2, value3 TYPE p LENGTH 7 DECIMALS 2, value4 TYPE p LENGTH 7 DECIMALS 2, END OF t_record, tt_record TYPE STANDARD TABLE OF t_record WITH NON-UNIQUE KEY transid item1 WITH UNIQUE HASHED KEY sec_key COMPONENTS value1 value2 value3. CLASS-DATA: mt_record TYPE tt_record. ENDCLASS. CLASS lcl_main IMPLEMENTATION. METHOD class_constructor. DO 10 TIMES. INSERT VALUE t_record( transid = sy-index item1 = |Item{ sy-index }| value1 = sy-index value2 = sy-index / 2 value3 = sy-index / 4 value4 = 0 ) INTO TABLE mt_record. ENDDO. ENDMETHOD. METHOD main. DATA: l_secs TYPE i, l_millisecs TYPE i, l_start TYPE timestampl, l_end TYPE timestampl, l_diff LIKE l_start. GET TIME STAMP FIELD l_start. LOOP AT mt_record INTO DATA(ls_record) USING KEY sec_key WHERE value1 = '100.00' AND value2 = '150.0' AND value3 = '10.0'. ASSERT 1 = 1. ENDLOOP. GET TIME STAMP FIELD l_end. l_diff = l_end - l_start. WRITE: / l_diff. ENDMETHOD. ENDCLASS. START-OF-SELECTION. lcl_main=>main( ). 

ZZY Program
The key "SEC_KEY" is a secondary key of the type "HASHED KEY". All key components should be provided in these cases.

What am I doing wrong here?

+5
source share
1 answer

As indicated in the syntax documentation of the LOOP conditions ,

If an optional table key is specified, any WHERE clause must also be optimizable . Otherwise, a syntax error occurs or an exception is thrown.

It is imperative that you have enough coffee or any other stimulants that you prefer on hand when you immerse yourself in the rules of what is comparable and optimized and what is not, because this tedious material. In this case, the specified comparison should include TYPE P and a TYPE C (for example, '100.00' ) - which should be possible - but additionally the lengths must match, and this is not so. Even the documentation states that

Due to the complexity of the comparison rules (especially for elementary data types, it is not recommended to create a set of rules that describe in detail when the comparison type corresponds to the data type of the left operand.

Bottom line: use the same type to access keys; never rely on implicit conversions. If you declare a variable or constant using the same type as used for valueN columns, and use this in comparison, it works.

+4
source

All Articles