CX_SY_ARITHMETIC_ERROR 在循环中计算小时数时
CX_SY_ARITHMETIC_ERROR when calculating hours in loop
我正在尝试找到一种压缩 sap cats 数据的好方法 table。我只想为每个“belnr”输入一个条目,而不是字段“belnr”的冗余条目。应将单个 cathours 添加到新字段“totalhours”,并且还应该有一个新字段“counter”,它向我显示汇总了多少个具有相同“belnr”的条目。
我有不同的解决方案,现在想比较运行时以获得最佳性能。通过一些测试条目,一切正常。但是当我添加更多测试条目(大约 6000 个)时,我总是得到异常“CX_SY_ARITHMETIC_ERROR”。它发生在“2995”的转换上。
在下文中,我将向您展示声明部分和一个解决方案,因为所有不同的解决方案都需要大量阅读。希望你们能帮助我。我觉得应该很容易解决,但是我就是搞不定...
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.
您的问题类型为 totalhours
,结果 table。它是catshours
类型,实际上是一个长度为4的数量字段,所以它不能接受itab lt_catsdb
.
的大量记录上填充的总小时数
这里是检查这个的简单测试:
" 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
这里我们检查可以放入 lt_cats_belnr_sorted
的 totalhours
列的最大值,发现它超过了其数据类型允许的最大值,因此出现错误。
解决方法:使用更宽的数量类型和更多的长度,例如布鲁斯:
TYPES: BEGIN OF ty_cats_belnr_sorted,
...
totalhours TYPE brgew,
END OF ty_cats_belnr_sorted,
我正在尝试找到一种压缩 sap cats 数据的好方法 table。我只想为每个“belnr”输入一个条目,而不是字段“belnr”的冗余条目。应将单个 cathours 添加到新字段“totalhours”,并且还应该有一个新字段“counter”,它向我显示汇总了多少个具有相同“belnr”的条目。 我有不同的解决方案,现在想比较运行时以获得最佳性能。通过一些测试条目,一切正常。但是当我添加更多测试条目(大约 6000 个)时,我总是得到异常“CX_SY_ARITHMETIC_ERROR”。它发生在“2995”的转换上。
在下文中,我将向您展示声明部分和一个解决方案,因为所有不同的解决方案都需要大量阅读。希望你们能帮助我。我觉得应该很容易解决,但是我就是搞不定...
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.
您的问题类型为 totalhours
,结果 table。它是catshours
类型,实际上是一个长度为4的数量字段,所以它不能接受itab lt_catsdb
.
这里是检查这个的简单测试:
" 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
这里我们检查可以放入 lt_cats_belnr_sorted
的 totalhours
列的最大值,发现它超过了其数据类型允许的最大值,因此出现错误。
解决方法:使用更宽的数量类型和更多的长度,例如布鲁斯:
TYPES: BEGIN OF ty_cats_belnr_sorted,
...
totalhours TYPE brgew,
END OF ty_cats_belnr_sorted,