如何使用sap ABAP从表中获取记录数

时间:2016-12-24 04:55:54

标签: sap abap

SELECT COUNT(*) AS NumberOfRecords FROM tableX;如何将其转换为SAP ABAP

5 个答案:

答案 0 :(得分:3)

对于数据库表,您可以这样SELECT COUNT

SELECT COUNT( * )
    INTO numberOfRecords
    FROM tableX. 

要获取内部表的行数,您需要DESCRIBE语句:

DESCRIBE TABLE tableX LINES numberOfRecords. 

答案 1 :(得分:1)

对于内部表,您也可以在函数中使用此构建:

numberOfRecords = lines( tableX)

答案 2 :(得分:0)

我遇到了类似的问题,一位同事让我关注。 我需要一个键的数据库表中的条目计数,并且没有在此处输入代码想要在循环中执行选择计数(*)。 因此,我们创建了一个带有唯一键的排序表,在循环之前使用条件在该表中执行 select... endselect,然后在循环中简单地读取这个新的排序表:

types:
            begin of ts_tab
                        f1 type 1
                        f2 type 2
                        f3 type i
            end of ts_tab
            tt_tab type sorted table of ts_tab
            with unigue key f1 f2
 
select f1 f2 into coorresponding fields of ls_tab
            read table lt_tab assigning <ls_tab>
                        with table key f1 = f1 etc.
            if sy-subrc = 0.
                        add 1 to <ls_tab>-f3
            else.
                        ls_tab-f3 = 1.
                        insert ls_tab into lt_tab.
            endif.
endselect 
loop etc.

   read lt_tab into ls_tab with key f1 = f1 f2 = f2.
   if sy-subrc eq 0.
      my_count = ls_tabl-f3.
   endif

endloop

答案 3 :(得分:-1)

您可以使用ABAP声明:

DESCRIBE TABLE itab[] lines lv_no.

答案 4 :(得分:-2)

1)如果只想对数据库表中的记录计数,请使用以下语法。

SELECT COUNT( * ) INTO RecordCount FROM tableX.

2)但是,如果您需要处理记录以及计数,请使用以下内容。

SELECT * INTO TABLE itab FROM tableX.
DESCRIBE TABLE itab[] lines RecordCount.