Select - Union: Syntax

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

SELECT - UNION

Quick Reference

Syntax
... UNION [ALL|DISTINCT]
[(] SELECT { select_clause
FROM source }
|{ FROM source
FIELDS select_clause }
[WHERE sql_cond]
[GROUP BY group] [HAVING group_cond]
[UNION [ALL|DISTINCT] select] [)] ...

Effect

The addition UNION creates the union of the results sets of two SELECT statements. The rows of the results set
of the SELECT statement on the right of UNION are inserted into the results set of the SELECT statement on the
left of UNION. The columns of the results set keep the names defined in the SELECT statement on the left
of UNION.

The SELECT statements on the left and right of UNION have their own SELECT, FROM, WHERE, GROUP BY,
and HAVING clauses. The SELECT statement on the right side of UNION can itself contain a UNION addition
and be enclosed in parentheses ( ). The ORDER BY clause and the INTO clause are applied to the union
results set and must be specified after the position of the last closing parenthesis.

The additions ALL and DISTINCT specify how duplicate rows are handled. DISTINCT is the default here:

 If the addition ALL is specified, all rows from the results set of the right SELECT statement are inserted
into the existing results set.

 If the addition DISTINCT is specified or if neither of the two additions is specified, the rows of the
results set of the right SELECT statement are inserted into the existing results set. All duplicate rows
are then deleted except for one (including all columns of the results set).

The SELECT statements associated with UNION are evaluated from left to right. Specific statements can be
prioritized using parentheses.

The results set of SELECT statements joined using UNION is always regarded as a multirow set and the
addition SINGLE is not allowed. If an assignment is made to a non-table-like target are (meaning
a SELECT statement without the addition INTO|APPENDING ... TABLE), a loop closed using ENDSELECT is
always opened.

The same applies to the clauses and additions of the SELECT statements grouped using UNION as to a
single SELECT statement, with the following exceptions:

 The SELECT list must consist of a list of specified columns col_spec that can cover the data sources
and SQL expressions, including host variables and host expressions. * and data_source~* cannot
be specified.
 All SELECT lists of SELECT statements grouped using UNION must have the same number of
elements.

 The columns assigned to each other must have the same type attributes with respect to predefined
data type, length, and number of digits after the decimal point, with the following exceptions:

o Numeric types INT1, INT2, INT4 and INT8 can create a column. The resulting column has the
data type with the greatest value range.

o Numeric types DEC can have different lengths, but must have the same number of digits after the
decimal point. The resulting column has the data type with the greatest length. The corresponding
special types CURR and QUAN are handled here like DEC.

o The numeric types DF16_DEC and DF34_DEC are handled like the numbers of type DEC (as
they are saved) and the rule above applies with respect to lengths and decimal places.

o Character-like types CHAR can have different lengths. The resulting column has the data type
with the greatest length. The corresponding special types CLNT, LANG, CUKY, and UNIT are
handled here like CHAR.

All other types must be exactly the same. This applies specifically to NUMC and RAW, where the
lengths must match. The different categories of strings cannot be combined either.

 If the addition CORRESPONDING or an inline declaration @DATA(...) is used in the INTO clause, the
column names of all results sets from left to right must match.

 Columns of the results set specified after ORDER BY must also have the same name in
all SELECT statements in question. The addition ORDER BY PRIMARY KEY is not allowed.

 The addition FOR ALL ENTRIES is not allowed in the WHERE conditions of the SELECT statements in
question.

 The addition UP TO n ROWS is not allowed.

The number of SELECT statements, which can be linked using UNION, is not limited by a fixed value.

Notes

 Each of the associated SELECT statements has its own client handling. An addition USING
CLIENT or CLIENT SPECIFIED applies only to the SELECT statement for which it is specified.

 If required, the column names of the individual results sets can be modified using alternative column
names after AS.

 The numeric types DF16_DEC and DF34_DEC can only be used together in a column if their decimal
places match.

 SQL expressions, host expressions, and host variables can be used to synchronize the columns of the
results set of the SELECT statements in question.

 Priorities using parentheses are particularly applicable when handling duplicate rows
using DISTINCT.
 The default behavior or the addition DISTINCT are always applied to the full existing results set. The
addition DISTINCT also removes any duplicate rows produced by the addition ALL of
preceding UNION additions.

 When UNION is used, the SELECT statement bypasses SAP buffering.

 The addition UNION can also be used in the statement OPEN CURSOR or in a subquery in
the WHERE condition or after INSERT.

 When UNION is used, the syntax check is performed in a strict mode, which handles the statement
more strictly than the regular syntax check. More specifically, the INTO clause and the
additions additional_options can be specified at the end of the full SELECT statement.

You might also like