SAP ABAP

参考英文原版教程请移步 Tutorialspoint

一、声明语句

Each variable in ABAP has a specific type, which determines

  1. Size and layout of the variable’s memory
  2. Range of values that can be stored within that memory
  3. Set of operations that can be applied to the variable
Tag Description
f Name (MAX 30)
type Data Type
val Initial value

Example

1
2
3
DATA d1(2) TYPE C.
DATA d2 LIKE d1.
DATA minimum_value TYPE I VALUE 10.

ABAP共包含以下三种变量类型

  1. Static 静态变量
  2. Reference 引用变量
  3. System 系统变量

二、Static Variables 静态变量

Delared in subroutines, function modules, and static methods

Statement Discription
CLASS-DATA Declare variables within the classes
PARAMETERS Declare the elementary data objects that are linked to input fields on a selection screen
SELECT-OPTIONS Declare the internal tables that are linked to input fields on a selection screen
  1. 不可使用特殊字符t ,
  2. 预定义的对象名称不可更改
  3. 变量名称不可与ABAP关键字相同
  4. 变量名称须表达真实含义
  5. 避免使用连字符 -
  6. 可使用下划线 _ 分隔连词
1
2
3
4
5
REPORT ZTest123_01.
PARAMETERS: NAME(10) TYPE C,
CLASS TYPE I,
SCORE TYPE P DECIMALS 2,
CONNECT TYPE MARA-MATNR.

三、Reference Variables 引用变量

1
DATA <ref> TYPE REF TO <type> VALUE IS INITIAL.
Statement Description
REF TO Declares a reference
Type Specifies the static type of the reference variable
The static type Restricts the set of objects to which can refer
The dynamic type Data type or class to which it currently refers
  1. The static type is always more general or the same as the dynamic type.
  2. The TYPE addition is used to create a bound reference type and as a start value, and only IS INITIAL can be specified after the VALUE addition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//定义类C1
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA Bl TYPE I VALUE 1.
ENDCLASS.
//此处声明变量
DATA: Oref TYPE REF TO C1 ,
Dref1 LIKE REF TO Oref,
Dref2 TYPE REF TO I .
//创建Oref实例
CREATE OBJECT Oref.
GET REFERENCE OF Oref INTO Dref1.
//创建Dref2实例
CREATE DATA Dref2.
Dref2->* = Dref1->*->Bl.
  1. In the above code snippet, an object reference Oref and two data reference variables Dref1 and Dref2 are declared.
  2. Both data reference variables are fully typed and can be dereferenced using the dereferencing operator →* at operand positions.

四、System Variables 系统变量

  1. 所有ABAP程序均可访问 ABAP system variables are accessible from all ABAP programs.
  2. 由运行时环境赋值 These fields are actually filled by the run-time environment.
  3. 系统变量可在任何时间获取,并指示系统状态 The values in these fields indicate the state of the system at any given point of time.
  4. SAP的SYST表中记录所有系统变量 You can find the complete list of system variables in the SYST table in SAP.
  5. 使用前缀访问独立字段 Individual fields of the SYST structure can be accessed by using either “SYST-” or “SY-”.
1
2
3
4
5
6
7
8
9
10
11
12
13
REPORT Z_Test123_01.
WRITE:/'SY-ABCDE', SY-ABCDE,
/'SY-DATUM', SY-DATUM,
/'SY-DBSYS', SY-DBSYS,
/'SY-HOST ', SY-HOST,
/'SY-LANGU', SY-LANGU,
/'SY-MANDT', SY-MANDT,
/'SY-OPSYS', SY-OPSYS,
/'SY-SAPRL', SY-SAPRL,
/'SY-SYSID', SY-SYSID,
/'SY-TCODE', SY-TCODE,
/'SY-UNAME', SY-UNAME,
/'SY-UZEIT', SY-UZEIT.

以上程序输出如下

1
2
3
4
5
6
7
8
9
10
11
12
SY-ABCDE ABCDEFGHIJKLMNOPQRSTUVWXYZ
SY-DATUM 22.07.2016
SY-DBSYS HDB
SY-HOST tth
SY-LANGU EN
SY-MANDT 300
SY-OPSYS Linux
SY-SAPRL 740
SY-SYSID TTS
SY-TCODE SE38
SY-UNAME SAP_ABEL
SY-UZEIT 17:03:53