Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ORDER BY when using SELECT FROM FIELDS

afordham
Participant
0 Kudos

Hi,

I have been trying to work out how to specify an ORDER BY clause when using the SELECT FROM tablename FIELDS fieldname variant of the SELECT statement, but I'm unable to find anything that syntax checks. There's no specific example of this clause in the help for the SELECT statement, so I'm a bit lost. Has anyone got the magic keyword order? The system is ABAP 7.5.

As an example, I'm trying to do this sort of thing:

SELECT FROM tablename
FIELDS field1, field2
ORDER BY field1
INTO TABLE @data(internal_tablename).

Thanks,

Andrew

3 REPLIES 3

matt
Active Contributor
0 Kudos

What error are you getting. There's an example here, https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapfields_clause.htm which seems to be the same as yours.

SELECT FROM scarr 
    FIELDS carrid, carrname
    ORDER BY carrid
    INTO TABLE @DATA(result1). 


0 Kudos

Thanks. Matthew. That example does indeed work for me!

I'm going to have to dig up my original attempt and work out how it differs from the example above. I know it had a number of joins in it, but the keywords were definitely in the same order.

pokrakam
Active Contributor

Your example looks right. I kinda like this syntax variant, it makes it a bit more readable when you see the tables before the fields.

Even with joins it's not much different from the 'classic' SELECT. Here's a recent piece of code I wrote (anomymized):

SELECT FROM foo INNER JOIN bar
                   ON foo~id = bar~id
       FIELDS foo~f1           AS field1,
              bar~f2           AS field2,
              SUM( bar~menge ) AS quantity,
              bar~meinh        AS unit
       WHERE foo~f3 = 'x'
         AND foo~f4 = 'y'
       GROUP BY foo~f1, bar~f2
       ORDER BY foo~f1, bar~f2
       INTO TABLE @itab.