动态生成ABAP程序-资料

Posted Rainystuday

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态生成ABAP程序-资料相关的知识,希望对你有一定的参考价值。

参考程序:

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.

 

以上是关于动态生成ABAP程序-资料的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在写ABAP程序的时候不会自动生成关键字。 像我打DATA,只要打了D后面的就都会显示出来要怎么设置

ABAP系列SAP ABAP 动态指针

ABAP 通过程序向外部邮箱发送邮件时生成的Excel附件能否设置格式?怎么设置

为什麽要用ABAP动态内表

ABAP Memory Inspector 里对动态内存对象的内存消耗度量方式

ABAP-基于MEMORY动态传参