简体   繁体   English

如何在abap中获取内部表的行数?

[英]How to get rows count of internal table in abap?

How do I get the row count of an internal table?如何获取内部表的行数? I guess that I can loop on it.我想我可以循环播放它。 But there must be a saner way.但必须有更理智的方式。

I don't know if it makes a difference but the code should run on 4.6c version.我不知道它是否有区别,但代码应该在 4.6c 版本上运行。

There is also a built-in function for this task:此任务还有一个内置函数:

variable = lines( itab_name ).

Just like the "pure" ABAP syntax described by IronGoofy , the function "lines( )" writes the number of lines of table itab_name into the variable.就像IronGoofy描述的“纯”ABAP 语法一样,函数“lines()”将表 itab_name 的行数写入变量。

You can use the following function:您可以使用以下功能:

 DESCRIBE TABLE <itab-Name> LINES <variable>

After the call, variable contains the number of rows of the internal table .调用后,变量包含内表的行数。

Beside the recommended除了推荐的

DESCRIBE TABLE <itab-Name> LINES <variable>

there is also the system variable SY-TFILL .还有系统变量SY-TFILL

From documentation:从文档:

After the statements DESCRIBE TABLE, LOOP AT and READ TABLE, the number of rows of the accessed internal table.在 DESCRIBE TABLE、LOOP AT 和 READ TABLE 语句之后,访问的内部表的行数。

Example script:示例脚本:

REPORT ytest.

DATA pf_exclude TYPE TABLE OF sy-ucomm WITH HEADER LINE.

START-OF-SELECTION.
  APPEND '1' TO pf_exclude.
  APPEND '2' TO pf_exclude.
  APPEND '3' TO pf_exclude.
  APPEND '4' TO pf_exclude.

  WRITE: / 'sy-tfill = ', sy-tfill.

  DESCRIBE TABLE pf_exclude.
  WRITE: / 'sy-tfill = ', sy-tfill, 'after describe table'.

  sy-tfill = 0. "Reset
  READ TABLE pf_exclude INDEX 1 TRANSPORTING NO FIELDS.
  WRITE: / 'sy-tfill = ', sy-tfill, 'after read table'.


  sy-tfill = 0. "Reset
  LOOP AT pf_exclude.
    WRITE: / 'sy-tfill = ', sy-tfill, 'in loop with', pf_exclude.
    sy-tfill = 0. "Reset
  ENDLOOP.

The result:结果:

sy-tfill =           0
sy-tfill =           4  after describe tabl
sy-tfill =           4  after read table
sy-tfill =           4  in loop with 1
sy-tfill =           0  in loop with 2
sy-tfill =           0  in loop with 3
sy-tfill =           0  in loop with 4

Please get attention of the value 0 for the 2nd entry: SY-TFILL is not updated with each step, only after the first loop.请注意第二个条目的值 0: SY-TFILL不会随每一步更新,仅在第一个循环之后更新。

I recommend the usage SY-TFILL only, if you need it direct after the READ (1)... If there are other commands between the READ and the usage of SY-TFILL, there is always the danger of a change of the system variable.我只推荐使用SY-TFILL,如果你需要在READ之后直接使用(1)... 如果在READ和使用SY-TFILL之间还有其他命令,总是有系统变化的危险变量。

(1) or describe table. (1) 或描述表。

  DATA : V_LINES TYPE I. "declare variable
  DESCRIBE TABLE <ITAB> LINES V_LINES. "get no of rows
  WRITE:/ V_LINES. "display no of rows

Refreance: http://www.sapnuts.com/courses/core-abap/internal-table-work-area.html参考: http : //www.sapnuts.com/courses/core-abap/internal-table-work-area.html

The functional module EM_GET_NUMBER_OF_ENTRIES will also provide the row count.功能模块 EM_GET_NUMBER_OF_ENTRIES 也将提供行数。 It takes 1 parameter - the table name.它需要 1 个参数 - 表名。

您还可以使用 OPEN Sql 使用 COUNT Grouping 子句查找行数,并且还有系统字段 SY-LINCT 来计算表的行数(ROWS)。

if I understand your question correctly, you want to know the row number during a conditional loop over an internal table.如果我正确理解您的问题,您想知道在内部表的条件循环期间的行号。 You can use the system variable sy-tabix if you work with internal tables.如果您使用内部表,则可以使用系统变量 sy-tabix。 Please refer to the ABAP documentation if you need more information (especially the chapter on internal table processing ).如果您需要更多信息,请参阅 ABAP 文档(尤其是有关内部表处理的章节)。

Example:示例:

LOOP AT itab INTO workarea
        WHERE tablefield = value.

     WRITE: 'This is row number ', sy-tabix.

ENDLOOP.
data: vcnt(4).

clear vcnt.

LOOP at itab WHERE value = '1'.
  add 1 to vcnt.
ENDLOOP.

The answer will be 3. (vcnt = 3).答案是3. (vcnt = 3).

I don't think there is a SAP parameter for that kind of result.我认为这种结果没有 SAP 参数。 Though the code below will deliver.虽然下面的代码将提供。

LOOP AT intTab.

  AT END OF value.

    result = sy-tabix.

    write result.  

  ENDAT.

ENDLOOP.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM