SELECT 示例 (Transact-SQL)SELECT Examples (Transact-SQL)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database

本主题提供了使用 SELECT 语句的示例。This topic provides examples of using the SELECT statement.

A.A. 使用 SELECT 检索行和列Using SELECT to retrieve rows and columns

以下示例显示三个代码示例。The following example shows three code examples. 第一个代码示例返回 AdventureWorks2012AdventureWorks2012 数据库的 * 表中的所有行(未指定 WHERE 子句)和所有列(使用了 Product)。This first code example returns all rows (no WHERE clause is specified) and all columns (using the *) from the Product table in the AdventureWorks2012AdventureWorks2012 database.

USE AdventureWorks2012;
GO
SELECT *
FROM Production.Product
ORDER BY Name ASC;
-- Alternate way.
USE AdventureWorks2012;
GO
SELECT p.*
FROM Production.Product AS p
ORDER BY Name ASC;
GO

该示例返回 AdventureWorks2012AdventureWorks2012 数据库的 Name 表的所有行(未指定 WHERE 子句)和列子集(ProductNumberListPriceProduct)。This example returns all rows (no WHERE clause is specified), and only a subset of the columns (Name, ProductNumber, ListPrice) from the Product table in the AdventureWorks2012AdventureWorks2012 database. 此外,还添加了一个列标题。Additionally, a column heading is added.

USE AdventureWorks2012;
GO
SELECT Name, ProductNumber, ListPrice AS Price
FROM Production.Product 
ORDER BY Name ASC;
GO

该示例仅返回 Product 表中产品系列为 R 且生产天数少于 4 的那些行。This example returns only the rows for Product that have a product line of R and that have days to manufacture that is less than 4.

USE AdventureWorks2012;
GO
SELECT Name, ProductNumber, ListPrice AS Price
FROM Production.Product 
WHERE ProductLine = 'R' 
AND DaysToManufacture < 4
ORDER BY Name ASC;
GO

B.B. 将 SELECT 与列标题和列计算一起使用Using SELECT with column headings and calculations

下面的示例返回 Product 表中的所有行。The following examples return all rows from the Product table. 第一个示例返回每种产品的总销售额与总折扣。The first example returns total sales and the discounts for each product. 在第二个示例中,计算每种产品的总收入。In the second example, the total revenue is calculated for each product.

USE AdventureWorks2012;
GO
SELECT p.Name AS ProductName, 
NonDiscountSales = (OrderQty * UnitPrice),
Discounts = ((OrderQty * UnitPrice) * UnitPriceDiscount)
FROM Production.Product AS p 
INNER JOIN Sales.SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID 
ORDER BY ProductName DESC;
GO

该查询将计算每个销售订单中每种产品的收入。This is the query that calculates the revenue for each product in each sales order.

USE AdventureWorks2012;
GO
SELECT 'Total income is', ((OrderQty * UnitPrice) * (1.0 - UnitPriceDiscount)), ' for ',
p.Name AS ProductName 
FROM Production.Product AS p 
INNER JOIN Sales.SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID 
ORDER BY ProductName ASC;
GO

C.C. 将 DISTINCT 与 SELECT 一起使用Using DISTINCT with SELECT

以下示例使用 DISTINCT 以避免检索重复标题。The following example uses DISTINCT to prevent the retrieval of duplicate titles.

USE AdventureWorks2012;
GO
SELECT DISTINCT JobTitle
FROM HumanResources.Employee
ORDER BY JobTitle;
GO

D.D. 使用 SELECT INTO 创建表Creating tables with SELECT INTO

以下第一个示例将在 #Bicycles 中创建一个名为 tempdb 的临时表。The following first example creates a temporary table named #Bicycles in tempdb.

USE tempdb;
GO
IF OBJECT_ID (N'#Bicycles',N'U') IS NOT NULL
DROP TABLE #Bicycles;
GO
SELECT * 
INTO #Bicycles
FROM AdventureWorks2012.Production.Product
WHERE ProductNumber LIKE 'BK%';
GO

第二个示例创建永久表 NewProductsThis second example creates the permanent table NewProducts.

USE AdventureWorks2012;
GO
IF OBJECT_ID('dbo.NewProducts', 'U') IS NOT NULL
    DROP TABLE dbo.NewProducts;
GO
ALTER DATABASE AdventureWorks2012 SET RECOVERY BULK_LOGGED;
GO

SELECT * INTO dbo.NewProducts
FROM Production.Product
WHERE ListPrice > $25 
AND ListPrice < $100;
GO
ALTER DATABASE AdventureWorks2012 SET RECOVERY FULL;
GO

E.E. 使用相关子查询Using correlated subqueries

相关子查询依赖于外部查询来查询其值。A correlated subquery is a query that depends on the outer query for its values. 该查询能为外部查询可能选择的每一行各重复执行一次。This query can be executed repeatedly, one time for each row that may be selected by the outer query.

第一个示例展示了语义等效的查询,以说明使用 EXISTS 关键字和 IN 关键字的区别。The first example shows queries that are semantically equivalent to illustrate the difference between using the EXISTS keyword and the IN keyword. 两个都是有效子查询示例,用于检索产品型号为长袖标志运动衫且 ProductModelID 编号在 ProductProductModel 两个表中相匹配的每种产品名称的实例。Both are examples of a valid subquery that retrieves one instance of each product name for which the product model is a long sleeve logo jersey, and the ProductModelID numbers match between the Product and ProductModel tables.

USE AdventureWorks2012;
GO
SELECT DISTINCT Name
FROM Production.Product AS p 
WHERE EXISTS
    (SELECT *
     FROM Production.ProductModel AS pm 
     WHERE p.ProductModelID = pm.ProductModelID
           AND pm.Name LIKE 'Long-Sleeve Logo Jersey%');
GO

-- OR

USE AdventureWorks2012;
GO
SELECT DISTINCT Name
FROM Production.Product
WHERE ProductModelID IN
    (SELECT ProductModelID 
     FROM Production.ProductModel AS pm
     WHERE p.ProductModelID = pm.ProductModelID
        AND Name LIKE 'Long-Sleeve Logo Jersey%');
GO

下个示例使用 IN 并检索 SalesPerson 表中奖金为 5000.00 且雇员标识号在 EmployeeSalesPerson 表中相匹配的每个雇员姓名的实例。The next example uses IN and retrieves one instance of the first and last name of each employee for which the bonus in the SalesPerson table is 5000.00 and for which the employee identification numbers match in the Employee and SalesPerson tables.

USE AdventureWorks2012;
GO
SELECT DISTINCT p.LastName, p.FirstName 
FROM Person.Person AS p 
JOIN HumanResources.Employee AS e
    ON e.BusinessEntityID = p.BusinessEntityID WHERE 5000.00 IN
    (SELECT Bonus
     FROM Sales.SalesPerson AS sp
     WHERE e.BusinessEntityID = sp.BusinessEntityID);
GO

该语句中前面的子查询无法独立于外部查询进行计算。The previous subquery in this statement cannot be evaluated independently of the outer query. 它需要使用 Employee.EmployeeID 值,但是该值随着 SQL Server 数据库引擎SQL Server Database Engine检查的 Employee 中的不同行而发生改变。It requires a value for Employee.EmployeeID, but this value changes as the SQL Server 数据库引擎SQL Server Database Engine examines different rows in Employee.

相关子查询还可以用于外部查询的 HAVING 子句。A correlated subquery can also be used in the HAVING clause of an outer query. 以下示例查找其最高标价高于其平均标价两倍以上的产品型号。This example finds the product models for which the maximum list price is more than twice the average for the model.

USE AdventureWorks2012;
GO
SELECT p1.ProductModelID
FROM Production.Product AS p1
GROUP BY p1.ProductModelID
HAVING MAX(p1.ListPrice) >= 
    (SELECT AVG(p2.ListPrice) * 2
     FROM Production.Product AS p2
     WHERE p1.ProductModelID = p2.ProductModelID);
GO

此示例使用两个相关子查询查找售出过某种特定产品的雇员的姓名。This example uses two correlated subqueries to find the names of employees who have sold a particular product.

USE AdventureWorks2012;
GO
SELECT DISTINCT pp.LastName, pp.FirstName 
FROM Person.Person pp JOIN HumanResources.Employee e
ON e.BusinessEntityID = pp.BusinessEntityID WHERE pp.BusinessEntityID IN 
(SELECT SalesPersonID 
FROM Sales.SalesOrderHeader
WHERE SalesOrderID IN 
(SELECT SalesOrderID 
FROM Sales.SalesOrderDetail
WHERE ProductID IN 
(SELECT ProductID 
FROM Production.Product p 
WHERE ProductNumber = 'BK-M68B-42')));
GO

F.F. 使用 GROUP BYUsing GROUP BY

以下示例查找数据库中各销售订单的总额。The following example finds the total of each sales order in the database.

USE AdventureWorks2012;
GO
SELECT SalesOrderID, SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID
ORDER BY SalesOrderID;
GO

由于使用了 GROUP BY 子句,因此针对每个销售订单只返回一行销售总额。Because of the GROUP BY clause, only one row containing the sum of all sales is returned for each sales order.

G.G. 对多个组使用 GROUP BYUsing GROUP BY with multiple groups

以下示例查找按产品 ID 和特价产品 ID 分组的平均价格和迄今为止的年销售总额。The following example finds the average price and the sum of year-to-date sales, grouped by product ID and special offer ID.

USE AdventureWorks2012;
GO
SELECT ProductID, SpecialOfferID, AVG(UnitPrice) AS [Average Price], 
    SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail
GROUP BY ProductID, SpecialOfferID
ORDER BY ProductID;
GO

H.H. 使用 GROUP BY 和 WHEREUsing GROUP BY and WHERE

以下示例在只检索标价大于 $1000 的行后,将结果进行分组。The following example puts the results into groups after retrieving only the rows with list prices greater than $1000.

USE AdventureWorks2012;
GO
SELECT ProductModelID, AVG(ListPrice) AS [Average List Price]
FROM Production.Product
WHERE ListPrice > $1000
GROUP BY ProductModelID
ORDER BY ProductModelID;
GO

I.I. 将 GROUP BY 与表达式一起使用Using GROUP BY with an expression

以下示例按表达式进行分组。The following example groups by an expression. 如果表达式不包含聚合函数,则可以按表达式进行分组。You can group by an expression if the expression does not include aggregate functions.

USE AdventureWorks2012;
GO
SELECT AVG(OrderQty) AS [Average Quantity], 
NonDiscountSales = (OrderQty * UnitPrice)
FROM Sales.SalesOrderDetail
GROUP BY (OrderQty * UnitPrice)
ORDER BY (OrderQty * UnitPrice) DESC;
GO

J.J. 将 GROUP BY 与 ORDER BY 一起使用Using GROUP BY with ORDER BY

以下示例查找每种产品的平均价格并按平均价格将结果排序。The following example finds the average price of each type of product and orders the results by average price.

USE AdventureWorks2012;
GO
SELECT ProductID, AVG(UnitPrice) AS [Average Price]
FROM Sales.SalesOrderDetail
WHERE OrderQty > 10
GROUP BY ProductID
ORDER BY AVG(UnitPrice);
GO

K.K. 使用 HAVING 子句Using the HAVING clause

下面的第一个示例显示带聚合函数的 HAVING 子句。The first example that follows shows a HAVING clause with an aggregate function. 该子句按产品 ID 将 SalesOrderDetail 表中的行进行分组并消除那些平均订单数量等于或小于五的产品。It groups the rows in the SalesOrderDetail table by product ID and eliminates products whose average order quantities are five or less. 第二个示例显示不带聚合函数的 HAVING 子句。The second example shows a HAVING clause without aggregate functions.

USE AdventureWorks2012;
GO
SELECT ProductID 
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING AVG(OrderQty) > 5
ORDER BY ProductID;
GO

该查询在 LIKE 子句中使用 HAVING 子句。This query uses the LIKE clause in the HAVING clause.

USE AdventureWorks2012 ;  
GO  
SELECT SalesOrderID, CarrierTrackingNumber   
FROM Sales.SalesOrderDetail  
GROUP BY SalesOrderID, CarrierTrackingNumber  
HAVING CarrierTrackingNumber LIKE '4BD%'  
ORDER BY SalesOrderID ;  
GO  

L.L. 使用 HAVING 和 GROUP BYUsing HAVING and GROUP BY

以下示例显示在一个 GROUP BY 语句中使用 HAVINGWHEREORDER BYSELECT 子句。The following example shows using GROUP BY, HAVING, WHERE, and ORDER BY clauses in one SELECT statement. 该语句生成组和汇总值(但是组和汇总值是在消除价格超过 $25 且平均订单数量低于 5 的产品之后得出的)。It produces groups and summary values but does so after eliminating the products with prices over $25 and average order quantities under 5. 它还按 ProductID 组织其结果。It also organizes the results by ProductID.

USE AdventureWorks2012;
GO
SELECT ProductID 
FROM Sales.SalesOrderDetail
WHERE UnitPrice < 25.00
GROUP BY ProductID
HAVING AVG(OrderQty) > 5
ORDER BY ProductID;
GO

M.M. 将 HAVING 与 SUM 和 AVG 一起使用Using HAVING with SUM and AVG

以下示例按产品 ID 将 SalesOrderDetail 表进行分组,结果中仅包含订单总金额超过 $1000000.00 且其平均订单数量少于 3 的产品的组。The following example groups the SalesOrderDetail table by product ID and includes only those groups of products that have orders totaling more than $1000000.00 and whose average order quantities are less than 3.

USE AdventureWorks2012;
GO
SELECT ProductID, AVG(OrderQty) AS AverageQuantity, SUM(LineTotal) AS Total
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) > $1000000.00
AND AVG(OrderQty) < 3;
GO

若要查看总销售额超过 $2000000.00 的产品,请使用以下查询:To see the products that have had total sales greater than $2000000.00, use this query:

USE AdventureWorks2012;
GO
SELECT ProductID, Total = SUM(LineTotal)
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) > $2000000.00;
GO

若要确保在计算中至少为每种产品包含 1500 项,请使用 HAVING COUNT(*) > 1500 消除返回总数少于 1500 售出项的产品。If you want to make sure there are at least one thousand five hundred items involved in the calculations for each product, use HAVING COUNT(*) > 1500 to eliminate the products that return totals for fewer than 1500 items sold. 该查询如下所示:The query looks like this:

USE AdventureWorks2012;
GO
SELECT ProductID, SUM(LineTotal) AS Total
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING COUNT(*) > 1500;
GO

N.N. 使用 INDEX 优化器提示Using the INDEX optimizer hint

以下示例说明了使用 INDEX 优化器提示的两种方式。The following example shows two ways to use the INDEX optimizer hint. 第一个示例说明如何强制优化器使用非聚集索引检索表中的行,第二个示例使用索引 0 强制执行表扫描。The first example shows how to force the optimizer to use a nonclustered index to retrieve rows from a table, and the second example forces a table scan by using an index of 0.

USE AdventureWorks2012;
GO
SELECT pp.FirstName, pp.LastName, e.NationalIDNumber
FROM HumanResources.Employee AS e WITH (INDEX(AK_Employee_NationalIDNumber))
JOIN Person.Person AS pp on e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO

-- Force a table scan by using INDEX = 0.
USE AdventureWorks2012;
GO
SELECT pp.LastName, pp.FirstName, e.JobTitle
FROM HumanResources.Employee AS e WITH (INDEX = 0) JOIN Person.Person AS pp
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO

M.M. 使用 OPTION 和 GROUP 提示Using OPTION and the GROUP hints

以下示例说明了如何将 OPTION (GROUP) 子句与 GROUP BY 子句一起使用。The following example shows how the OPTION (GROUP) clause is used with a GROUP BY clause.

USE AdventureWorks2012;
GO
SELECT ProductID, OrderQty, SUM(LineTotal) AS Total
FROM Sales.SalesOrderDetail
WHERE UnitPrice < $5.00
GROUP BY ProductID, OrderQty
ORDER BY ProductID, OrderQty
OPTION (HASH GROUP, FAST 10);
GO

O.O. 使用 UNION 查询提示Using the UNION query hint

以下示例使用 MERGE UNION 查询提示。The following example uses the MERGE UNION query hint.

USE AdventureWorks2012;
GO
SELECT BusinessEntityID, JobTitle, HireDate, VacationHours, SickLeaveHours
FROM HumanResources.Employee AS e1
UNION
SELECT BusinessEntityID, JobTitle, HireDate, VacationHours, SickLeaveHours
FROM HumanResources.Employee AS e2
OPTION (MERGE UNION);
GO

P.P. 使用简单 UNIONUsing a simple UNION

在下面的示例中,结果集同时包含 ProductModelIDName 表中的 ProductModelGloves 列中的内容。In the following example, the result set includes the contents of the ProductModelID and Name columns of both the ProductModel and Gloves tables.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.Gloves', 'U') IS NOT NULL
DROP TABLE dbo.Gloves;
GO
-- Create Gloves table.
SELECT ProductModelID, Name
INTO dbo.Gloves
FROM Production.ProductModel
WHERE ProductModelID IN (3, 4);
GO

-- Here is the simple union.
USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves
ORDER BY Name;
GO

Q.Q. 将 SELECT INTO 与 UNION 一起使用Using SELECT INTO with UNION

在下面的示例中,第二个 INTO 语句中的 SELECT 子句指定名为 ProductResults 的表保存 ProductModelGloves 表中的指定列的并集(最终结果集)。In the following example, the INTO clause in the second SELECT statement specifies that the table named ProductResults holds the final result set of the union of the designated columns of the ProductModel and Gloves tables. 注意,Gloves 表是由第一个 SELECT 语句创建的。Note that the Gloves table is created in the first SELECT statement.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.ProductResults', 'U') IS NOT NULL
DROP TABLE dbo.ProductResults;
GO
IF OBJECT_ID ('dbo.Gloves', 'U') IS NOT NULL
DROP TABLE dbo.Gloves;
GO
-- Create Gloves table.
SELECT ProductModelID, Name
INTO dbo.Gloves
FROM Production.ProductModel
WHERE ProductModelID IN (3, 4);
GO

USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
INTO dbo.ProductResults
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves;
GO

SELECT ProductModelID, Name 
FROM dbo.ProductResults;

R.R. 将 ORDER BY 与两个 SELECT 语句的 UNION 一起使用Using UNION of two SELECT statements with ORDER BY

在 UNION 子句中使用的某些参数的顺序非常重要。The order of certain parameters used with the UNION clause is important. 下面的示例通过两个 UNION 语句说明 SELECT 的错误用法和正确用法(在这两个语句的输出中将重命名一个列)。The following example shows the incorrect and correct use of UNION in two SELECT statements in which a column is to be renamed in the output.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.Gloves', 'U') IS NOT NULL
DROP TABLE dbo.Gloves;
GO
-- Create Gloves table.
SELECT ProductModelID, Name
INTO dbo.Gloves
FROM Production.ProductModel
WHERE ProductModelID IN (3, 4);
GO

/* INCORRECT */
USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
ORDER BY Name
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves;
GO

/* CORRECT */
USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves
ORDER BY Name;
GO

S.S. 使用三个 SELECT 语句的 UNION 来说明 ALL 和括号的作用Using UNION of three SELECT statements to show the effects of ALL and parentheses

下列示例使用 UNION 来组合具有相同 5 行数据的三个表的结果。The following examples use UNION to combine the results of three tables that all have the same 5 rows of data. 第一个示例使用 UNION ALL 显示重复记录,返回所有的 15 行。The first example uses UNION ALL to show the duplicated records, and returns all 15 rows. 第二个示例使用不带 UNIONALL,删除三个 SELECT 语句的组合结果中的重复行,返回 5 行。The second example uses UNION without ALL to eliminate the duplicate rows from the combined results of the three SELECT statements, and returns 5 rows.

第三个示例将 ALL 用于第一个 UNION,并用括号将第二个没有使用 UNIONALL 括起来。The third example uses ALL with the first UNION and parentheses enclose the second UNION that is not using ALL. 第二个 UNION 因位于括号内而首先得到处理,并且因为没有使用 ALL 选项,所以重复行被删除而返回 5 行。The second UNION is processed first because it is in parentheses, and returns 5 rows because the ALL option is not used and the duplicates are removed. 通过使用 SELECT 关键字将这 5 行与第一个 UNION ALL 的结果组合在一起。These 5 rows are combined with the results of the first SELECT by using the UNION ALL keywords. 这不会删除两个 5 行结果集之间的重复行。This does not remove the duplicates between the two sets of 5 rows. 最终结果有 10 行。The final result has 10 rows.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.EmployeeOne', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeOne;
GO
IF OBJECT_ID ('dbo.EmployeeTwo', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeTwo;
GO
IF OBJECT_ID ('dbo.EmployeeThree', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeThree;
GO

SELECT pp.LastName, pp.FirstName, e.JobTitle 
INTO dbo.EmployeeOne
FROM Person.Person AS pp JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO
SELECT pp.LastName, pp.FirstName, e.JobTitle 
INTO dbo.EmployeeTwo
FROM Person.Person AS pp JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO
SELECT pp.LastName, pp.FirstName, e.JobTitle 
INTO dbo.EmployeeThree
FROM Person.Person AS pp JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO
-- Union ALL
SELECT LastName, FirstName, JobTitle
FROM dbo.EmployeeOne
UNION ALL
SELECT LastName, FirstName ,JobTitle
FROM dbo.EmployeeTwo
UNION ALL
SELECT LastName, FirstName,JobTitle 
FROM dbo.EmployeeThree;
GO

SELECT LastName, FirstName,JobTitle
FROM dbo.EmployeeOne
UNION 
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeTwo
UNION 
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeThree;
GO

SELECT LastName, FirstName,JobTitle 
FROM dbo.EmployeeOne
UNION ALL
(
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeTwo
UNION
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeThree
);
GO

另请参阅See Also

CREATE TRIGGER (Transact-SQL) CREATE TRIGGER (Transact-SQL)
CREATE VIEW (Transact-SQL) CREATE VIEW (Transact-SQL)
DELETE (Transact-SQL) DELETE (Transact-SQL)
EXECUTE (Transact-SQL) EXECUTE (Transact-SQL)
表达式 (Transact-SQL) Expressions (Transact-SQL)
INSERT (Transact-SQL) INSERT (Transact-SQL)
LIKE (Transact-SQL) LIKE (Transact-SQL)
UNION (Transact-SQL) UNION (Transact-SQL)
EXCEPT 和 INTERSECT (Transact-SQL) EXCEPT and INTERSECT (Transact-SQL)
UPDATE (Transact-SQL) UPDATE (Transact-SQL)
WHERE (Transact-SQL) WHERE (Transact-SQL)
PathName (Transact-SQL) PathName (Transact-SQL)
INTO 子句 (Transact-SQL)INTO Clause (Transact-SQL)