ABAP中的WITH HEADER LINE有什么用?

What is WITH HEADER LINE used for in ABAP?

我一直在研究这个 book 并引用它:

DATA: BEGIN OF CUSTOMER_TAB OCCURS 5,
         KUNNR TYPE KNA1-KUNNR,
         NAME1 TYPE KNA1-NAME1,
      END OF CUSTOMER_TAB.

This declaration creates an internal table and a structure using the same name: CUSTOMER_TAB.

然后在接下来的页面中:

Declaring Both an Internal Table and a Structure by Referring to a Structured 
Local/Global TYPE or Local/Global Structure

DATA <internal table name> {TYPE|LIKE} <structure name> OCCURS <number> WITH HEADER LINE.

WITH HEADER LINE is a reserved key phrase. The addition of this phrase creates a structure.

此刻我很困惑。第一个示例是仅声明一个 Internal Table 还是一个 Internal Table 和一个同名的 Structure?

题目应该是"What was WITH HEADER LINE used for in ABAP"。您应该只在遗留代码中看到它们。无论如何只允许 outside classes and are obsolete

正在回答您的问题。两者都是。它声明了一个内部 table customer_tab[] 和一个结构 customer_tab.

然后您可以执行 "amazing" 之类的操作。

 LOOP AT customer_tab. "loops at tab CUSTOMER_TAB and stores current record in header line structure CUSTOMER_TAB. :]
   "do something with header line
 END LOOP.

 APPEND customer_tab.

如您所见,它更短,而且非常吸引人使用它的简洁性。虽然它很难阅读和混淆,因此被标记为过时。

糟糕,还有一分!您还应该了解

之间的区别
CLEAR customer_tab.

REFRESH customer_tab.

您显示的两个声明都创建一个 table 和 header 行。第一个声明没有具体说明 'WITH HEADER LINE',但它确实创建了一个工作区和一个 table,就好像您也使用了 'WITH HEADER LINE' 语句一样。有关 header 行的信息,请参阅此 SAP Help。您引用的文档是过时的语法。由于遗留代码,您会看到它,因此您需要理解它但避免使用此语法。

types:
  begin of typ_functions,
  funcname type rs38l_fnam,
  pname    type pname,
  fmode    type fmode,
  stext    type rs38l_ftxt,
  end of typ_functions,
  typ_functions_t type standard table of typ_functions initial size 0.

  data:   
           "Global Structure using structure type
           gs_function   type typ_functions,   
           "Global Table using table type typ_functions_t
           gt_functions  type typ_functions_t,
           "Global Table using data dictionary structure
           gt_exp        type standard table of rsdsexpr initial size 0.

我个人偏好使用如上所示的语法,在 TYPES 中,我创建了结构类型和 table 类型。然后在 DATA 中,我将创建实际的 table 和结构。在示例中,我声明了一个全局结构和 table。为了仅从数据字典引用中声明 table,我使用了显示为 GT_EXP 的声明。内联注释仅用于本次讨论,我本身不在程序中使用这种格式。