简体   繁体   English

CX_SY_ARITHMETIC_ERROR 在循环中计算小时数

[英]CX_SY_ARITHMETIC_ERROR when calculating hours in loop

I'm trying to find a good way to compress data from the sap cats table.我正在尝试找到一种从 sap cat 表中压缩数据的好方法。 Instead of redundant entries for field "belnr" I just want one entry for each "belnr".而不是字段“belnr”的冗余条目,我只想要每个“belnr”一个条目。 Individual cathours should be added to a new field "totalhours" and there should also be a new field "counter" which shows me how many several entries with equal "belnr" were summarized.单个 cathours 应该添加到一个新字段“totalhours”,并且还应该有一个新字段“counter”,它向我显示有多少个具有相同“belnr”的条目被汇总。 I have different solutions, want to compare runtimes for best performance now.我有不同的解决方案,现在想比较运行时以获得最佳性能。 With a few test entries, everything works fine.通过一些测试条目,一切正常。 But when I add more test entries (round about 6000) I always get exception "CX_SY_ARITHMETIC_ERROR".但是当我添加更多测试条目(大约 6000 个)时,我总是收到异常“CX_SY_ARITHMETIC_ERROR”。 It happens on conversion of '2995'.它发生在'2995'的转换上。

In the following I show you the declaration part and one solution because all different solutions would be to much to read.在下文中,我向您展示了声明部分和一种解决方案,因为所有不同的解决方案都需要大量阅读。 Hope you guys can help me out.希望大家能帮帮我。 I think it should be easy to solve, but I can't get through it...我认为应该很容易解决,但我无法解决它......

TYPES: BEGIN OF ty_cats,
         counter   TYPE catscounte,
         raufnr    TYPE eaufnr,
         belnr     TYPE catsbelnr,
         kokrs     TYPE kokrs,
         catshours TYPE catshours,
       END OF ty_cats,
       tyt_cats TYPE STANDARD TABLE OF ty_cats.

TYPES: BEGIN OF ty_cats_belnr,
         belnr      TYPE catsbelnr,
         kokrs      TYPE kokrs,
         objnr      TYPE j_objnr,
         count      TYPE i,
         totalhours TYPE catshours,
       END OF ty_cats_belnr,
       tyt_cats_belnr TYPE STANDARD TABLE OF ty_cats_belnr.

TYPES: BEGIN OF ty_cats_belnr_sorted,
         belnr      TYPE catsbelnr,
         kokrs      TYPE kokrs,
         objnr      TYPE j_objnr,
         count      TYPE i,
         totalhours TYPE catshours,
       END OF ty_cats_belnr_sorted,
       tyt_cats_belnr_sorted TYPE  SORTED TABLE OF ty_cats_belnr_sorted WITH UNIQUE KEY belnr.


DATA: lt_cats_belnr        TYPE tyt_cats_belnr,
      lt_cats_belnr_sorted TYPE tyt_cats_belnr_sorted,
      lt_catsdb            TYPE tyt_cats,
      lt_catsdb_buf        TYPE tyt_cats,
      lv_counter           TYPE catscounte,
      lv_totalhours        TYPE catshours,
      lv_start_time        TYPE timestampl,
      lv_end_time          TYPE timestampl,
      lv_diff              TYPE p DECIMALS 5.


PARAMETERS: p_enl(3) TYPE n DEFAULT '010'.



START-OF-SELECTION.

*..: internal table with test entries

  lt_catsdb = VALUE #( ( counter =  1 kokrs = '1100' belnr = 10001 raufnr = 900000 catshours = '1.20' )
                       ( counter =  2 kokrs = '1100' belnr = 10011 raufnr = 900000 catshours = '2.00' )
                       ( counter =  3 kokrs = '1100' belnr = 10001 raufnr = 900000 catshours = '4.50' )
                       ( counter =  4 kokrs = '1100' belnr = 10012 raufnr = 900000 catshours = '1.00' )
                       ( counter =  5 kokrs = '1100' belnr = 10002 raufnr = 900000 catshours = '1.50' )
                       ( counter =  6 kokrs = '1100' belnr = 10003 raufnr = 900000 catshours = '1.00' )
                       ( counter =  7 kokrs = '1100' belnr = 10002 raufnr = 900000 catshours = '3.50' )
                       ( counter =  8 kokrs = '1100' belnr = 10004 raufnr = 900000 catshours = '1.00' )
                       ( counter =  9 kokrs = '1100' belnr = 10004 raufnr = 900000 catshours = '2.00' )
                       ( counter = 10 kokrs = '1100' belnr = 10004 raufnr = 900000 catshours = '0.50' )
                       ( counter = 11 kokrs = '1100' belnr = 10005 raufnr = 900000 catshours = '1.00' )
                       ( counter = 12 kokrs = '1100' belnr = 10006 raufnr = 900000 catshours = '1.50' ) ).

*..: copy test entries

  DO p_enl TIMES.
    lt_catsdb_buf = VALUE #( BASE lt_catsdb_buf ( LINES OF lt_catsdb ) ).
  ENDDO.

  LOOP AT lt_catsdb_buf ASSIGNING FIELD-SYMBOL(<ls_buf>).
    lv_counter = lv_counter + 1.
    <ls_buf>-counter = lv_counter.
  ENDLOOP.

  lt_catsdb = VALUE #( ( LINES OF lt_catsdb_buf ) ).

*..: First solution:

TRY.

      CLEAR: lt_cats_belnr_sorted[].

      LOOP AT lt_catsdb ASSIGNING FIELD-SYMBOL(<ls_catsdb_2>).

        INSERT VALUE #( BASE CORRESPONDING ty_cats_belnr_sorted( <ls_catsdb_2> )
                        objnr      = |OR{ <ls_catsdb_2>-raufnr ALPHA = IN }|
                        count      = REDUCE #( INIT i = 0
                                           FOR <count> IN lt_catsdb WHERE ( belnr = <ls_catsdb_2>-belnr )
                                           NEXT i = i + 1 )
                        totalhours = REDUCE #( INIT total = '0.00'
                                     FOR <cats_belnr> IN lt_catsdb WHERE ( belnr = <ls_catsdb_2>-belnr )
                                     NEXT total = total + <cats_belnr>-catshours )
                       ) INTO TABLE lt_cats_belnr_sorted.

      ENDLOOP.

    CATCH cx_root INTO lox_root.
      CLEAR lv_error_text.
      lv_error_text = lox_root->get_text( ).
      IF lv_error_text IS NOT INITIAL.
        WRITE:/ 'Fehler bei "insert (komprimierte Form)":'.
        WRITE:/ lv_error_text.
      ENDIF.

  ENDTRY.

Your problem is in the type of totalhours for result table.您的问题在于结果表的总totalhours数类型。 It is of type catshours , which is in fact a quantity field of length 4, so it cannot accept total hours that are populated on big number of records of itab lt_catsdb .它的类型为catshours ,实际上是长度为 4 的数量字段,因此它不能接受填充在大量 itab lt_catsdb记录上的总小时数。

Here is the simple test for checking this:这是检查这一点的简单测试:

  " this var is equal to 12420.00 if p_enl = 600
  DATA(sum_of_all_hours) = REDUCE dmbtr( INIT val TYPE dmbtr FOR wa IN lt_catsdb NEXT val = val + wa-catshours ). 

  " this is equal to 999.99
  DATA(max_possible) = cl_abap_exceptional_values=>get_max_value( lv_totalhours ).
  ASSIGN max_possible->* TO FIELD-SYMBOL(<max_possible>).

  ASSERT sum_of_all_hours < <max_possible>. " <- this assertion will fail

Here we check maximum value we can put into the totalhours column of lt_cats_belnr_sorted and see that it exceeds highest value allowed by its datatype, hence the error.在这里,我们检查可以放入totalhours列中的lt_cats_belnr_sorted并看到它超过了其数据类型允许的最大值,因此出现错误。

The solution : use wider quantity type with more length, eg BRGEW:解决方案:使用更长的更宽的数量类型,例如 BRGEW:

TYPES: BEGIN OF ty_cats_belnr_sorted,
        ...
        totalhours TYPE brgew,
       END OF ty_cats_belnr_sorted,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM