下面ABAP语句允许你调用一个可执行程序或者事务码,调用后你可以直接退出主调程序,或者返回到调用被调程序的地方继续后继代码的执行:
可执行Program Transaction
Call (without returning) SUBMIT LEAVE TO TRANSACTION
Call and return SUBMIT AND RETURN CALL TRANSACTION
SUBMIT
如果省略AND RETURN选项,主调程序的所有数据与所有级别的List都会从 internal session 中删除。在被调程序执行完后,会返回到主调程序启动的地方。
如果带AND RETURN选项,系统将会保持主调程序的所有数,并在被调程序执行完后返回到主调程序调用处(SUBMIT…AND RETURN语句调用处),然后系统会继续执行主调程序SUBMIT…AND RETURN后面的语句。
当正常执行一个报表程序时,标准的选择屏幕会显示,这些屏幕选择条件可以是程序自己本身的,也可以是绑定的LDB的屏幕字段。当使用SUBMIT调用时,可以通过各种参数选项来给这些屏幕选择条件填充值:
SUBMIT... [VIA SELECTION-SCREEN]
[USING SELECTION-SET ]
[WITH
[WITH FREE SELECTIONS
[WITH SELECTION-TABLE
VIA SELECTION-SCREEN
被调报表程序的选择屏幕会显示。如果此选择打开,并且还使用了其他参数选项来传输值时,这些值也会显示在屏幕中相应的输入框中,并且用户可以进一步修改这些值。在默认的情况下,通过SUBMIT调用时是不会显示可执行程序的选择屏幕的。
USING SELECTION-SET
使用变式。This option tells the system to start the called program with the variant .
WITH
使用此选择可以对选择屏幕的某个选择条件
l ], 用来传输屏幕选择条件的单个值
如果去填充选择内表
如果
l [NOT] BETWEEN ], 用来传递范围取值
会传递给选择内表
l IN
该选项会将主调程序中的条件内表
WITH FREE SELECTION
To use this option, both calling and called programs must be connected to a logical
database that supports dynamic selections. In the calling program, use the function
modules FREE_SELECTIONS_INIT and FREE_SELECTIONS_DIALOG. They allow the
user to enter dynamic selections on a selection screen. One export parameter of these
function modules has structure RSDS_TEXPR from the RSDS type group. Transfer the
values of this export parameter by means of the internal table
structure to the called report.
要使用此选项,必须将调用报表和被调用报表都连接到支持动态选择的逻辑数据库。在调用报表中,使用功能模块 FREE-SELECTIONS_INIT 和 FREE_SELECTIONS_DIALOG,它们允许用户在选择屏幕上输入动态选择。这些功能块的一个输出参数有 RSDS 类型组中的 RSDS_TEXPR 结构。通过相同结构的内部表
WITH SELECTION-TABLE
SELNAME:SELECT-OPTION 或者 PARAMETER屏幕字段名称
KIND:如果是SELECT-OPTION时,取值为S,如果PARAMETER时,取值为P。
其他字段与SELECT-OPTION选择条内表行结构相同。
除了WITH SELECTION-TABLE以外,WITH
REPORT zjzj_rep2 NO STANDARD PAGE HEADING.
DATA: int TYPE i,
rspar LIKE rsparams OCCURS 10 WITH HEADER LINE.
RANGES seltab FOR int.
WRITE: 'Select a Selection!',
/ '--------------------'.
SKIP.
FORMAT HOTSPOT COLOR 5 INVERSE ON.
WRITE: 'Selection 1',
/ 'Selection 2'.
AT LINE-SELECTION.
CASE sy-lilli."选中的行号
WHEN 4.
seltab-sign = 'I'.
seltab-option = 'BT'.
seltab-low = 1.
seltab-high = 5.
APPEND seltab.
SUBMIT zjzj_rep1 VIA SELECTION-SCREEN
WITH paramet EQ 'Selection 1'
WITH selecto IN seltab
WITH selecto NE 3
AND RETURN.
WHEN 5.
rspar-selname = 'SELECTO'.
rspar-kind = 'S'.
rspar-sign = 'E'.
rspar-option = 'BT'.
rspar-low = 14.
rspar-high = 17.
APPEND rspar.
rspar-selname = 'PARAMET'.
rspar-kind = 'P'.
rspar-low = 'Selection 2'.
APPEND rspar.
rspar-selname = 'SELECTO'.
rspar-kind = 'S'.
rspar-sign = 'I'.
rspar-option = 'GT'.
rspar-low = 10.
APPEND rspar.
SUBMIT zjzj_rep1 VIA SELECTION-SCREEN
WITH SELECTION-TABLE rspar
AND RETURN.
ENDCASE.
REPORT ZJZJ_REP1.
DATA number TYPE i.
PARAMETERS paramet(14).
SELECT-OPTIONS selecto FOR number.
REPORT yjzj_call_prog_9_6_2.
TABLES: mard.
DATA: BEGIN OF gx_mard OCCURS 0,
matnr LIKE mard-matnr,
werks LIKE mard-werks,
lgort LIKE mard-lgort,
labst LIKE mard-labst,
insme LIKE mard-insme,
END OF gx_mard,
gt_mard LIKE TABLE OF gx_mard.
CONSTANTS: g_memory(30) VALUE 'JACKYTEST'.
PARAMETERS: p_werks LIKE mard-werks OBLIGATORY.
SELECT-OPTIONS: s_matnr FOR mard-matnr.
PARAMETERS: p_qty AS CHECKBOX.
PARAMETERS: p_type NO-DISPLAY DEFAULT '1'.
START-OF-SELECTION.
DATA: gt_tabsel LIKE rsparams OCCURS 10 WITH HEADER LINE.
IF NOT s_matnr[] IS INITIAL.
LOOP AT s_matnr.
"selection类型参数需要封装到rsparams类型的内表中才能传递
MOVE-CORRESPONDING s_matnr TO gt_tabsel.
"被调用程序中的selection-option参数
gt_tabsel-selname = 'S_MATNR'.
gt_tabsel-kind = 'S'.
APPEND gt_tabsel.
CLEAR gt_tabsel.
ENDLOOP.
ENDIF.
SUBMIT yjzj_call_prog_9_6 WITH p_werks = p_werks
WITH p_qty = p_qty
WITH p_type = 'X'
"传递selection类型的参数到被调用程序中
WITH SELECTION-TABLE gt_tabsel
AND RETURN.
"从被调用程序中导入结果
IMPORT gt_mard TO gt_mard FROM MEMORY ID g_memory.
FREE MEMORY ID g_memory.
LOOP AT gt_mard INTO gx_mard.
WRITE:/02 gx_mard-matnr,
gx_mard-werks,
gx_mard-lgort,
gx_mard-labst,
gx_mard-insme.
ENDLOOP.
REPORT yjzj_call_prog_9_6.
TABLES: mard.
DATA: BEGIN OF gx_mard OCCURS 0,
matnr LIKE mard-matnr,
werks LIKE mard-werks,
lgort LIKE mard-lgort,
labst LIKE mard-labst,
insme LIKE mard-insme,
END OF gx_mard,
gt_mard LIKE TABLE OF gx_mard.
CONSTANTS: g_memory(30) VALUE 'JACKYTEST'.
PARAMETERS: p_werks LIKE mard-werks OBLIGATORY.
SELECT-OPTIONS: s_matnr FOR mard-matnr.
PARAMETERS: p_qty AS CHECKBOX.
PARAMETERS: p_type NO-DISPLAY.
START-OF-SELECTION.
SELECT matnr werks lgort labst insme
INTO CORRESPONDING FIELDS OF TABLE gt_mard FROM mard WHERE
matnr IN s_matnr AND
werks EQ p_werks.
DATA: g_tabix TYPE i.
IF p_qty NE space.
LOOP AT gt_mard INTO gx_mard.
g_tabix = sy-tabix.
gx_mard-labst = gx_mard-labst + gx_mard-insme.
IF gx_mard-labst <= 0.
DELETE gt_mard INDEX g_tabix.
CONTINUE.
ENDIF.
ENDLOOP.
ENDIF.
IF p_type NE space.
FREE MEMORY ID g_memory.
"将结果导出到主调用程序
EXPORT gt_mard FROM gt_mard TO MEMORY ID g_memory.
ELSE.
LOOP AT gt_mard INTO gx_mard.
WRITE:/02 gx_mard-matnr,
gx_mard-werks,
gx_mard-lgort,
gx_mard-labst,
gx_mard-insme.
ENDLOOP.
ENDIF.
当你使用SUBMIT提交一个ABAP program时,你可以控制这个被调程序的输出列表:修改输出行列长度格式、输出到spool file来代替输出到屏幕、或者是将输出列表存储到ABAP memory中。
SUBMIT... [LINE-SIZE
如果被调程序的REPORT语句后面没有这LINE-SIZE与LINE-COUNT两个选项,系统将会使用SUBMIT语句后面的这两个选项来格式化输出列表。
REPORT zjzj_rep2 NO STANDARD PAGE HEADING.
DATA: NAME(9) VALUE 'ZJZJ_REP1',
WID TYPE I VALUE 80,
LEN TYPE I VALUE 0.
SET PF-STATUS 'SELECT'.
WRITE: 'Select a report and its list format:',
/ '-------------------------------------'.
SKIP.
WRITE: 'Report ', NAME INPUT ON,
/ 'Line size ', WID INPUT ON,
/ 'Page length', LEN INPUT ON.
AT USER-COMMAND.
CASE SY-UCOMM.
WHEN 'SELE'.
READ LINE: 4 FIELD VALUE NAME,
5 FIELD VALUE WID,
6 FIELD VALUE LEN.
SUBMIT (NAME) LINE-SIZE WID LINE-COUNT LEN AND RETURN.
ENDCASE.
SUBMIT... TO SAP-SPOOL
可以将被调程序的输出列表直接输出到spool system,而不是显示在屏幕上。
可以使用EXPORTING LIST TO MEMORY选项将被调程序输出列表保存到ABAP内存中(而不是直接显示在屏幕上),供被调程序执行完后,主调程序来使用输出列表:
SUBMIT... AND RETURN EXPORTING LIST TO MEMORY.
将被调程序的输出列表存储到ABAP内存中,可以在被调程序执行完后,主调程序还可以访问它。EXPORTING LIST TO MEMORY选项需要与AND RETURN选项一起使用。并且不能将EXPORTING LIST TO MEMORY选项与TO SAP-SPOOL选项一起使用。
输出列表存到内存里时,会以行类型为ABAPLIST的内表形式保存,可以使用下面这几个Function来访问ABAP内在中保存的输出列表(这些函数都是属于function group SLST):
LIST_FROM_MEMORY:从ABAP Memory中将ListsLoad到row type ABAPLIST的内表中
WRITE_LIST:将行类型为ABAPLIST 的内表中的内容插入到当前输出列表中.
DISPLAY_LIST:将行类型为ABAPLIST 的内表中的内容显示在独立的list screen中
LIST_TO_ASCI:将行类型为ABAPLIST 的内表中的内容转换ASCII形式
DATA list_tab TYPE TABLE OF abaplist.
SUBMIT report EXPORTING LIST TO MEMORY
AND RETURN.
"从ABAP内存中加载缓存的LIST
CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = list_tab
EXCEPTIONS
not_found = 1
OTHERS = 2.
"在当前屏幕中显示上面加载的LIST
IF sy-subrc = 0.
CALL FUNCTION 'WRITE_LIST'
TABLES
listobject = list_tab.
ENDIF.
在通常情况下,我们在被调程序的基础输出列表(Level为0的List屏幕)按F3就会退出该程序。但是,在返回到主调程序之前,在被调程序里需要执行更多的语句,比如需要使用EXPORT语句将被调程序中的数据存储到ABAP memory中便于主调程序使用时,像这样,你可以为Back Function自定义Function Code(不要使用默认的BACK,即要自己重写默认的BACK Function Code),并且在AT USER-COMMAND事件块里对相应的Function Code进行处理,当你将数据存储起来之后,就可以使用LEAVE(因为重写了默认的BACK Function Code,所以需要使用LEAVE语句来以编程方式退出程序)语句来退出当前被调用的程序了:
REPORT ZJZJ1 NO STANDARD PAGE HEADING.
DATA: ITAB TYPE I OCCURS 10,
NUM TYPE I.
SUBMIT zjzj2 AND RETURN.
IMPORT ITAB FROM MEMORY ID 'HK'.
LOOP AT ITAB INTO NUM.
WRITE / NUM.
ENDLOOP.
TOP-OF-PAGE.
WRITE 'Report 1'.
ULINE.
注:不要将Function定义成默认的BACK了,否则不会被AT USER-COMMAND事件块捕获到,会使用默认的回退功能。
REPORT ZJZJ2 NO STANDARD PAGE HEADING.
DATA: NUMBER TYPE I,
ITAB TYPE I OCCURS 10.
SET PF-STATUS 'MYBACK'.
DO 5 TIMES.
NUMBER = SY-INDEX.
APPEND NUMBER TO ITAB.
WRITE / NUMBER.
ENDDO.
TOP-OF-PAGE.
WRITE 'Report 2'.
ULINE.
AT USER-COMMAND.
CASE SY-UCOMM.
WHEN 'MBCK'.
EXPORT ITAB TO MEMORY ID 'HK'."退出前执行一些语句
LEAVE."离开当前被调程序,返回到主调程序
ENDCASE.
二种方法调用Tcode:
1、 如果调用后不需要返回到主调程序,则可以使用下面这种方式:
LEAVE TO TRANSACTION
该语句会结束当前主调程序去执行事务码
2、 如果调用后还要返回到主调程序,则使用下面这种方式:
CALL TRANSACTION
系统会重新开启一个internal session,当被调程序结束后,被调Tcode所在的这个internal session会被delete掉,然后返回到主调程序调用处,继续运行主调程序后面的语句
LEAVE PROGRAM.
退出整个程序,并删除所在内部会话、包括加载的程序、实例、数据。
Passing Data Between Programs
原文链接:www.cnblogs.com/jiangzhengjun