ABAP predefined functions - LINES
When dealing with internal tables, many a time, you are looking to know the number of records the table holds.
In this bite-sized post, I will try to show you how to use ABAP predefined function LINES over the traditional approach of using DESCRIBE keyword.
ABAP veterans have been using the good old DESCRIBE keyword to get the number of lines of an internal table.
A typical example:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REPORT syst-repid. | |
DATA: | |
lt_marc TYPE STANDARD TABLE OF marc. | |
DATA: | |
lv_lines TYPE i, | |
lv_text TYPE string. | |
PARAMETERS: | |
p_matnr TYPE matnr. | |
START-OF-SELECTION. | |
SELECT * | |
FROM marc | |
INTO TABLE lt_marc | |
WHERE matnr = p_matnr. | |
IF syst-subrc <> 0. | |
CLEAR: | |
lt_marc[]. | |
ENDIF. | |
DESCRIBE TABLE lt_marc LINES lv_lines. | |
lv_text = |No. of fetched records: { lv_lines } |. | |
cl_demo_output=>display( lv_text ). |
Function Lines:
The lines function returns the number of rows of an internal table. The argument must be an internal table. The return value has type I.Furthermore, the result of the function can directly be passed to suitable operand positions and method calls.
Same example using LINES:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REPORT syst-repid. | |
DATA: | |
lt_marc TYPE STANDARD TABLE OF marc. | |
PARAMETERS: | |
p_matnr TYPE matnr. | |
START-OF-SELECTION. | |
SELECT * | |
FROM marc | |
INTO TABLE lt_marc | |
WHERE matnr = p_matnr. | |
IF syst-subrc <> 0. | |
CLEAR: | |
lt_marc[]. | |
ENDIF. | |
cl_demo_output=>display( |No. of fetched records: { lines( lt_marc[] ) } | ). |