首页 > 代码库 > 小工具: 执行文本文件中的代码

小工具: 执行文本文件中的代码

代码很简单

但是比较实用

尤其是调试期间

(本意只想执行一些SQL的,后来发现使用的场景还是比较多的

于是就分享一下)

 

比如程序上传到测试机后

突然想用SQL修改一些自定义表中的东西

但是测试机有没有开发权限

又要从开发机改了传一个过来?

把代码写在文本文件中

执行本程序就OK

 

ABAP代码就像一个脚本一样了

测试期间多准备几个”脚本”  谁用谁知道!

效果如下:

外部文本

image

image

执行效果:

image

 

 

代码:

CLASS lcl_sql DEFINITION.  PUBLIC SECTION.    TYPES: tt_code TYPE TABLE OF string.    METHODS:      "执行外部文件中的SQL代码 方便过滤错误数据      excute.  PRIVATE SECTION.    METHODS:      invoke_local_prog CHANGING ct_code TYPE tt_code        EXCEPTIONS no_input input_error.    DATA:          gt_code TYPE TABLE OF string.ENDCLASS.CLASS lcl_sql IMPLEMENTATION.  METHOD excute.    DATA: lt_code TYPE tt_code,          ls_code TYPE string.    CALL METHOD invoke_local_prog      CHANGING        ct_code     = lt_code      EXCEPTIONS        no_input    = 1        input_error = 2        OTHERS      = 3.    IF sy-subrc <> 0.      CASE sy-subrc.        WHEN 1.          MESSAGE 获取外部SQL文件路径异常 TYPE S DISPLAY LIKE E.          RETURN.        WHEN 2.          MESSAGE 读取动态SQL异常 TYPE S DISPLAY LIKE E.          RETURN.        WHEN OTHERS.          MESSAGE 加载文件异常 TYPE S DISPLAY LIKE E.          RETURN.      ENDCASE.    ENDIF.    IF lt_code IS INITIAL.      MESSAGE 没有从外部文件获取处理代码 TYPE S DISPLAY LIKE E.      RETURN.    ENDIF.    DATA: class TYPE string,          oref TYPE REF TO object.    APPEND `program.` TO gt_code.    APPEND `class main definition.` TO gt_code.    APPEND `public section.` TO gt_code.    APPEND `methods meth.` TO gt_code.    APPEND `endclass.` TO  gt_code.    APPEND `class main implementation.` TO gt_code.    APPEND `method meth.` TO gt_code.    LOOP  AT lt_code INTO ls_code.      APPEND  ls_code TO gt_code.    ENDLOOP.    APPEND `endmethod.` TO gt_code.    APPEND `endclass.` TO gt_code.    GENERATE SUBROUTINE POOL gt_code NAME DATA(prog).    IF sy-subrc = 0.      MESSAGE 动态调用成功 TYPE S.    ELSE.      MESSAGE 生成动态Program异常 TYPE S DISPLAY LIKE E.      RETURN.    ENDIF.    class = `\PROGRAM=` && prog && `\CLASS=MAIN`.    CREATE OBJECT oref TYPE (class).    CALL METHOD oref->(METH).  ENDMETHOD.  METHOD invoke_local_prog.    DATA: lt_file_table TYPE filetable,          ls_file_table LIKE LINE OF lt_file_table,          l_rc TYPE i,          l_filename TYPE string.    CALL METHOD cl_gui_frontend_services=>file_open_dialog*      EXPORTING*        window_title            =*        default_extension       =*        default_filename        =*        file_filter             =*        with_encoding           =*        initial_directory       =*        multiselection          =      CHANGING        file_table              = lt_file_table        rc                      = l_rc*       user_action             =*       file_encoding           =      EXCEPTIONS        file_open_dialog_failed = 1        cntl_error              = 2        error_no_gui            = 3        not_supported_by_gui    = 4        OTHERS                  = 5.    IF sy-subrc <> 0.*      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO*                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.      RAISE input_error.    ENDIF.    READ TABLE lt_file_table INDEX 1 INTO ls_file_table.    IF sy-subrc <> 0.      RAISE no_input.    ENDIF.    l_filename = ls_file_table.    CALL METHOD cl_gui_frontend_services=>gui_upload      EXPORTING        filename                = l_filename*       filetype                = ‘ASC‘*       has_field_separator     = SPACE*       header_length           = 0*       read_by_line            = ‘X‘*       dat_mode                = SPACE*       codepage                = SPACE*       ignore_cerr             = ABAP_TRUE*       replacement             = ‘#‘*       virus_scan_profile      =*      IMPORTING*       filelength              =*       header                  =      CHANGING        data_tab                = ct_code*       isscanperformed         = SPACE      EXCEPTIONS        file_open_error         = 1        file_read_error         = 2        no_batch                = 3        gui_refuse_filetransfer = 4        invalid_type            = 5        no_authority            = 6        unknown_error           = 7        bad_data_format         = 8        header_not_allowed      = 9        separator_not_allowed   = 10        header_too_long         = 11        unknown_dp_error        = 12        access_denied           = 13        dp_out_of_memory        = 14        disk_full               = 15        dp_timeout              = 16        not_supported_by_gui    = 17        error_no_gui            = 18        OTHERS                  = 19.    IF sy-subrc <> 0.*     Implement suitable error handling here      RAISE input_error.    ENDIF.  ENDMETHOD.ENDCLASS.

 

调用:

DATA: go_sql TYPE REF TO lcl_sql.    CREATE OBJECT go_sql.    go_sql->excute( ).

另外,生成动态程序应该也是有权限的

但是开发的账号一般权限都比较高吧

还有这种东西就别传生产机了

万一  !!!!!

小工具: 执行文本文件中的代码