ORDER BY clause
Variants:

1. ... ORDER BY PRIMARY KEY
2. ... ORDER BY f1 ... fn
3. ... ORDER BY (itab)
Effect
Orders the records in a SELECT statement. Without the ORDER-BY clause, the order in which the selected lines are supplied is undefined. This means that two similar SELECT statements may produce lines in a different order.
Variant 1
...ORDER BY PRIMARY KEY
Effect
Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....
Example
Output the passenger list for the Lufthansa flight 0400 on 28.02.1995:
DATA WA_BOOK TYPE SBOOK.

SELECT * FROM SBOOK INTO WA_SBOOK
        WHERE
          CARRID  = 'LH '      AND
          CONNID  = '0400'    AND
          FLDATE  = '19950228'
        ORDER BY PRIMARY KEY.
  WRITE: / WA_SBOOK-BOOKID, WA_SBOOK-CUSTOMID,  WA_SBOOK-CUSTTYPE,
          WA_SBOOK-SMOKER, WA_SBOOK-LUGGWEIGHT, WA_SBOOK-WUNIT,
          WA_SBOOK-INVOICE.
ENDSELECT.
Notes
Since views do not have a primary key, specifying ORDER BY PRIMARY KEY only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.
Variant 2
ORDER BY f1 ... fn
Effect
Sorts the selected records in ascending order by the specified column references f1 ... fn. I
f a list is also specified in the SELECT clause, the column references f1, ..., fn must appear in this list.

By supplementing the statement with DESCENDING, you can sort in descending order using any of the fields f1, ..., fn.

The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.
Examples
Output Lufthansa flights from 27.02.1995 to 05.03.1995, sorted by plane type and number of occupied seats:
DATA WA_SFLIGHT TYPE SFLIGHT.

SELECT * FROM SFLIGHT INTO WA_SFLIGHT
        WHERE CARRID = 'LH' AND
              FLDATE BETWEEN '19950227' AND '19950305'
        ORDER BY PLANETYPE ASCENDING SEATSOCC DESCENDING.
  WRITE: / WA_SFLIGHT-PLANETYPE, WA_SFLIGHT-SEATSOCC,
          WA_SFLIGHT-CONNID, WA_SFLIGHT-FLDATE
ENDSELECT.
Notes
1.Pooled and cluster tables can only be sorted by their primary key.
2.With a SELECT * ..., the client field automatically becomes the first sort criterion in client-specific tables, unless the addition CLIENT SPECIFIED is specified in the FROM clause.
3.Specifying FOR ALL ENTRIES IN itab WHERE ... in the WHERE clause excludes ORDER BY f1 ... fn.
Notes
Performance:
1.In contrast to ... ORDER BY PRIMARY KEY, ORDER BY f1 ... fnsort of in order is not automatically supported by a (sorted) index. Without an index, you must sort the result set at runtime. Because of the SAP architecture, this should not be performed on the database server, but on the applications server. If it does not make sense to create an index, you should not sort the result set with ... ORDER BY f1 ... fn on the database server, but with SORT on the application server.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。