Application Development 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: 

How do I read the last entry of an internal table using READ TABLE?

walkerist
Participant
0 Kudos
763

How do I read the last entry of an internal table? Assuming that sometimes the entries of the internal is not fixed as sometime it has 1,2,3, or 4 entries.

I want to read the the last entry of T_EKBE table. Sorting the Material Document Number is prohibited as the material document numbers is not always in sequential form. Here's my code and I believe that READ TABLE only selects the first entry.

    READ TABLE t_ekko_ekpo ASSIGNING FIELD-SYMBOL(<lfs_ekko_ekpo>)
WITH KEY ebeln = t_ekbe-ebeln
ebelp = t_ekbe-ebelp
waers = t_ekbe-waers
BINARY SEARCH.

Currently, the first line is being read here. I want the last line to be read instead.

9 REPLIES 9

DominikTylczyn
Active Contributor
635

Hello walkerist

READ TABLE t_ekko_ekpo INDEX lines( t_ekko_ekpo ) ASSIGNING FIELD-SYMBOL(<lfs_ekko_ekpo>).

Best regards

Dominik Tylczynski

635

Is there a reason why you need the last entry? A problem is that modern databases not always return in the right order. I had several problems when I forgot to sort the selected entries. So, I am not sure if the solution from Dominik will work if you don't have a (somehow) sorted table.

0 Kudos
635

mschwingenschloegl Technically the piece of code I proposed will read the last entry from the internal table, no matter sorting. Neither understand I the business rationale of such a read.

635

mschwingenschloegl because the last entry of T_EKBE contains latest payment made on the Purchase Order. But yeah there are better ways. Cheers!

0 Kudos
635

3a9e4ce873a94034b33dc62b0ce600ee Can SORT BY and DESCENDING co-exist in a single line?

Initially, the code is like this but I need to get the latest entry by sorting its date and time so I added CPUDT, CPUTM. However, the internal table t_ekbe still is not sorted in descending order.

Initial code:
SORT t_ekbe BY ebeln ebelp.
Newly modified code:
SORT t_ekbe BY ebeln ebelp DESCENDING cpudt cputm.

DominikTylczyn
Active Contributor
635

walkerist As per SAP Help documentation on SORT statement, your code

SORT t_ekbe BY ebeln ebelp DESCENDING cpudt cputm.

is sorting by

  1. ebeln ascending
  2. ebelp descending
  3. cpudt ascending
  4. cputm ascending

If you want to sort by ebeln and ebelp ascending and by cpudt and cputm descending, you should go like:

SORT t_ekbe BY ebeln ebelp cpudt DESCENDING cputm DESCENDING.

Or better yet specify sort directions of all the fields explicitly. It's a bit superfluous as ASCENDING is the default sort direction but it is more readable.

SORT t_ekbe BY ebeln ASCENDING ebelp ASCENDING cpudt DESCENDING cputm DESCENDING.

shantraj
Explorer
0 Kudos
635
Data lv_count type i.

Describe table t_ekko_ekpo lines lv_count
READ TABLE t_ekko_ekpo ASSIGNING FIELD-SYMBOL(<lfs_ekko_ekpo>) index lv_count.

matt
Active Contributor
635

Use LINES( t_ekko_ekpo ). DESCRIBE is so 20th century.

635

matthew.billingham Speaking of 20th vs 21st century - SAP has introduced all those shiny new features to ABAP. Still ABAP is very space or white character sensitive, e.g.

READ TABLE t_ekko_ekpo INDEX lines( t_ekko_ekpo ) ASSIGNING FIELD-SYMBOL(<lfs_ekko_ekpo>).

is not the same as

READ TABLE t_ekko_ekpo INDEX lines(t_ekko_ekpo) ASSIGNING FIELD-SYMBOL(<lfs_ekko_ekpo>).

The latter will trigger a syntax error. It is "so 20th century" 😉

I wonder if there are any plans to improve ABAP syntax and get rid off those space dependencies.