简体   繁体   中英

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. Instead of redundant entries for field "belnr" I just want one entry for each "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. 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". It happens on conversion of '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. 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 .

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.

The solution : use wider quantity type with more length, eg BRGEW:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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