【07】SAP ABAP性能优化 - 使用 Field Symbol 和 Data Reference

本文深入解析了ABAP编程中的两种复杂变量类型:FieldSymbol和DataReference。FieldSymbol类似C语言中的指针,不占用内存,只指向变量的地址空间,能提升变量操作效率。DataReference则用于动态创建未知类型的变量,如数据变量、结构或内表,配合FieldSymbol使用可实现高效灵活的编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相比较于一般的数据类型(例如 I, P, F, N, C, D, T等)的变量,Field Symbol和Data Reference类型的变量是ABAP中两个相关复杂的类型。理解和使用这两种变量,对于编写高性能、高灵活性的ABAP程序是十分重要的。

1.含义解析

  • Field Symbol:类似于C语言中的“指针”。初始状态为Unassigned。Field Symbol类型的变量自身并不对占用存储空间,仅“指向”被Assgined变量的地址空间。
  • Data Reference: 一种变量类型。初始状态为Initial。当被创建(实例化)成具体的类型时,系统会为其分配相应的存储空间。通过 ‘ ->* ’ 的方式访问,将值从地址空间传输到变量中(显示)。若要改变Data Reference变量中的值,需要配合Field Symbol使用。即使用Field Symbol指向Data Reference变量所存储的地址空间,并用Field Symbol对其进行操作。

下图给出了配合Field Symbol操作Data Reference的一个例子 -
在这里插入图片描述

2. 使用Field Symbol提升访问效率

因为Field Symbol并不会占用内存空间,因此在操作变量时,可以使用Field Symbol来提升运行效率。常用的Field Symbol的场景有以下几种 -

  • Read table < itab > assigning < fs >
  • Loop at < itab > assigning < fs >
  • Assign < data object > to < fs >
  • Assign component <component name/component index> to < fs >
  • Append initial line to < itab > assigning < fs >
**********************************************************************
* Field Symbol to prioritize the accessing of internal table
**********************************************************************

DATA: lt_sflight TYPE STANDARD TABLE OF sflight,
      ls_sflight TYPE sflight.

FIELD-SYMBOLS: <ls_sflight> TYPE sflight.

SELECT * FROM sflight INTO TABLE lt_sflight UP TO 10 ROWS.

* extra effort to copy data into variable ls_sflight
LOOP AT lt_sflight INTO ls_sflight.
  WRITE: / ls_sflight-carrid, ls_sflight-connid, ls_sflight-fldate.
ENDLOOP.

* access the address directly
LOOP AT lt_sflight ASSIGNING <ls_sflight>.
  WRITE: / <ls_sflight>-carrid, <ls_sflight>-connid, <ls_sflight>-fldate.
ENDLOOP.

3.使用Data Reference动态创建变量

使用Data Reference的主要场景是针对于unknown的变量类型,用于ABAP的动态编程。创建所需要的变量时并不能确定具体的类型(但这里并不是指“漫无边际”的不确定,需要变量/结构/内表是确定的,而所需要的具体类型是不确定的) -

  • 创建时,并无法确定具体类型的“变量 variable”
  • 创建时,并无法确定具体类型的“结构 structure”
  • 创建时,并无法确定具体类型的“内表 internal table”

下面给出了,三个不同场景的具体例子:

**********************************************************************
* 1. Un known data reference(creation of data variable)
**********************************************************************
PARAMETERS: p_var TYPE string DEFAULT 'I'.

DATA: lr_data TYPE REF TO data.
CREATE DATA lr_data TYPE (p_var).

FIELD-SYMBOLS: <fs> TYPE any.
ASSIGN lr_data->* TO <fs>.
IF sy-subrc = 0.
  <fs> = '100'.
ENDIF.

WRITE: <fs>.
**********************************************************************
* 2. Un known data reference(creation of structure)
**********************************************************************

PARAMETERS: p_var TYPE string DEFAULT 'SFLIGHT'.

DATA: lr_data TYPE REF TO data.
CREATE DATA lr_data TYPE (p_var).

FIELD-SYMBOLS: <fs> TYPE any.
ASSIGN lr_data->* TO <fs>.
IF sy-subrc = 0.
  SELECT SINGLE * FROM (p_var) INTO <fs>.
  IF sy-subrc = 0.
    FIELD-SYMBOLS: <fs_field> TYPE any.

    DO .
      ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <fs_field>.
      IF sy-subrc = 0.
        WRITE: / <fs_field>.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.

  ENDIF.
ENDIF.
**********************************************************************
* 3. Un known data reference(creation of table)
**********************************************************************

PARAMETERS: p_var TYPE string DEFAULT 'SFLIGHT'.

DATA: lr_data TYPE REF TO data.
CREATE DATA lr_data TYPE TABLE OF (p_var).

FIELD-SYMBOLS: <ft_table> TYPE ANY TABLE.

ASSIGN lr_data->* TO <ft_table>.
IF sy-subrc = 0.
  SELECT * FROM (p_var) INTO TABLE <ft_table> UP TO 10 ROWS.
  IF sy-subrc = 0.

    cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lr_salv)
                            CHANGING t_table      = <ft_table> ).
    lr_salv->display( ).

  ENDIF.
ENDIF.
### SAP Field Symbols in ABAP Programming Field symbols in ABAP represent placeholders that can point to any data object during runtime without occupying storage space themselves. These symbols allow dynamic referencing of variables or fields within a program, enhancing flexibility and efficiency. A field symbol must be declared with its type before being used. The declaration specifies whether the field symbol will reference elementary types, structured types, internal tables, etc., but not which specific variable it points to initially. This is done using the `ASSIGN` statement at runtime[^1]. The syntax for declaring a field symbol includes specifying its name preceded by `<`, followed by `TYPE` along with either a predefined ABAP type like `I` (integer), `STRING`, structure components, table work areas, or even generic references such as `ANY`. For example: ```abap FIELD-SYMBOLS: <fs_string> TYPE string, <fs_table> TYPE STANDARD TABLE OF spfli. ``` To assign a value to a field symbol, one uses the `ASSIGN` command followed by the target variable's address (`&`) or directly when dealing with static identifiers. Once assigned, operations performed on the field symbol affect the actual referenced memory location rather than creating new copies of data objects. Using field symbols offers several advantages including improved performance due to reduced copying overheads; increased code readability through abstracting away complex structures into simpler aliases; easier maintenance since changes only need to occur once where assignments take place instead of multiple hard-coded locations throughout the source listing[^2]. However, caution should always accompany their application because improper handling may lead to unexpected behavior if pointers become invalid after certain modifications elsewhere in the calling context—such scenarios could result from deleting records pointed-to by these symbolic links while they remain active inside loops iterating over collections containing them[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十年铸器

给作者赏杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值