在哪里使用 table 类型和结构?

Where to use table types and where structures?

为什么我们用Table输入SAP/ABAP?我们可以声明为 table 类型,如下所示。

DATA it_doc TYPE TABLE OF table_name.

如果我想存储 table 的特定属性,我可以使用结构类型。

TYPES: BEGIN OF st_student,
        sid TYPE i,
        sname TYPE c LENGTH 8,
    END OF st_student.
DATA student TYPE st_student.

table类型和结构在性能上有区别吗?

不,tables和结构实际上是非常不同的,所以你对性能的担忧有点多余。正如我在评论中所述,table 是一个元素列表。

例子

您想存储有关学校的信息 class。您的应用程序应该能够存储一名学生的姓名、生日、性别等数据。要将这些字段组合在一起,可以使用一种结构:

TYPES:
    BEGIN OF student,
        name   TYPE string,
        birth  TYPE d,
        gender TYPE char1,
   END OF student.

现在您可以声明类型为 student 的变量并像这样分配数据:

DATA stud1 TYPE student.
stud1-name = 'Joe'.
...

你现在想把学生放在一个 class 房间里。为此,您需要一个内部 table。

TYPES classroom TYPE STANDARD TABLE OF student WITH EMPTY KEY.
" ignore STANDARD and WITH EMPTY KEY for now

DATA e3fi TYPE classroom.

" fill table here
DATA joe TYPE student.
" init joe
APPEND joe TO e3fi.

DATA susan TYPE student.
" init susan
APPEND susan TO e3fi

在那之后你的 class 房间 e3fi 有两个学生 susanjoe。这些学生每个人都有自己的名字、生日等等。

如果我没理解错的话,你说的是数据字典中的 table 类型。由于您可以使用语句 TYPE TABLE OF <structure> ,因此在其之上创建 table 类型似乎不直观。但是,如果您想将整个 table 作为参数传递给功能模块或 Class 方法,则需要此 table 类型。

例如,您不能写以下内容:

 methods PASS_TABLE
    returning
      value(rt_table) TYPE TABLE OF <structure> .

在这种情况下,您必须使用字典 table 类型:

 methods PASS_TABLE
    returning
      value(rt_table) TYPE  dict_table_type .