DATA:lv_formname TYPE string,
lv_program TYPE program.
PERFORM (lv_formname) IN PROGRAM (lv_program) IF FOUND.
PS: ABAP子例程的动态调用不仅可以调用当前主程序及子程序中的FORM,也可以动态指定程序调用,生成的动态子例程就用该方式调用。注意form名称及程序名必须大写。
1.2 ABAP方法动态调用
DATA: lo_object TYPE REF TO object,
lv_class TYPE seoclsname,
lv_method TYPE string,
lt_parameter TYPE abap_parmbind_tab,
ls_parameter TYPE abap_parmbind,
lt_exception TYPE abap_excpbind_tab,
ls_exception TYPE abap_excpbind.
lv_class = 'CL_DEMO_TEXT'.
lv_method = 'DISPLAY_STRING'.
lt_parameter = VALUE #( ( name = 'TEXT_STRING' kind = 'E' value = REF #( `CL_DEMO_TEXT=>DISPLAY_STRING` ) ) ).
lt_exception = VALUE #( ( name = 'OTHERS' value = 9 ) ).
TRY.
CALL METHOD (lv_class)=>(lv_method) PARAMETER-TABLE lt_parameter EXCEPTION-TABLE lt_exception.
CATCH cx_root.
ENDTRY.
CALL METHOD (meth_name)
| cref->(meth_name)
| iref->(meth_name)
| (class_name)=>(meth_name)
| class=>(meth_name)
| (class_name)=>meth
PS:示例为静态方法的调用,实例方法需要动态创建对象后调用,使用方式同理,另外function动态调用时参数(参数字段KIND: E,I,C,R 分别对应EXPORTING, IMPORTING, CHANGING, RECEIVING)的处理与方法一致,不再说明。注意类名和方法名需要大写。
DATA: lt_data TYPE TABLE OF t001.
DATA: lv_from TYPE string,
lv_field TYPE string,
lv_where TYPE string.
lv_from = 'T001'.
lv_field = 'BUKRS BUTXT'.
lv_where = `BUKRS = '0001'`.
SELECT (lv_field)
INTO CORRESPONDING FIELDS OF TABLE lt_data
FROM (lv_from)
WHERE (lv_where).
DATA: lr_result TYPE REF TO data.
DATA: lo_structdescr TYPE REF TO cl_abap_datadescr.
TRY.
" 需要执行的SQL语句字符串
DATA(lv_statement) = `SELECT * FROM T001 WHERE BUKRS = '0001'`.
DATA(lo_db_connection) = cl_sql_connection=>get_connection( cl_sadl_dbcon=>get_default_dbcon( ) ).
DATA(lo_result) = lo_db_connection->create_statement( )->execute_query( statement = lv_statement ).
DATA(lt_md_data) = lo_result->get_metadata( ).
DATA(lr_metadata) = lo_result->get_struct_ref( md_tab = lt_md_data
p_strict = abap_false ).
lo_structdescr ?= cl_abap_typedescr=>describe_by_data_ref( lr_metadata ).
DATA(lo_tabletype) = cl_abap_tabledescr=>create( p_line_type = lo_structdescr
p_table_kind = cl_abap_tabledescr=>tablekind_std ).
CREATE DATA lr_result TYPE HANDLE lo_tabletype.
* finally save the result_set in the handled tabletype
lo_result->set_param_table( itab_ref = lr_result ) .
lo_result->next_package( ).
lo_db_connection->close( ).
CATCH cx_sql_exception.
ENDTRY.
PS: 这里的SQL语句为底层数据库SQL,而不是OPEN SQL.
SORT itab BY (comp1)...(compn)
READ TABLE itab WITH KEY(k1)=v1...(kn)=vn
READ TABLE itab...INTOwaCOMPARING(comp1)...(compn) TRANSPORTING(comp1)...
MODIFY [TABLE] itab TRANSPORTING(comp1)...(compn)
DELETE TABLEitabWITH TABLE KEY(comp1)...(compn)
DELETE ADJACENT DUPLICATES FROM itab COMPARING(comp1)...(compn)
AT NEW/END OF (comp)
CREATE DATA ... TYPE (type)...
DATA: a TYPE REF TO i.
CREATE DATA a TYPE ('I').
a->* = 1.
FIELD-SYMBOLS:<fs>.
DATA:str(20) TYPE c VALUE 'Output String',
name(20) TYPE c VALUE 'STR'.
"静态分配:编译时就知道要分配的对象名
ASSIGN name TO <fs>."结果是<fs>与name变量等同
"通过变量名动态访问变量
ASSIGN (name) TO <fs>."结果是是<fs>的值为str变量值
DATA: BEGIN OF line,
col1 TYPE i VALUE '11',
col2 TYPE i VALUE '22',
col3 TYPE i VALUE '33',
END OF line.
DATA comp(5) VALUE 'COL3'.
FIELD-SYMBOLS: <f1>, <f2>, <f3>.
ASSIGN line TO <f1>.
ASSIGN comp TO <f2>.
"还可以直接使用以下的语法访问其他程序中的变量
ASSIGN ('(ZJDEMO)SBOOK-FLDATE') TO <fs>.
"通过索引动态的访问结构成员
ASSIGN COMPONENT sy-index OF STRUCTURE <f1> TO <f3>.
"通过字段名动态的访问结构成员
ASSIGN COMPONENT <f2>OF STRUCTURE <f1> TO <f3>.
"如果定义的内表没有组件名时,可以使用索引为0的组件来访问这个无名字段(注:不是1)
ASSIGN COMPONENT 0 OF STRUCTURE itab TO <fs>.
ASSIGN oref->('attr') TO <attr>.
ASSIGN oref->('static_attr') TO <attr>.
ASSIGN ('C1')=>('static_attr') TO <attr>.
ASSIGN c1=>('static_attr') TO <attr>.
ASSIGN ('C1')=>static_attr TO <attr>.