参考程序:
Tcode ABAPdocu-->
BC - ABAP Programming-->
The ABAP Programming Language-->
Special Techniques-->
Generating Programs Dynamically.
1.仅动态生成程序代码:
REPORT demo_special_tech_dyn_insert. DATA: code TYPE TABLE OF rssource-line. * type of an editor line: rssource-line APPEND 'REPORT ZDYN1.' TO code. APPEND 'WRITE / ''Hello, I am dynamically created!''.' TO code. INSERT REPORT 'demo_special_tech_ZDYN1' FROM code. WRITE: text-001, 'demo_special_tech_ZDYN1'.
2.动态更改程序代码
REPORT demo_special_tech_dyn_read_rep . DATA: code TYPE TABLE OF rssource-line. * type of an editor line: rssource-line READ REPORT 'demo_special_tech_ZSTRUC1' INTO code. APPEND 'SKIP.' TO code. APPEND 'WRITE / ''And I am a dynamic extension!''.' TO code. INSERT REPORT 'demo_special_tech_ZDYN2' FROM code. WRITE: text-001, 'demo_special_tech_ZDYN2'.
3 动态生成 并 调用
REPORT demo_special_tech_dyn_start. DATA: code TYPE TABLE OF rssource-line. * type of an editor line: rssource-line DATA lin TYPE i. READ REPORT 'demo_special_tech_zdyn1' INTO code. SUBMIT demo_special_tech_zdyn1 AND RETURN. DESCRIBE TABLE code LINES lin. MODIFY code INDEX lin FROM 'WRITE / ''Dynamic Program Changed!''.'. INSERT REPORT 'demo_special_tech_zdyn1' FROM code. SUBMIT demo_special_tech_zdyn1.
4 生成并增加include
REPORT demo_special_tech_include_1. TYPES code_type TYPE rssource-line. * type of an editor line: rssource-line DATA code TYPE TABLE OF code_type. DATA lin TYPE i. READ REPORT 'demo_special_tech_ZINCLUD1' INTO code. DESCRIBE TABLE code LINES lin. MODIFY code INDEX lin FROM 'WRITE / ''Changed INCLUDE program!''.'. INSERT REPORT 'demo_special_tech_ZINCLUD1' FROM code. INCLUDE demo_special_tech_zinclud1.
5. 调用其他程序的子程序
REPORT demo_special_tech_include_2 . TYPES code_type(72) TYPE c. DATA code TYPE TABLE OF code_type. READ REPORT 'demo_special_tech_ZINCLUD1' INTO code. APPEND 'WRITE / ''Extension of INCLUDE program!''.' TO code. INSERT REPORT 'demo_special_tech_ZINCLUD1' FROM code. PERFORM. sub1(demo_special_tech_zform1).
6. 生成动态 临时 子程序 并调用(生成的代码有错 writ 应该是 write).
REPORT demo_special_tech_subroutine_1. 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. APPEND 'PROGRAM SUBPOOL.' TO code. APPEND 'FORM. DYN1.' TO code. APPEND 'WRITE / ''Hello, I am the temporary subroutine DYN1!''.' "#EC NOTEXT TO code. APPEND 'ENDFORM.' TO code. APPEND 'FORM. DYN2.' TO code. APPEND 'WRIT / ''Hello, I am the temporary subroutine DYN2!''.' "#EC NOTEXT TO code. APPEND 'ENDFORM.' TO code. GENERATE SUBROUTINE POOL code NAME prog MESSAGE msg LINE lin WORD wrd OFFSET off. IF sy-subrc <> 0. WRITE: / 'Error during generation in line', lin, "#EC NOTEXT / msg, / 'Word:', wrd, 'at offset', off. "#EC NOTEXT ELSE. WRITE: / 'The name of the subroutine pool is', prog. "#EC NOTEXT SKIP 2. PERFORM. dyn1 IN PROGRAM (prog). SKIP 2. PERFORM. dyn2 IN PROGRAM (prog). ENDIF. ---------------------------------------- 核心代码 : GENERATE SUBROUTINE POOL code NAME prog MESSAGE msg LINE lin WORD wrd OFFSET off. 生成 子程序池,检查代码错误. _____________________________
7. 生成动态 临时 子程序 并调用(生成的代码正确).
REPORT demo_special_tech_subroutine_2. 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. APPEND 'PROGRAM SUBPOOL.' TO code. APPEND 'FORM. DYN1.' TO code. APPEND 'WRITE / ''Hello, I am the temporary subroutine DYN1!''.' "#EC NOTEXT TO code. APPEND 'ENDFORM.' TO code. APPEND 'FORM. DYN2.' TO code. APPEND 'WRITE / ''Hello, I am the temporary subroutine DYN2!''.' "#EC NOTEXT TO code. APPEND 'ENDFORM.' TO code. GENERATE SUBROUTINE POOL code NAME prog MESSAGE msg LINE lin WORD wrd OFFSET off. IF sy-subrc <> 0. WRITE: / 'Error during generation in line', lin, "#EC NOTEXT / msg, / 'Word:', wrd, 'at offset', off. "#EC NOTEXT ELSE. WRITE: / 'The name of the subroutine pool is', prog. "#EC NOTEXT SKIP 2. PERFORM. dyn1 IN PROGRAM (prog). SKIP 2. PERFORM. dyn2 IN PROGRAM (prog). ENDIF.