利用数据库来帮助我们计算.
REPORT zsolar.
DATA: lv_re TYPE string.
PERFORM frm_test USING '20*(2+3)' CHANGING lv_re.
WRITE:/ lv_re.
*&---------------------------------------------------------------------*
*& Form frm_test
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->LV_IN text
* -->LV_OUT text
*----------------------------------------------------------------------*
FORM frm_test USING lv_in CHANGING lv_out.
DATA: code TYPE TABLE OF rssource-line,
prog(8) TYPE c, msg(120) TYPE c, lin(3) TYPE c,
wrd(10) TYPE c, off(3) TYPE c.
DATA: lv_temp TYPE string.
CONCATENATE 'SELECT' lv_in 'from mara into :lv_out' INTO lv_temp SEPARATED BY space.
APPEND 'PROGRAM SUBPOOL.' TO code.
APPEND 'FORM frm_string_count2 USING lv_in CHANGING lv_out.' TO code.
APPEND 'DATA: msg TYPE string,' TO code.
APPEND 'oref TYPE REF TO cx_sy_native_sql_error.' TO code.
APPEND 'TRY .' TO code.
APPEND 'EXEC SQL.' TO code.
* APPEND 'SELECT 12*34+56 from mara into :lv_out' TO code.
APPEND lv_temp TO code.
APPEND 'ENDEXEC.' TO code.
APPEND 'CATCH cx_sy_native_sql_error INTO oref.' TO code.
APPEND 'CALL METHOD oref->get_text' TO code.
APPEND 'RECEIVING' TO code.
APPEND 'result = msg.' TO code.
APPEND 'WRITE msg.' TO code.
APPEND 'ENDTRY.' TO code.
APPEND 'ENDFORM.' TO code.
GENERATE SUBROUTINE POOL code NAME prog
MESSAGE msg
LINE lin
WORD wrd
OFFSET off.
IF sy-subrc EQ 0.
PERFORM frm_string_count2 IN PROGRAM (prog) USING lv_in CHANGING lv_re IF FOUND.
ENDIF.
ENDFORM. "frm_test
*&---------------------------------------------------------------------*
*& Form frm_string_count
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*FORM frm_string_count2 USING lv_in CHANGING lv_out.
* DATA: msg TYPE string,
* oref TYPE REF TO cx_sy_native_sql_error.
* TRY .
* EXEC SQL.
*
* SELECT 12*34+56 from mara into :lv_out
*
* ENDEXEC.
* CATCH cx_sy_native_sql_error INTO oref.
* CALL METHOD oref->get_text
* RECEIVING
* result = msg.
* WRITE msg.
* ENDTRY.
*ENDFORM. "frm_string_count