Open SQL equivalent for ROW_NUMBER()

久未见 提交于 2019-12-31 02:25:28

问题


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.



来源:https://stackoverflow.com/questions/23559808/open-sql-equivalent-for-row-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!