1.定义

data:zprog like abapsource occurs 0 with header line,

     prog(20) type c,

     msg(255) type c.

 

 

2.动态语句

    zprog-line = 'PROGRAM ZRICOO.'. append zprog.

    zprog-line = 'START-OF-SELECTION.'. append zprog.

    zprog-line = 'FORM RICOO.'. append zprog.

    zprog-line = 'UPDATE ZSDT0001 SET… WHERE … .'.  append zprog.

    zprog-line = 'ENDFORM.'.  append zprog.

    generate subroutine pool zprog

                        name prog

                        message msg.

 

3.全部代码

*&---------------------------------------------------------------------*
*& Report  ZSDA073_DO_RICO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
 
REPORT  ZSDA073_DO_RICO.
 
data container1 type ref to cl_gui_custom_container.
data editor1 type ref to cl_gui_textedit.
data container2 type ref to cl_gui_custom_container.
data editor2 type ref to cl_gui_textedit.
data container3 type ref to cl_gui_custom_container.
data editor3 type ref to cl_gui_textedit.
 
 
data: gt_fieldcat like lvc_s_fcat occurs 0 with header line,
      gs_layout type lvc_s_layo.
data: cuscontainer type ref to cl_gui_custom_container,
      alv_grid  type ref to cl_gui_alv_grid,
      ls_vari type disvariant.
data:lt_excl_func type ui_functions.
 
types:begin of t_texttable,
       line(255) type c,
      end of t_texttable.
data i_texttable type table of t_texttable.
 
data: zprog like abapsource occurs 0 with header line.
data: zinclude like abapsource occurs 0 with header line.
 
data:data(256) type c occurs 0,
     sql(256) type c occurs 0,
     dline(256) type c,
     sline(256) type c,
     confirm type c,
     prog(20) type c,
     msg(255) type c,
     sqlinclude(20) type c,
     zmsg(255) type c,
     zt1 type string,
     zt2 type string,
     exflg type c,
     zcom type string,
     znam type string.
data:lin type i,
     flg type c,
     zln type char10,
     zcode type string,
     zn type string,
     zsn type string,
     z type string.
 
start-of-selection.
    z = ''''.
    z = z+0(1).
 
call screen 100.
 
module status_0100 output.
  set pf-status '100'.
 
    if container1 is initial.
    create object container1
      exporting
        container_name = 'DATACON'.
    create object editor1
      exporting
        parent                     = container1
        wordwrap_mode              = cl_gui_textedit=>wordwrap_at_fixed_position
        wordwrap_position          = 58
        wordwrap_to_linebreak_mode = cl_gui_textedit=>true.
 
    call method editor1->set_readonly_mode
      exporting
        readonly_mode = cl_gui_textedit=>false.
 
 
    call method editor1->set_toolbar_mode
      exporting
        toolbar_mode = cl_gui_textedit=>false
      exceptions
        others       = 1.
 
    call method editor1->set_statusbar_mode
      exporting
        statusbar_mode = cl_gui_textedit=>false
      exceptions
        others         = 1.
  endif.
 
  clear:i_texttable.
 
  append 'DATA:BEGIN OF TAB OCCURS 0,' to i_texttable.
  append 'MANDT LIKE MARA-MANDT,' to i_texttable.
  append 'MATNR LIKE MARA-MATNR,' to i_texttable.
  append 'MAKTX LIKE MAKT-MAKTX,' to i_texttable.
  append 'END OF TAB.' to i_texttable.
  append 'DATA:MANDT LIKE SY-MANDT.' to i_texttable.
  append 'MANDT = SY-MANDT.' to i_texttable.
 
    call method editor1->set_text_as_r3table
      exporting
        table = i_texttable.
 
 
 
    if container2 is initial.
    create object container2
      exporting
        container_name = 'SQLCON'.
    create object editor2
      exporting
        parent                     = container2
        wordwrap_mode              = cl_gui_textedit=>wordwrap_at_fixed_position
        wordwrap_position          = 58
        wordwrap_to_linebreak_mode = cl_gui_textedit=>true.
 
    call method editor2->set_readonly_mode
      exporting
        readonly_mode = cl_gui_textedit=>false.
 
 
    call method editor2->set_toolbar_mode
      exporting
        toolbar_mode = cl_gui_textedit=>false
      exceptions
        others       = 1.
 
    call method editor2->set_statusbar_mode
      exporting
        statusbar_mode = cl_gui_textedit=>false
      exceptions
        others         = 1.
  endif.
 
  clear:i_texttable.
      append 'SELECT A.MANDT,A.MATNR,B.MAKTX INTO :TAB FROM MARA A INNER ' to i_texttable.
      append 'JOIN MAKT B ON A.MATNR = B.MATNR  ' to i_texttable.
        append 'WHERE A.MATNR like ''%33%'' AND A.MANDT = :MANDT ' to i_texttable.
 
    call method editor2->set_text_as_r3table
      exporting
        table = i_texttable.
 
 
 
 
    if container3 is initial.
    create object container3
      exporting
        container_name = 'SMCON'.
    create object editor3
      exporting
        parent                     = container3
        wordwrap_mode              = cl_gui_textedit=>wordwrap_at_fixed_position
        wordwrap_position          = 32
        wordwrap_to_linebreak_mode = cl_gui_textedit=>true.
 
    call method editor3->set_readonly_mode
      exporting
        readonly_mode = cl_gui_textedit=>true.
 
 
    call method editor3->set_toolbar_mode
      exporting
        toolbar_mode = cl_gui_textedit=>false
      exceptions
        others       = 1.
 
    call method editor3->set_statusbar_mode
      exporting
        statusbar_mode = cl_gui_textedit=>false
      exceptions
        others         = 1.
  endif.
 
  clear:i_texttable.
      append '注意:' to i_texttable.
      append '1.先定义输出内表,然后定义变量' to i_texttable.
      append '2.内表名:TAB' to i_texttable.
      append '3.SQL语句变量前需加 '':''.' to i_texttable.
      append '4.自定义数据表中需有''MANDT''字段,否则会操作多个''CLIENT''.' to i_texttable.
      append ' ' to i_texttable.
      append '如左边实例:' to i_texttable.
      append ' ' to i_texttable.
      append ' ' to i_texttable.
      append '修改如下:' to i_texttable.
      append 'UPDATE MAKT SET MAKTX = ''RICOO'' WHERE MATNR LIKE ''%RICO%'' AND MANDT = :MANDT ' to i_texttable.
    call method editor3->set_text_as_r3table
      exporting
        table = i_texttable.
 
endmodule.
 
 
module user_command_0100 input.
  case sy-ucomm.
    when '&EXEC'.
    clear:data,sql,dline,sline,confirm,zt1,zt2,exflg,zprog,zinclude.
    refresh:data,sql,zprog,zinclude.
 
    call method editor1->get_text_as_r3table
          importing
            table = data.
 
    call method editor2->get_text_as_r3table
          importing
            table = sql.
 
 
 
    call function 'POPUP_TO_CONFIRM_STEP'
         exporting
              textline1 = 'DO YOU WANT TO REALLY EXEC?'
              titel     = 'EXIT'
         importing
              answer    = confirm.
    case confirm.
      when 'N'.
         exit.
      when 'A'.
         exit.
      when 'J'.
    endcase.
 
    loop at sql into sline.
      if sline <> ''.
        translate sline to upper case.
        split sline at space  into zt1 zt2.
        if zt1 = 'SELECT'.
           perform include_sql.
           perform s_data.
        else.
          select single count(*) from zsdt021 where uname = sy-uname and unmtx = 'X'.
          if sy-subrc = 0.
             perform u_data.
          else.
             message e001(00) with '您无权限执行此操作!!!'.
          endif.
        endif.
        exit.
      endif.
      clear:sline.
    endloop.
    when '&EXIT'.
      leave to screen 0.
  endcase.
endmodule.
 
 
form s_data.
 
    clear:lin,zt1,zt2,zn,zln.
 
    ZPROG-LINE = 'PROGRAM ZSQL_EXEC_RICOO.'. APPEND ZPROG.
    ZPROG-LINE = 'DATA:ZN TYPE STRING,'. APPEND ZPROG.
    ZPROG-LINE = 'LIN TYPE I,'. APPEND ZPROG.
    ZPROG-LINE = 'ZJS TYPE I.'. APPEND ZPROG.
    ZPROG-LINE = 'START-OF-SELECTION.'. APPEND ZPROG.
 
    loop at data into dline.
      translate dline to upper case.
      search dline for 'BEGIN OF'.
      if sy-subrc = 0.
      split dline at 'OF' into zt1 zt2.
      shift zt2 left deleting leading space.
      split zt2 at space into zt1 zt2.
      endif.
      zprog-line = dline.
      append zprog.
      clear:dline.
    endloop.
 
    ZPROG-LINE = 'FORM RICOO.'. APPEND ZPROG.
 
    clear:flg.
    loop at data into dline.
      translate dline to upper case.
      if flg = 'X'.
      zprog-line = dline.
      append zprog.
      endif.
      search dline for 'END OF'.
      if sy-subrc = 0.
        flg = 'X'.
      endif.
      clear:dline.
    endloop.
 
*    ZPROG-LINE = 'MANDT = SY-MANDT.'. APPEND ZPROG.
    zprog-line = 'EXEC SQL PERFORMING APP_TAB.'. append zprog.
    loop at sql into sline.
    translate sline to upper case.
    zprog-line = sline.
    append zprog.
    clear:sline.
    endloop.
*    zprog-line = ' AND MANDT = :MANDT'. append zprog.
    zprog-line = 'ENDEXEC.'.  append zprog.
    CLEAR:ZSN.
*    CONCATENATE 'APPEND ' ZT1 '.' INTO ZSN SEPARATED BY SPACE.
*    zprog-line = ZSN.  append zprog.
    clear:ZSN.
    concatenate 'EXPORT ' ZT1 ' TO MEMORY ID ''RICOO''.' INTO ZSN SEPARATED BY SPACE.
    zprog-line = ZSN. append zprog.
    zprog-line = 'DESCRIBE TABLE TAB LINES LIN.'. append zprog.
    zprog-line = 'EXPORT LIN TO MEMORY ID ''RICOOLIN''.'. append zprog.
    zprog-line = 'SUBMIT ZMMT2099_EXEC_RICOO_CALL AND RETURN.'. APPEND ZPROG.
*    zprog-line = 'MESSAGE S001(00) WITH ''Exec success ! ^_^ ''.'.  append zprog.
    zprog-line = 'ENDFORM.'.  append zprog.
    zprog-line = 'FORM APP_TAB.'.  append zprog.
    CLEAR:ZSN.
    CONCATENATE 'APPEND ' ZT1 '.' INTO ZSN SEPARATED BY SPACE.
    zprog-line = ZSN.  append zprog.
    zprog-line = 'ENDFORM.'.  append zprog.
    CLEAR:ZCODE.
    generate subroutine pool zprog
                        name prog
                        message msg.
  if sy-subrc <> 0.
    message e001(00) with msg.
  else.
    perform ricoo in program (prog).
  endif.
 
 
endform.
 
form u_data.
 
    zprog-line = 'PROGRAM ZRICOO.'. append zprog.
*    zprog-line = 'DATA:MANDT LIKE SY-MANDT.'. append zprog.
    zprog-line = 'START-OF-SELECTION.'. append zprog.
 
    loop at data into dline.
    translate dline to upper case.
    zprog-line = dline.
    append zprog.
    clear:dline.
    endloop.
 
    zprog-line = 'FORM RICOO.'. append zprog.
 
    clear:flg.
    loop at data into dline.
      translate dline to upper case.
      if flg = 'X'.
      zprog-line = dline.
      append zprog.
      endif.
      search dline for 'END OF'.
      if sy-subrc = 0.
        flg = 'X'.
      endif.
      clear:dline.
    endloop.
 
*    zprog-line = 'EXEC SQL.'. append zprog.
 
    loop at sql into sline.
    translate sline to upper case.
    zprog-line = sline.
    append zprog.
    clear:sline.
    endloop.
 
*    zprog-line = 'ENDEXEC.'.  append zprog.
 
    zprog-line = 'MESSAGE S001(00) WITH ''Exec success ! ^_^ ''.'.  append zprog.
    zprog-line = 'ENDFORM.'.  append zprog.
 
    generate subroutine pool zprog
                        name prog
                        message msg.
 
  if sy-subrc <> 0.
    message e001(00) with msg.
  else.
    perform ricoo in program (prog).
  endif.
 
endform.
 
 
form include_sql.
    clear:zinclude.
    clear:lin,zt1,zt2,zn,zln.
    refresh:zinclude.
 
    zinclude-LINE = 'PROGRAM ZSQL_RICOO_INCLUED.'. APPEND zinclude.
    zinclude-LINE = 'DATA:ZN TYPE STRING,'. APPEND zinclude.
    zinclude-LINE = 'ZJS TYPE I.'. APPEND zinclude.
    zinclude-LINE = 'TYPES CODE_TYPE TYPE RSSOURCE-LINE.'. APPEND zinclude.
    zinclude-LINE = 'DATA CODE LIKE TABLE OF RSSOURCE WITH HEADER LINE.'. APPEND zinclude.
    zinclude-LINE = 'DATA:ZLIN TYPE I,'. APPEND zinclude.
    zinclude-LINE = 'ZT TYPE I.'. APPEND zinclude.
    zinclude-LINE = 'FORM ZRICOO_SQL_INCLUDE.'. APPEND zinclude.
    zinclude-LINE = 'READ REPORT ''ZSQL_INCLUDE'' INTO CODE.'. APPEND zinclude.
    zinclude-LINE = 'DESCRIBE TABLE CODE LINES ZLIN.'. APPEND zinclude.
    zinclude-LINE = 'DO ZLIN TIMES.'. APPEND zinclude.
    zinclude-LINE = 'ZT = ZT + 1.'. APPEND zinclude.
    zinclude-LINE = 'MODIFY CODE INDEX ZT FROM  ''''.'. APPEND zinclude.
    zinclude-LINE = 'ENDDO.'. APPEND zinclude.
    zinclude-LINE = 'INSERT REPORT ''ZSQL_INCLUDE'' FROM CODE.'. APPEND zinclude.
 
    loop at data into dline.
    zn = zn + 1.
    concatenate z dline z into dline.
    concatenate 'ZJS = ' ZN '.' INTO ZSN SEPARATED BY SPACE.
    zinclude-LINE = ZSN. APPEND zinclude.
    zinclude-line = 'IF ZJS > ZLIN.'. APPEND zinclude.
    CONCATENATE 'CODE-LINE = ' DLINE '.' INTO ZSN SEPARATED BY SPACE.
    zinclude-LINE = ZSN.  APPEND zinclude.
    zinclude-LINE = 'APPEND CODE.'. APPEND zinclude.
    zinclude-LINE = 'ELSE.'. APPEND zinclude.
    concatenate 'MODIFY CODE INDEX ZJS FROM ' DLINE '.' INTO ZCODE SEPARATED BY SPACE.
    zinclude-line = zcode. append zinclude. clear:zcode.
    zinclude-LINE = 'ENDIF.'.APPEND zinclude.
 
    search dline for 'END OF'.
     if sy-subrc = 0.
        exit.
     endif.
    clear:dline,zcode.
    endloop.
    CLEAR:ZSN.
    zinclude-LINE = 'ZJS = ZJS + 1.'. APPEND zinclude.
    ZSN = '''RICOO'''.
    CONCATENATE Z ZSN Z '.' INTO ZSN .
    CONCATENATE  'IMPORT TAB FROM MEMORY ID ' ZSN INTO ZSN SEPARATED BY SPACE.
    zinclude-line = 'IF ZJS > ZLIN.'. APPEND zinclude.
    CONCATENATE Z ZSN Z INTO ZSN SEPARATED BY SPACE.
    zinclude-line = 'CODE-LINE =   '. APPEND ZINCLUDE.
    CONCATENATE ' ' ZSN '.' INTO ZSN SEPARATED BY SPACE.
    zinclude-LINE = ZSN.  APPEND zinclude.
    zinclude-LINE = 'APPEND CODE.'. APPEND zinclude.
    zinclude-LINE = 'ELSE.'. APPEND zinclude.
    concatenate 'MODIFY CODE INDEX ZJS FROM ' ZSN INTO ZCODE SEPARATED BY SPACE.
    zinclude-line = zcode. append zinclude. clear:zcode.
    zinclude-LINE = 'ENDIF.'.APPEND zinclude.
 
    zinclude-LINE = 'INSERT REPORT ''ZSQL_INCLUDE'' FROM CODE.'. APPEND zinclude.
    zinclude-LINE = 'INCLUDE ZSQL_INCLUDE.'. APPEND zinclude.
    zinclude-LINE = 'COMMIT WORK.'. APPEND zinclude.
    zinclude-LINE = 'ENDFORM.'. APPEND zinclude.
    clear:zsn,zn,zcode,dline.
    generate subroutine pool zinclude
                        name sqlinclude
                        message zmsg.
 
  if sy-subrc <> 0.
    message e001(00) with zmsg.
  else.
    perform ZRICOO_SQL_INCLUDE in program (sqlinclude).
  endif.
 
endform.
View Code

 

4.基于Class定义

FUNCTION ZFGTR0110_01_DYNAMIC_PROGRAM.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(IV_ANY) TYPE  ANY OPTIONAL
*"     REFERENCE(IV_PRG) TYPE  STRING OPTIONAL
*"     REFERENCE(IV_METHOD) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     VALUE(EV_ANY) TYPE  ANY
*"     VALUE(EV_PRG) TYPE  STRING
*"  TABLES
*"      ET_RETURN STRUCTURE  ZSAP0010 OPTIONAL
*"      IT_METHOD STRUCTURE  ZSTR1110 OPTIONAL
*"----------------------------------------------------------------------

data: lv_class  type string,
      lv_mess   type string,
      lv_prog   type string,
      lv_wrd    type string,
      lv_sid    type string,
      lv_str    type string,
      lv_col    type i,
      lv_lin    type i.
data: ls_dir    type trdir.
data: lt_tab    type table of string.
data: lo_ref    type ref   to object.
field-symbols:
      <fs_method> type zstr1110.

loop at it_method assigning <fs_method>.
  search <fs_method>-methcode for 'INSERT'.
  if sy-subrc = 0.
    perform frm_public_message_get tables et_return
                                   using  'ZMCTR0040' '000' 'E'
                                          text-107
                                          space
                                          space
                                          space.
  endif.

  search <fs_method>-methcode for 'UPDATE'.
  if sy-subrc = 0.
    perform frm_public_message_get tables et_return
                                   using  'ZMCTR0040' '000' 'E'
                                          text-107
                                          space
                                          space
                                          space.
  endif.

  search <fs_method>-methcode for 'MODIFY'.
  if sy-subrc = 0.
    perform frm_public_message_get tables et_return
                                   using  'ZMCTR0040' '000' 'E'
                                          text-107
                                          space
                                          space
                                          space.
  endif.

  search <fs_method>-methcode for 'DELETE'.
  if sy-subrc = 0.
    perform frm_public_message_get tables et_return
                                   using  'ZMCTR0040' '000' 'E'
                                          text-107
                                          space
                                          space
                                          space.
  endif.

endloop.

check et_return[] is initial.

try.

*  lt_tab = value #(
*    ( ` program.`                       )
*    ( ` class main definition.`         )
*    ( `   public section.`              )
*    ( `     methods meth `              )
*    ( `  importing value(iv) type any`  )
*    ( `  exporting value(ev) type any.` )
*    ( ` endclass.`                      )
*    ( ` class main implementation.`     )
*    ( `   method meth.`                 )
*    (       iv_code                     )
*    ( `   endmethod.`                   )
*    ( ` endclass.`                      ) ).

  if iv_prg is initial.

    check it_method[] is not initial.

    describe table it_method lines lv_col.

    lv_str = 'program.'.
    append lv_str to lt_tab.
    lv_str = 'class main definition.'.
    append lv_str to lt_tab.
    lv_str = 'public section.'.
    append lv_str to lt_tab.
    lv_str = 'class-methods:'.
    append lv_str to lt_tab.

    loop at it_method assigning <fs_method>.
      lv_lin = lv_lin + 1.

      lv_str = <fs_method>-fieldname.
      append lv_str to lt_tab.
      lv_str = 'importing value(iv) type any'.
      append lv_str to lt_tab.
      if lv_lin = lv_col.
        lv_str = 'exporting value(ev) type any.'.
      else.
        lv_str = 'exporting value(ev) type any,'.
      endif.
      append lv_str to lt_tab.
    endloop.

    lv_str = 'endclass.'.
    append lv_str to lt_tab.

    lv_str = 'class main implementation.'.
    append lv_str to lt_tab.

    loop at it_method assigning <fs_method>.
      concatenate 'method' <fs_method>-fieldname '.' into lv_str separated by space.
      append lv_str to lt_tab.
      lv_str = <fs_method>-methcode.
      append lv_str to lt_tab.
      lv_str = 'endmethod.'.
      append lv_str to lt_tab.
    endloop.

    lv_str = 'endclass.'.
    append lv_str to lt_tab.

    generate subroutine pool lt_tab name  lv_prog
             message                      lv_mess
             shortdump-id                 lv_sid.
    if sy-subrc = 0.
      ev_prg = lv_prog.
    else.
      perform frm_public_message_get tables et_return
                                     using  'ZMCTR0040' '000' 'E'
                                            lv_mess
                                            space
                                            space
                                            space.
    endif.
  else.
    lv_prog = iv_prg.

    if iv_method is not initial.
      lv_class = '\PROGRAM=' && lv_prog && '\CLASS=MAIN'.

      create object lo_ref type (lv_class).

      call method lo_ref->(iv_method)
        exporting
          iv = iv_any
        importing
          ev = ev_any.
    endif.

    ev_prg = lv_prog.
  endif.

  catch cx_root into data(lo_exc).
    lv_mess = lo_exc->get_text( ).
    perform frm_public_message_get tables et_return
                                   using  'ZMCTR0040' '000' 'E'
                                          lv_mess
                                          space
                                          space
                                          space.
    return.
endtry.

ENDFUNCTION.
View Code

 

posted on 2018-12-24 17:51  ricoo  阅读(388)  评论(0编辑  收藏  举报