Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open SQL equivalent for ROW_NUMBER()

Tags:

abap

opensql

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?

like image 662
Lilienthal Avatar asked Sep 01 '26 01:09

Lilienthal


1 Answers

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.
like image 65
Chris Moore Avatar answered Sep 07 '26 13:09

Chris Moore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!