ABAP Help Document(13):6.Program调用与跳转

6.Calling and leaving program units

6.1Calling ABAP Programs

1.SUBMIT关键词

示例:

REPORT ZTOM_LEANING2.
"SUBMIT
"语法:
*SUBMIT {rep|(name)} [selscreen_options]
*                    [list_options]
*                    [job_options]
*                    [AND RETURN].
"[selscreen_options]:
*    [USING SELECTION-SCREEN dynnr]
*    [VIA SELECTION-SCREEN]
*    [selscreen_parameters]
"[USING SELECTION-SCREEN dynnr]:指定调用Executable Program,不知道默认1000
"[VIA SELECTION-SCREEN]:display selection screen,否则后台执行selection screen.

"[selscreen_parameters]:
*    [USING SELECTION-SET variant]
*    [USING SELECTION-SETS OF PROGRAM prog]
"使用variant,使用不同program variant
*    [WITH SELECTION-TABLE rspar]
"rspar的standard表结构RSPARAMS or RSPARAMSL_255
*    [WITH expr_syntax1 WITH expr_syntax2 ...]
"单个值:sel {EQ|NE|CP|NP|GT|GE|LT|LE} dobj [SIGN sign]
"范围值:sel [NOT] BETWEEN dobj1 AND dobj2 [SIGN sign]
"Range类型表:sel IN rtab
*    [WITH FREE SELECTIONS texpr]

"[list_options]
*    [LINE-SIZE width]
*    [LINE-COUNT page_lines]
*    { [EXPORTING LIST TO MEMORY]
*    | [TO SAP-SPOOL spool_options] }
"示例:DEMO_PROGRAM_SUBMIT_LINE
"spool_options:
*  SPOOL PARAMETERS pri_params
*  [ARCHIVE PARAMETERS arc_params]
*  WITHOUT SPOOL DYNPRO...

"[job_options]
*[USER user] VIA JOB job NUMBER n...

"定义屏幕
SELECTION-SCREEN BEGIN OF SCREEN 1200.
PARAMETERS: params  RADIOBUTTON GROUP grp1.
PARAMETERS: params1 RADIOBUTTON GROUP grp1.
SELECTION-SCREEN END OF SCREEN 1200.

CLASS demo_submit DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.
CLASS demo_submit IMPLEMENTATION.
  METHOD main.
    DATA: lr_seltab    TYPE RANGE OF i,
          ls_selection LIKE LINE OF lr_seltab,
          int       TYPE i,
          lt_rspar     TYPE TABLE OF rsparams,
          wa_rspar  LIKE LINE OF lt_rspar.
    "调用屏幕
    CALL SELECTION-SCREEN 1200 STARTING AT 10 10.
    IF sy-subrc <> 0.
      RETURN.
    ENDIF.
    IF params = 'X'.
      wa_rspar-selname = 'SELECTO'.
      wa_rspar-kind = 'S'.
      wa_rspar-sign = 'E'.
      wa_rspar-option = 'BT'.
      wa_rspar-low  = 14.
      wa_rspar-high = 17.
      APPEND wa_rspar TO lt_rspar.
      wa_rspar-selname = 'SELECTO'.
      wa_rspar-kind = 'S'.
      wa_rspar-sign = 'I'.
      wa_rspar-option = 'GT'.
      wa_rspar-low  = 10.
      APPEND wa_rspar TO lt_rspar.
      wa_rspar-selname = 'PARAMET'.
      wa_rspar-kind = 'P'.
      wa_rspar-low  = 'RSPARAMS'.
      APPEND wa_rspar TO lt_rspar.
      "使用Selection table
      SUBMIT ZTOM_LEANING3
             VIA SELECTION-SCREEN
             WITH SELECTION-TABLE lt_rspar
             AND RETURN.
    ELSEIF params1 = 'X'.
      ls_selection-sign = 'I'.
      ls_selection-option = 'BT'.
      ls_selection-low  = 1.
      ls_selection-high   = 5.
      APPEND ls_selection TO lr_seltab.
      "使用with + 参数名
      SUBMIT ZTOM_LEANING3
             VIA SELECTION-SCREEN
             WITH paramet EQ 'WITH EXPR'
             WITH selecto IN lr_seltab
             WITH selecto NE 3
             AND RETURN.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo_submit=>main( ).

"EXPORTING LIST TO MEMORY
"必须和AND RETURN配合
FORM f_export_list.
  DATA list_tab TYPE TABLE OF abaplist.
  SUBMIT report EXPORTING LIST TO MEMORY
                AND RETURN.
  CALL FUNCTION 'LIST_FROM_MEMORY'
    TABLES
      listobject = list_tab
    EXCEPTIONS
      not_found  = 1
      OTHERS     = 2.
  IF sy-subrc = 0.
    CALL FUNCTION 'WRITE_LIST'
      TABLES
        listobject = list_tab.
  ENDIF.
ENDFORM.

"spool options
FORM f_spool_opt.
  DATA: print_parameters TYPE pri_params,
       archi_parameters TYPE arc_params,
      valid_flag       TYPE c LENGTH 1.
  "获取print参数
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      report  = 'SUBMITABLE'
      archive_mode = '3'
    IMPORTING
      out_parameters = print_parameters
      out_archive_parameters = archi_parameters
      valid  = valid_flag
    EXCEPTIONS
      invalid_print_params  = 2
      OTHERS         = 4.
  IF valid_flag = 'X' AND sy-subrc = 0.
    SUBMIT submitable TO SAP-SPOOL
          SPOOL PARAMETERS print_parameters
     ARCHIVE PARAMETERS archi_parameters
     WITHOUT SPOOL DYNPRO.
  ENDIF.
ENDFORM.

"job options
FORM f_job_opt.
  DATA:number TYPE tbtcjob-jobcount,
       name  TYPE tbtcjob-jobname VALUE 'JOB_TEST',
       print_parameters TYPE pri_params.
  ...
  "开启job
  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname      = name
    IMPORTING
      jobcount     = number
    EXCEPTIONS
      cant_create_job  = 1
      invalid_job_data = 2
      jobname_missing  = 3
      OTHERS      = 4.
  IF sy-subrc = 0.
    SUBMIT submitable TO SAP-SPOOL
      SPOOL PARAMETERS print_parameters
       WITHOUT SPOOL DYNPRO
      VIA JOB name NUMBER number
      AND RETURN.
    IF sy-subrc = 0.
      "关闭job
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount        = number
          jobname        = name
          strtimmed       = 'X'
        EXCEPTIONS
          cant_start_immediate = 1
          invalid_startdate   = 2
          jobname_missing    = 3
          job_close_failed   = 4
          job_nosteps      = 5
          job_notex       = 6
          lock_failed      = 7
          OTHERS        = 8.
      IF sy-subrc <> 0.
        ...
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.

2.Call Transaction

示例:

"Calling Transactions
"1.CALL TRANSACTION
"语法:
*CALL TRANSACTION ta { [AND SKIP FIRST SCREEN]
*  | [USING bdc_tab [bdc_options]] }.
"bdc_tab类型BDCDATA
"[bdc_options]:
*{ {[MODE mode] [UPDATE upd]}
*  |[OPTIONS FROM opt]}
*[MESSAGES INTO itab]
"MODE:
"'A',screen display;
"'E',screen display only error occurs;
"'N',process without screen display;
"'P',process without screen display,debug;
"UPDATE:
"'A',异步更新,commit work;
"'S',同步更新,commit work and wait;
"'L',本地更新,SET UPDATE TASK LOCAL;
"OPTIONS,表类型CTU_PARAMS
"包含mode,update等设置字段
"2.LEAVE TO TRANSACTION
"语法:
*LEAVE TO { {TRANSACTION ta} | {CURRENT TRANSACTION} } 
*         [AND SKIP FIRST SCREEN]. 
FORM f_call_trans.
  "订单number
  DATA:lv_order TYPE vbak-vbeln.
  "transaction call,spa/gpa parameters
  SET PARAMETER ID 'AUN' FIELD lv_order.
  CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.

  "transaction call,bdc table
  DATA class_name TYPE c LENGTH 30 VALUE 'CL_SPFLI_PERSISTENT'.
  DATA: bdcdata_wa  TYPE bdcdata,
        bdcdata_tab TYPE TABLE OF bdcdata.
  DATA opt TYPE ctu_params.
  CLEAR bdcdata_wa.
  bdcdata_wa-program  = 'SAPLSEOD'.
  bdcdata_wa-dynpro   = '1000'.
  bdcdata_wa-dynbegin = 'X'.
  APPEND bdcdata_wa TO bdcdata_tab.
  CLEAR bdcdata_wa.
  bdcdata_wa-fnam = 'BDC_CURSOR'.
  bdcdata_wa-fval = 'SEOCLASS-CLSNAME'.
  APPEND bdcdata_wa TO bdcdata_tab.
  CLEAR bdcdata_wa.
  bdcdata_wa-fnam = 'SEOCLASS-CLSNAME'.
  bdcdata_wa-fval = class_name.
  APPEND bdcdata_wa TO bdcdata_tab.
  CLEAR bdcdata_wa.
  bdcdata_wa-fnam = 'BDC_OKCODE'.
  bdcdata_wa-fval = '=WB_DISPLAY'.
  APPEND bdcdata_wa TO bdcdata_tab.
  opt-dismode = 'E'.
  opt-defsize = 'X'.
  CALL TRANSACTION 'SE24' USING bdcdata_tab OPTIONS FROM opt. 
ENDFORM.

6.2Calling Processing Blocks

  Procedure可以内部,外部调用。

 

  内部调用:定义Class的可见methods;Function Group中Function Modules;Subroutine子程序;

  外部调用:Global Class,public class全局调用;protected子类调用;private本身和友类调用;Function Module;

  Method Calls:静态调用,动态调用;

静态调用:

meth( ... ) - standalone method call

meth( ... ) - functional method call

...meth1( ... )->meth2( ... )->... - method chaining

动态调用:

CALL METHOD

示例:

"静态方法调用
*{ static_meth( )
*| static_meth( a )
*| static_meth( p1 = a1 p2 = a2 ... )
*| static_meth( [EXPORTING  p1 = a1 p2 = a2 ...]
*    { [IMPORTING  p1 = a1 p2 = a2 ...]
*      [CHANGING   p1 = a1 p2 = a2 ...] }
*    | [RECEIVING  r  = a]
*      [EXCEPTIONS [exc1 = n1 exc2 = n2 ...]
*      [OTHERS = n_others]] ) }
"类方法调用
"同一个类方法可以直接meth( )调用或me->meth( )
*meth | oref->meth | class=>meth | super->meth | super->constructor
"Functional Method Call
"这种method只有输入参数,使用return定义返回值
*{ static_meth( )
*| static_meth( a )
*| static_meth( p1 = a1 p2 = a2 ... ) }
"链式方法调用
"返回值为类对象引用
*{ static_meth( ... )->meth1( ... )->meth2( ... )->...->meth( ... )}
*| { static_meth( ... )->meth1( ... )->meth2( ... )->...->attr }
"动态方法调用
"语法:
*CALL METHOD dynamic_meth { parameter_list
*    | parameter_tables }.
"dynamic_meth:
* (meth_name) 
*| cref->(meth_name) 
*| iref->(meth_name) 
*| (class_name)=>(meth_name) 
*| (class_name)=>meth 
*| class=>(meth_name)
"parameter
*[PARAMETER-TABLE ptab] 
*[EXCEPTION-TABLE etab].
"ptab-name,length 30;
"ptab-kind值:类CL_ABAP_OBJECTDESCR定义const值;
"ptab-value:data reference;
"etab-name,length 30,大写;
"etab-value,i类型;
FORM f_dynamic_meth_call.
  DATA: line     TYPE c LENGTH 80,
        text_tab LIKE STANDARD TABLE OF line,
        filename TYPE string,
        filetype TYPE c LENGTH 10,
        fleng    TYPE i.
  DATA: meth  TYPE string,
        class TYPE string,
        ptab TYPE abap_parmbind_tab,
        ptab_line TYPE abap_parmbind,
        etab TYPE abap_excpbind_tab,
        etab_line TYPE abap_excpbind.
  DATA: exc_ref TYPE REF TO cx_sy_dyn_call_error,
        exc_text TYPE string.

  "调用class,method
  class    = 'CL_GUI_FRONTEND_SERVICES'.
  meth     = 'GUI_DOWNLOAD'.
  filename = 'c:\temp\text.txt'.
  filetype = 'ASC'.
  ptab_line-name = 'FILENAME'.
  ptab_line-kind = cl_abap_objectdescr=>exporting.
  GET REFERENCE OF filename INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  ptab_line-name = 'FILETYPE'.
  ptab_line-kind = cl_abap_objectdescr=>exporting.
  GET REFERENCE OF filetype INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  ptab_line-name = 'DATA_TAB'.
  ptab_line-kind = cl_abap_objectdescr=>changing.
  GET REFERENCE OF text_tab INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  ptab_line-name = 'FILELENGTH'.
  ptab_line-kind = cl_abap_objectdescr=>importing.
  GET REFERENCE OF fleng INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  etab_line-name = 'OTHERS'.
  etab_line-value = 4.
  INSERT etab_line INTO TABLE etab.
  TRY.
      CALL METHOD (class)=>(meth)
          PARAMETER-TABLE
          ptab
          EXCEPTION-TABLE
          etab.
      CASE sy-subrc.
        WHEN 1.
      ENDCASE.
    CATCH cx_sy_dyn_call_error INTO exc_ref.
      exc_text = exc_ref->get_text( ).
      MESSAGE exc_text TYPE 'I'.
  ENDTRY.
ENDFORM.

CALL FUNCTION

General Function Module Call

1. CALL FUNCTION func { parameter_list | parameter_tables }.

Registration of an Update Task Function Module

2. CALL FUNCTION update_function IN UPDATE TASK

                [EXPORTING p1 = a1 p2 = a2 ...]

                [TABLES t1 = itab1 t2 = itab2 ...].

Remote Function Call

3. CALL FUNCTION... DESTINATION ...

示例1:general function call

"Function Call
"一般function调用
"CALL FUNCTION func { parameter_list | parameter_tables }.
"function parameter
*[EXPORTING  p1 = a1 p2 = a2 ...]
*[IMPORTING  p1 = a1 p2 = a2 ...]
*[TABLES     t1 = itab1 t2 = itab2 ...]
*[CHANGING   p1 = a1 p2 = a2 ...]
*[EXCEPTIONS [exc1 = n1 exc2 = n2 ...]
*    [error_message = n_error]
*    [OTHERS = n_others] ].
"EXCEPTIONS exc1 = n1 exc2 = n2 ... OTHERS = n_others
"最多0-65535个exception;
"sy-subrc = 0,无exception,否则返回对应exception对应n值;
"使用RAISE or MESSAGE RAISING + execn,抛出具体exception;
"使用RAISE不指定exception,runtime error;
"使用MESSAGE RAISING不知道exception,抛出信息,然后程序是否执行取决于信息类型;
"S, I, or W类型,只后台log记录;
"E and A,exception信息sy-msgid, sy-msgno, sy-msgty, and sy-msgv1, ... sy-msgv4;
"X,short dumps;
"和动态method call类似
*[PARAMETER-TABLE ptab]
*[EXCEPTION-TABLE etab]
"ptab类型:ABAP_FUNC_PARMBIND_TAB
"ptab 行类型:ABAP_FUNC_PARMBIND
"ptab-name,length 30;
"ptab-kind,abap定义类型,
*- ABAP_FUNC_EXPORTING for input parameters
*- ABAP_FUNC_IMPORTING for output parameters
*- ABAP_FUNC_TABLES for table parameters
*- ABAP_FUNC_CHANGING for input/output parameters
"ptab-value,数据引用;
"ptab-tables_wa,数据引用,abap_func_tables参数的header line;
FORM f_dynamic_func_call.
  DATA: line     TYPE c LENGTH 80,
      text_tab LIKE STANDARD TABLE OF line,
      filename TYPE string,
      filetype TYPE c LENGTH 10,
      fleng    TYPE i.
  DATA: func TYPE string,
        ptab TYPE abap_func_parmbind_tab,
        ptab_line TYPE abap_func_parmbind,
        etab TYPE abap_func_excpbind_tab,
        etab_line TYPE abap_func_excpbind.
  func = 'GUI_DOWNLOAD'.
  filename = 'c:\temp\text.txt'.
  filetype = 'ASC'.

  ptab_line-name = 'FILENAME'.
  ptab_line-kind = abap_func_exporting.
  GET REFERENCE OF filename INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  ptab_line-name = 'FILETYPE'.
  ptab_line-kind = abap_func_exporting.
  GET REFERENCE OF filetype INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  ptab_line-name = 'DATA_TAB'.
  ptab_line-kind = abap_func_tables.
  GET REFERENCE OF text_tab INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  ptab_line-name = 'FILELENGTH'.
  ptab_line-kind = abap_func_importing.
  GET REFERENCE OF fleng INTO ptab_line-value.
  INSERT ptab_line INTO TABLE ptab.
  etab_line-name = 'OTHERS'.
  etab_line-value = 10.
  INSERT etab_line INTO TABLE etab.
  "动态调用func
  CALL FUNCTION func
    PARAMETER-TABLE
    ptab
    EXCEPTION-TABLE
    etab.
  CASE sy-subrc.
    WHEN 1.
  ENDCASE.
ENDFORM

示例2:call function in update task

"Call Function in update task
*CALL FUNCTION update_function IN UPDATE TASK 
*  [EXPORTING p1 = a1 p2 = a2 ...] 
*  [TABLES t1 = itab1 t2 = itab2 ...]. 

示例3:RFC

"Remote Function Call(RFC)
"同步function
*1. CALL FUNCTION func DESTINATION dest parameter_list. 
"异步function
*2. CALL FUNCTION func STARTING NEW TASK task 
* [DESTINATION {dest|{IN GROUP {group|DEFAULT}}}] 
* [{PERFORMING subr}|{CALLING meth} ON END OF TASK] 
* parameter_list. 
"background function(bgRFC)
*3. CALL FUNCTION func IN BACKGROUND UNIT parameter_list. 
"transactional function(tRFC)
*4. CALL FUNCTION func IN BACKGROUND TASK 
*   [AS SEPARATE UNIT] 
*   [DESTINATION dest] 
*   parameter_list.
"bgRFC是tRFC的增强,推荐使用

PERFORM

子程序调用

General Subroutine Call

1. PERFORM subr_identifier [parameter_list].

Registration of Subroutines

2. PERFORM subr ON { {COMMIT [LEVEL idx]} | {ROLLBACK} }.

示例:

"form
"perform调用子程序
*PERFORM subr_identifier [parameter_list].
"subr_identifier
*subr | {subr|(sname) IN PROGRAM [prog|(pname)] [IF FOUND]}
*| {n OF subr1 subr2 ...}
"[parameter_list]
*[TABLES   itab1 itab2 ...]
*[USING    a1 a2 ...]
*[CHANGING a1 a2 ...].

6.3Exiting Program Units

退出Program。

LEAVE PROGRAM

退出Process Blocks

RETURN

EXIT

CHECK

STOP

CHECK SELECT-OPTIONS (obsolete, for logical databases only)

REJECT (obsolete, for logical databases only)

示例:

"1.退出Program
"LEAVE PROGRAM.
"2.退出block
"RETURN
"EXIT
"CHECK log_exp.
"log_exp,逻辑表达式
*{ {operand1 {=|EQ|<>|NE|>|GT|<|LT|>=|GE|<=|LE
*                |CO|CN|CA|NA|CS|NS|CP|NP
*                |BYTE-CO|BYTE-CN|BYTE-CA|BYTE-NA|BYTE-CS|BYTE-NS
*                |O|Z|M} operand2 }
*    | {operand  [NOT] BETWEEN operand1 AND operand2}
*    | {<fs>     IS [NOT] ASSIGNED}
*    | {ref      IS [NOT] BOUND}
*    | {operand  IS [NOT] INITIAL}
*    | {para     IS [NOT] REQUESTED}
*    | {para     IS [NOT] SUPPLIED}
*    | {[operand [NOT] IN] seltab} }
"LOAD-OF-PROGRAM event block
"不能使用return,exit,check退出
"STOP
"只可以在下面block使用
*AT SELECTION-SCREEN (without additions)
*START-OF-SELECTION
*GET
"3.退出loops
*EXIT,退出循环
*CONTINUE,跳过本次循环
*CHECK,如果check失败,跳出循环
posted @ 2021-04-22 19:19  渔歌晚唱  阅读(354)  评论(0编辑  收藏  举报