STRING_AGG verkettet die Ergebnisse eines SQL-Ausdrucks in einer Zeile (Typ SSTRING, Länge 1333). Wenn der String länger als 1333 Zeichen ist, wird eine Ausnahme (CX_SY_OPEN_SQL_DB) geworfen. Die Beschränkung auf 1333 Zeichen kann durch die Funktion TO_CLOB umgangen werden.
Variante 1 (transparente Tabelle)
SELECT FROM t100
FIELDS arbgb,
to_clob( STRING_AGG( text, ', ' ) ) AS text_agg
WHERE sprsl = 'D'
GROUP BY arbgb
ORDER BY arbgb
INTO TABLE @DATA(it_t100)
UP TO 10 ROWS.
IF sy-subrc = 0.
cl_demo_output=>write_data( it_t100 ).
* HTML-Code vom Demo-Output holen
DATA(lv_html) = cl_demo_output=>get( ).
* Daten im Inline-Browser im SAP-Fenster anzeigen
cl_abap_browser=>show_html( EXPORTING title = 'T100'
html_string = lv_html
container = cl_gui_container=>default_screen ).
* cl_gui_container=>default_screen erzwingen
WRITE: space.
ENDIF.
Variante 2 (interne Tabelle)
* interne Tabelle anlegen
TYPES: BEGIN OF ty_s_values,
name TYPE char5,
text TYPE char3,
END OF ty_s_values.
TYPES: ty_it_values TYPE STANDARD TABLE OF ty_s_values WITH DEFAULT KEY.
DATA(it_values) = VALUE ty_it_values(
( name = 'TEXT1' text = '123' )
( name = 'TEXT1' text = '456' )
( name = 'TEXT1' text = '789' )
( name = 'TEXT2' text = 'abc' )
( name = 'TEXT2' text = 'def' )
( name = 'TEXT2' text = 'ghi' )
( name = 'TEXT3' text = '***' )
( name = 'TEXT3' text = '---' )
( name = 'TEXT3' text = '+++' )
).
* Daten aus der internen Tabelle selektieren
SELECT FROM @it_values AS val
FIELDS val~name,
to_clob( STRING_AGG( val~text, ', ' ) ) AS text_agg
WHERE val~name = 'TEXT1'
GROUP BY val~name
ORDER BY val~name
INTO TABLE @DATA(it_agg).
IF sy-subrc = 0.
cl_demo_output=>write_data( it_agg ).
* HTML-Code vom Demo-Output holen
DATA(lv_html) = cl_demo_output=>get( ).
* Daten im Inline-Browser im SAP-Fenster anzeigen
cl_abap_browser=>show_html( EXPORTING title = 'TXT'
html_string = lv_html
container = cl_gui_container=>default_screen ).
* cl_gui_container=>default_screen erzwingen
WRITE: space.
ENDIF.