Open SQL equivalent for ROW_NUMBER()

2020-04-11 06:38发布

问题:

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?

回答1:

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.


回答2:

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.



标签: sap abap opensql