3

Is there an equivalent for the ROW_NUMBER() function for ABAP programs?

This function is used as follows in SQL:

SELECT ROW_NUMBER() OVER (ORDER BY SomeField) AS Row, *
FROM SomeTable

Where it should return the line number as the first column in the resulting rows (I'm unsure if it will be the line number in the result set or the line number in the source table). I've found that this statement can be used in SAP Business One but can't seem to find an Open SQL equivalent.

Is there one or will I be forced to manually loop over the resulting itab to assign indices?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Lilienthal
  • 4,327
  • 13
  • 52
  • 88
  • Just out of curiosity - what would you need this for? The row number will (have to) be volatile, so what would want to you do with it? – vwegert May 09 '14 at 12:03
  • The basic use case is to provide the row number in its own column in a structure that could be sent to XML or to an ALV. This function would make it so I can fill this entire structure with a single SELECT, without having to put the selected date in a temporary itab, looping over that itab and filling the row number. – Lilienthal May 09 '14 at 13:37
  • 1
    Instead of open sql it is possible to use native: http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3b8b358411d1829f0000e829fbfe/content.htm – Jorg May 11 '14 at 22:11

2 Answers2

2

If this is for storing the row the record is kept on for later access within the ABAP program, you don't need to store it. It is kept as you loop through. SOmething like.

LOOP AT itab INTO wa_itab.
write /:sy-tabix "This is the index of the record within the table
ENDLOOP.

If you are modifying the contents then storing them back into the table, add a column for your value and do something like: DATA: my_string TYPE string.

LOOP AT itab INTO wa_itab.
my_string = sy-tabix.
CONCATENATE some_text my_string more_text into wa_itab-my_field.
MODIFY itab FROM wa_itab. "Here you can leave out INDEX sy-tabix because it is inside a loop and the current line will be used.
CLEAR my_string.
ENDLOOP.
1

No, this is impossible in OpenSQL. It was impossible 5 years ago and still impossible now.

However, it is possible in SAP HANA with the CREATE SEQUENCE statement.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90