Цитата:
Addition 5
... BY (otab)
Effect:
With the addition BY (otab), the table is not sorted according to the table key, but instead according to the component specified dynamically in the internal table otab (as of release 7.0). Each line of the table otab defined a component of the sort key. The priority of the sort is based on the order of the lines in otab. A maximum of 250 components can be specified. If the table otab is initial, the table is not sorted.
For otab, a standard table of the table type ABAP_SORTORDER_TAB from the ABAP Dictionary must be specified. The line type of this table is the Dictionary structure ABAP_SORTORDER with the following components:
NAME of type SSTRING
for specifying a component of the sort key. The component is entered in the form "comp_name[+off(len)]", whereby "comp_name" must be the name of a component contained in itab in upper case to which an offset/length specification "+off(len)" can be appended. "off" and "len" must be suitable numeric values.
DESCENDING of type CHAR of length 1 for specifying the
sort direction for the current component. If DESCENDING is initial, the sort is performed in ascending order. If DESCENDING has the value "X", the table is sorted in descending order.
ASTEXT of type CHAR of length 1
for textual sorting of the current component. If ASTEXT has the value "X", the sort is performed as with the AS TEXT addition. This is only possible for character-type components. If ASTEXT is initial, character-type components are sorted according to their binary representation.
If a column of otab, has invalid content (NAME contains the name of a component that does not exist or an incorrect offset/length specification, DESCENDING and ASTEXT do not contain "X" or the initial value), this leads to a treatable exception of the class CX_SY_DYN_TABLE_ILL_COMP_VAL.
Notes:
The addition BY (otab) cannot be combined with BY compi.
When using the addition BY (otab), it is not possible to use DESCENDING or AS TEXT to specify a descending sort direction or textual sorting for all components.
If a single bracketed data object (dobj) is specified after the BY addition, its data type decides whether its content is used to specify a single component or multiple components. In either case, no sort takes place if dobj is initial.
Example:
Dynamic import of a database table into a dynamic internal table and dynamic sorting of its content. The name of the database table and the names of the columns by which the table is to be sorted can be entered on a selection screen.
Code:
PARAMETERS dbtab TYPE c LENGTH 30.
SELECT-OPTIONS columns FOR dbtab NO INTERVALS.
DATA: otab TYPE abap_sortorder_tab,
oline TYPE abap_sortorder,
dref TYPE REF TO data.
FIELD-SYMBOLS: <column> LIKE LINE OF columns,
<itab> TYPE STANDARD TABLE.
TRY.
CREATE DATA dref TYPE STANDARD TABLE OF (dbtab).
ASSIGN dref->* TO <itab>.
CATCH cx_sy_create_data_error.
MESSAGE 'Wrong data type!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
TRY.
SELECT *
FROM (dbtab)
INTO TABLE <itab>.
CATCH cx_sy_dynamic_osql_semantics.
MESSAGE 'Wrong database table!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
LOOP AT columns ASSIGNING <column>.
oline-name = <column>-low.
APPEND oline TO otab.
ENDLOOP.
TRY.
SORT <itab> BY (otab).
CATCH cx_sy_dyn_table_ill_comp_val.
MESSAGE 'Wrong column name!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.