Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Reads →  UNION →  UNION Examples 

SELECT, Union with Global Temporary Table

This example demonstrates a union across a table and a global temporary table (GTT).

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA carrid TYPE sflight-carrid VALUE 'AA'.
    cl_demo_input=>request( CHANGING field = carrid ).

    INSERT demo_sflight_agg FROM (
    SELECT carrid,
           connid,
           CAST( '00000000' AS DATS ) AS fldate,
           SUM( seatsocc ) AS seatsocc
           FROM sflight
           WHERE carrid = @( to_upper( carrid ) )
           GROUP BY carrid, connid ).

    SELECT ' ' AS mark, carrid, connid, fldate, seatsocc
           FROM sflight
           WHERE carrid = @( to_upper( carrid ) )
           UNION SELECT 'X' AS mark,
                        carrid, connid, fldate, seatsocc
                        FROM demo_sflight_agg
           ORDER BY carrid, connid, mark, fldate, seatsocc
           INTO TABLE @DATA(result).

    DELETE FROM demo_sflight_agg.
    cl_demo_output=>display( result ).

Description

This example has the same result as the executable example for a union with aggregate expression. Here, however, the SELECT statement is split into two ABAP SQL statements for demonstration purposes:

  • UNION is used to create the union of the results set of a SELECT statement on the table SFLIGHT and a SELECT statement on the GTT.

The GTT must be cleared explicitly before the end of the program to prevent the runtime error COMMIT_GTT_ERROR in the next implicit database commit.


Note

See also the corresponding executable example for common table expressions.