当完全指定了辅助密钥但未使用时发出警告,但如果指定了,则会出错



我很困惑。如果我编译以下代码

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( ).

我收到以下警告

程序ZZY
完全指定了辅助密钥"SEC_key"。但是,主
密钥用于访问。检查使用"SEC_KEY"访问是否更多
高效

但是如果我用USING KEY sec_key指定这个键,那么我会得到一个编译时错误!

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
密钥"SEC_Key"是"HASHED Key"类型的辅助密钥。在这些情况下,必须提供所有关键部件

我在这里做错了什么?

LOOP条件的语法文档中所述,

如果指定了辅助表键,则同时指定的任何WHERE条件都必须是可优化的。否则会发生语法错误或引发异常。

当深入研究什么是可比较的、可优化的,什么是不可比较的规则时,一定要手头有足够的咖啡或任何你喜欢的兴奋剂,因为这是乏味的事情。在这种情况下,指定的比较必须包括TYPE PTYPE C(如'100.00')——这应该是可能的——但另外,长度必须匹配,但它们不匹配。甚至文件中也指出

由于比较规则的复杂性(特别是对于基本数据类型,构建一组规则来详细说明比较类型与左操作数的数据类型何时匹配不是一个好主意

一句话:使用完全相同的类型进行键访问,永远不要依赖于隐式转换。如果您使用与valueN列相同的类型来声明变量或常量,并在比较中使用该类型,则它是有效的。

相关内容

  • 没有找到相关文章

最新更新