The TSQL UNION ALL operator is used to
combine the result sets of 2 or more SELECT statements. It returns all rows
from the query (even if the row exists in more than one of the SELECT
statements).
Return single field
Each SELECT statement within the UNION ALL must
have the same number of fields in the result sets with similar data types.
The syntax for the TSQL UNION ALL operator is:
SELECT expression1, expression2, ... expression_n
FROM tables
WHERE conditions
UNION ALL
SELECT expression1, expression2, ... expression_n
FROM tables
WHERE conditions;
There must be same number of expressions in both SELECT
statements.
Return single field
The following is an example of the TSQL UNION ALL
operator that returns one field from multiple SELECT statements (and both
fields have the same data type):
SELECT customer_id
FROM customer
UNION ALL
SELECT customer_id
FROM orders;
This TSQL UNION ALL example would return a customer_id
multiple times in your result set if the customer_id appeared in both the
suppliers and orders table. The TSQL UNION ALL operator does not remove duplicates. If you wish to remove duplicates, try using the
UNION operator.
No comments:
Post a Comment