0

I have a structure in ABAP :

id werks name1

My query is :

    SELECT    werks name1
    INTO      TABLE lt_store
    FROM      t001w
    WHERE     vlfkz   EQ 'A'.

It is possible to add a field id that contains a incrementable value for example :

  id    werks    name1
-----------------------
 1      R0001    test
 2      ERT3     test2 .....

and to do some SELECT like that

SELECT (value) werks name FROM...

My solution is to do a loop on lt_store and add value manually but I think it was optimize if we do that directly into the query.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
shmoolki
  • 1,551
  • 8
  • 34
  • 57
  • 2
    Possible duplicate of [Open SQL equivalent for ROW\_NUMBER()](http://stackoverflow.com/questions/23559808/open-sql-equivalent-for-row-number) – Jagger Feb 24 '16 at 13:37

3 Answers3

1

What you want cannot be done with OpenSQL. Depending on the database you are running this on you might be able to put something together in Native SQL, but for this requirement I would not recommend that since there is a simple solution with the LOOP.

Gert Beukema
  • 2,510
  • 1
  • 17
  • 18
1

Open SQL doesn't support aggregate functions, but you can optimize a bit with a field symbol:

LOOP AT lt_store assignin <fs_store>.
  <fs_store>-id = sy-tabix.
ENDLOOP.
  • This optimization is dependent on the size of the table structure, for small tables (with a small number of fields) it is faster to use a work area. In regards to lines of code the field symbol approach wins hand down :-) – Gert Beukema Mar 04 '16 at 19:52
-1

You can use sy-tabix field while processing your results in loop. for example,

LOOP AT lt_store INTO ls_store.
  WRITE: /(1) sy-tabix, (5) ls_store-werks, (15)ls_store-name1.
ENDLOOP.