转摘https://blog.csdn.net/rztyzxzy/article/details/46458131
REPORT ztest_sy04.
TABLES: spfli, sflight, scarr.
DATA: gt_spfli LIKE TABLE OF spfli,
gt_sflight LIKE TABLE OF sflight,
gt_scarr LIKE TABLE OF scarr.
DATA: l_subrc TYPE sy-subrc,
l_msgtx TYPE bapi_msg,
spfli_dbcnt TYPE sy-dbcnt,
sflight_dbcnt TYPE sy-dbcnt,
scarr_dbcnt TYPE sy-dbcnt.
DATA: lr_ref TYPE REF TO cx_root.
START-OF-SELECTION.
PERFORM deal_data.
&---------------------------------------------------------------------
*& Form deal_data
&---------------------------------------------------------------------
text
----------------------------------------------------------------------
FORM deal_data.
" 表中若有数据,先清空
SELECT SINGLE * FROM spfli.
IF sy-subrc EQ 0.
DELETE FROM spfli.
l_subrc = sy-subrc.
IF l_subrc <> 0.
WRITE: / ‘表SPFLI清空失败!’.
ENDIF.
ENDIF.
SELECT SINGLE * FROM sflight.
IF sy-subrc EQ 0 AND l_subrc EQ 0.
DELETE FROM sflight.
l_subrc = sy-subrc.
IF l_subrc <> 0.
WRITE: / ‘表SFLIGHT清空失败!’.
ENDIF.
ENDIF.
SELECT SINGLE * FROM scarr.
IF sy-subrc EQ 0 AND l_subrc EQ 0.
DELETE FROM scarr.
l_subrc = sy-subrc.
IF l_subrc <> 0.
WRITE: / ‘表SCARR清空失败!’.
ENDIF.
ENDIF.
CHECK l_subrc EQ 0.
" 重新获取数据
CALL FUNCTION ‘BCALV_GENERATE_TEST_DATA’
EXPORTING
i_amount_spfli = 100
i_amount_sflight = 100
TABLES
et_spfli = gt_spfli
et_sflight = gt_sflight.
" 删除主键相同的记录
SORT gt_spfli BY carrid connid.
DELETE ADJACENT DUPLICATES FROM gt_spfli COMPARING carrid connid.
" 删除主键相同的记录
SORT gt_sflight BY carrid connid.
DELETE ADJACENT DUPLICATES FROM gt_sflight COMPARING carrid connid.
"插入数据
TRY.
INSERT spfli FROM TABLE gt_spfli.
l_subrc = sy-subrc.
spfli_dbcnt = sy-dbcnt.
IF l_subrc EQ 0.
INSERT sflight FROM TABLE gt_sflight.
l_subrc = sy-subrc.
sflight_dbcnt = sy-dbcnt.
ENDIF.
IF l_subrc EQ 0.
SELECT
carrid
carrname
currency AS currcode
INTO CORRESPONDING FIELDS OF TABLE gt_scarr
FROM alv_t_t2.
" 删除主键相同的记录
SORT gt_scarr BY carrid.
DELETE ADJACENT DUPLICATES FROM gt_scarr COMPARING carrid.
INSERT scarr FROM TABLE gt_scarr.
l_subrc = sy-subrc.
scarr_dbcnt = sy-dbcnt.
ENDIF.
CATCH cx_root INTO lr_ref.
l_msgtx = lr_ref->get_text( ). " 获取错误信息
l_subrc = 1."表示有错误
ENDTRY.
CASE: l_subrc. "处理是否成功
WHEN 0.
COMMIT WORK. "提交客制化表的表处理提交
WRITE: / ‘表SPFLI 已经成功生成数据:’,spfli_dbcnt, ‘条!’.
WRITE: / ‘表SFLIGHT已经成功生成数据:’,sflight_dbcnt,‘条!’.
WRITE: / ‘表SCARR 已经成功生成数据:’,scarr_dbcnt, ‘条!’.
SKIP 3.
WRITE: / ‘请查看spfli、sflight、scarr三表中的数据!’.
WHEN OTHERS.
ROLLBACK WORK."回滚客制化表的表处理提交
WRITE:/ l_msgtx. " 错误提示
ENDCASE.
ENDFORM. "deal_data
作者:rztyzxzy
来源:CSDN
原文:https://blog.csdn.net/rztyzxzy/article/details/46458131
版权声明:本文为博主原创文章,转载请附上博文链接!