Returns a single result set that combines the results of two or more queries. Duplicate rows are eliminated from the result set unless the all keyword is specified.
select [top unsigned_integer] select_list [into clause] [from clause] [where clause] [group by clause] [having clause] [union [all] select [top unsigned_integer] select_list [from clause] [where clause] [group by clause] [having clause]]... [order by clause] [compute clause]
select stor_id, stor_name from sales union select stor_id, stor_name from sales_east
select pub_id, pub_name, city into results from publishers union select stor_id, stor_name, city from stores union select stor_id, stor_name, city from stores_east
select au_lname, city, state from authors union ((select stor_name, city, state from sales union select stor_name, city, state from sales_east) union select pub_name, city, state from publishers)
select top 3 au_lname from authors union all select top 3 title from titles
You cannot use the union operator in a a subquery
You cannot use the union operator with the for browse clause
You cannot use an identity function in a select into statement with the union operator.
The maximum number of subqueries within a single side of a union is 250.
The total number of tables that can appear on all sides of a union query is 256.
create view select * from Jan1998Sales union all select * from Feb1998Sales union all
The order by and compute clauses are allowed only at the end of the union statement to define the order of the final results or to compute summary values.
The group by and having clauses can be used only within individual queries and cannot be used to affect the final result set.
The default evaluation order of a SQL statement containing union operators is left-to-right.
Since union is a binary operation, parentheses must be added to an expression involving more than two queries to specify evaluation order.
The first query in a union statement may contain an into clause that creates a table to hold the final result set. The into statement must be in the first query, or you receive an error message (see Example 2).
insert into sales.overall select * from sales union select * from sales_east
/* Example of invalid command--shows imbalance */ /* in select list items */ select au_id, title_id, au_ord from titleauthor union select stor_id, date from sales
Corresponding columns in the select lists of union statements must occur in the same order, because union compares the columns one-to-one in the order given in the individual queries.
select Cities = city from stores union select city from stores_east order by Cities
The descriptions of the columns that are part of a union operation do not have to be identical. The rules for the datatypes and the corresponding column in the result table are:
Datatype of Columns in union operation |
Datatype of Corresponding Column in Result Table |
---|---|
Not datatype-compatible (data conversion is not handled implicitly by the SAP ASE server) |
Error returned by the SAP ASE server. |
Both are fixed-length character with lengths L1 and L2 |
Fixed-length character with length equal to the greater of L1 and L2. |
Both are fixed-length binary with lengths L1 and L2 |
Fixed-length binary with length equal to the greater of L1 and L2. |
Either or both are variable-length character |
Variable-length character with length equal to the maximum of the lengths specified for the column in the union. |
Either or both are variable-length binary |
Variable-length binary with length equal to the maximum of the lengths specified for the columns in the union. |
Both are numeric datatypes (for example, smallint, int, float, money) |
A datatype equal to the maximum precision of the two columns. For example, if a column in table A is of type int and the corresponding column in table B is of type float, then the datatype of the corresponding column of the result table is float, because float is more precise than int. |
Both column descriptions specify NOT NULL |
Specifies NOT NULL. |
See also convert in Reference Manual: Building Blocks.
ANSI SQL – Compliance level: Entry-level compliant
The use of union in the select clause of an insert statement
Specifying new column headings in the order by clause of a select statement when the union operator is present in the select statement