Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

INTERNAL TABLE PROBLEM

Former Member
0 Likes
914

Hi frnds... came with my new issue..

wht is the logic ...

i dont know how many records r there in my itab but i need to hav the last record of my table how to do that.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
883

DESCRIBE table itab lines w_lines.

read table itab index w_lines.

W_lines will give you the no. of lines of the itab and then you read the last row.

Regards,

Ravi

Note : Please mark the helpful answers

9 REPLIES 9
Read only

Former Member
0 Likes
884

DESCRIBE table itab lines w_lines.

read table itab index w_lines.

W_lines will give you the no. of lines of the itab and then you read the last row.

Regards,

Ravi

Note : Please mark the helpful answers

Read only

Former Member
0 Likes
883

Hi,

try this logic.

loop at itab.

at last.

write:/ itab-fld1,itab-fld2.

endat.

endloop.

regards,

keerthi.

Read only

Former Member
0 Likes
883

U can use Condition Stmts like

AT END,

AT NEW,

AT LAST. And u have to call there within Loop Only.

AT - control break 


Variants: 


1. AT NEW f. 
2. AT END OF f. 
3. AT FIRST. 
4. AT LAST. 



Variant 1 
AT NEW f. 

Variant 2 
AT END OF f.




Effect 
f is a sub-field of an internal table or extract dataset (EXTRACT) which is being processed with LOOP, i.e. the variants 1 and 2 only make sense within a LOOP. 
Both "AT NEW f." and "AT END OF f. " introduce processing blocks which are concluded by " ENDAT.". 
These processing blocks are processed whenever the contents of a field f or a sub-field defined before f change as a result of processing with LOOP. "AT NEW f." begins a new group of (table) lines with the same contents as the field f while "AT END OF f." concludes such a group. 

Within the AT ... ENDAT processing of internal tables, all argument fields following f are filled with "*". 



Examples 
1. AT for sub-fields of an internal table 



DATA: BEGIN OF COMPANIES OCCURS 20, 
        NAME(30), 
        PRODUCT(20), 
        SALES TYPE I, 
      END   OF COMPANIES. 
... 
LOOP AT COMPANIES. 
  AT NEW NAME. 
    NEW-PAGE. 
    WRITE / COMPANIES-NAME. 
  ENDAT. 
  WRITE: / COMPANIES-PRODUCT, COMPANIES-SALES. 
  AT END OF NAME. 
    SUM. 
    WRITE: / COMPANIES-NAME, COMPANIES-SALES. 
  ENDAT. 
ENDLOOP. 



The AT statements refer to the field COMPANIES-NAME. 



Examples 
2. AT for the field of an extract dataset 



DATA: NAME(30), 
      SALES TYPE I. 
FIELD-GROUPS: HEADER, INFOS. 
INSERT: NAME  INTO HEADER, 
        SALES INTO INFOS. 
... 
LOOP. 
  AT NEW NAME. 
    NEW-PAGE. 
  ENDAT. 
  ... 
  AT END OF NAME. 
    WRITE: / NAME, SUM(SALES). 
  ENDAT. 
ENDLOOP. 



Notes 
If the processing you want to perform on an internal table is fairly restricted (i.e. a WHERE addition with the LOOP statement), do not use the AT statements specified in variants 1 to 5, since the interaction of the WHERE addition and the AT statement is currently not defined. 



When you use LOOP with an extract dataset, fields on hex zero are ignored during control level checking with AT NEW or AT END OF . This procedure is the same as the SORT statement. When sorting extracted datasets, this statement always sorts blank fields (i.e. fields on hex zero) regardless of the sequence (ascending or descending) before all fields that contain values. 



Since fields addressed with AT are not set to an initial value when you enter a LOOP, the first new group of (table) lines in AT NEW f may not be processed, if f happens to be set to this value. 



Variant 3 
AT FIRST. 

Variant 4 
AT LAST. 



Effect 
The variants 3 and 4 only make sense within a LOOP. 
The processing block between AT FIRST and ENDAT is executed before the individual lines are processed; the processing block between AT LAST and ENDAT is executed after all the individual lines have been processed. 

In AT FIRST or AT LAST ... ENDAT processing, all argument fields are filled with "*" (internal tables). 
When you are processing extract datasets, a control total SUM(n) can only be processed with AT END OF or AT LAST. 



Example
DATA: BEGIN OF COMPANIES OCCURS 20, 
        NAME(30), 
        PRODUCT(20), 
        SALES TYPE I, 
      END   OF COMPANIES. 
... 
LOOP AT COMPANIES. 
  AT FIRST. 
    SUM. 
    WRITE:    'Sum of all SALES:', 
           55 COMPANIES-SALES. 
  ENDAT. 
  WRITE: / COMPANIES-NAME, COMPANIES-PRODUCT, 
        55 COMPANIES-SALES. 
ENDLOOP.

Regards

Prabhu

Read only

Former Member
0 Likes
883

Hello Sathish,

Use like this

DESCRIBE TABLE ITAB LINES LV_LINES.

READ TABLE ITAB INDEX LV_LINES.

Reward if helps.

Vasanth

Read only

Former Member
0 Likes
883

hi,

use describe stmt..

data v_lines type i.

descibe table itab lines v_lines.

hope this helps,

priya.

Read only

Former Member
0 Likes
883

Hi,

Simple.

Sort itab descending.

read table index 1.

You will read the last record only.

Regards

Subbu

Read only

aris_hidalgo
Contributor
0 Likes
883

Hi Satish,

Here is your solution:

TABLES: spfli.

DATA: it_itab TYPE TABLE OF spfli,

wa_itab like line of it_itab.

SELECT * FROM spfli

INTO TABLE it_itab.

DESCRIBE TABLE it_itab LINES sy-tfill.

READ TABLE it_itab INDEX sy-tfill into wa_itab.

IF sy-subrc = 0.

WRITE wa_itab.

ELSE.

EXIT.

ENDIF.

Here is a step by step explanation:

1. I selected all records from SPFLI.

2. I used the DESCRIBE statement to get the total number of records in my itab.

3. Now, I used the number that I got from the describe statement to read the last line of my internal table.

So for example, after reading my itab I got the value of 22, so when I read my itab using the value that holds the number of records(in this case I used sy-tfill) it reads the last record of my itab. You can use other variables to hold the number of records in your itab if you like.

Hope this helps...

P.S. Please award points for useful answers.

Read only

Former Member
0 Likes
883

Hi Satish,

If you want to read the last entry in your itab just after fetching data into it from select query,

You can use SY-DBCNT as the index while reading.

Read table itab index sy-dbcnt.

Because once the query is executed, the sy-dbcnt will hold the number of records read from the database.

This will avoid the DESCRIBE TABLE statement.

Regards,

Amogh

Read only

uwe_schieferstein
Active Contributor
0 Likes
883

Hello Satish

The new version of the DESCRIBE command is:

DESCRIBE TABLE itab.  " fills syst-tfill with the 
                      " number of entries = last entry
READ itab INTO gs_entry INDEX syst-tfill.

Regards

Uwe