sql sever怎么在视图里查询
时间: 2024-05-04 12:16:58 浏览: 74
要在 SQL Server 中创建一个视图,您可以使用CREATE VIEW语句。以下是一个示例:
```
CREATE VIEW [dbo].[vw_EmployeeDetails]
AS
SELECT
EmployeeID,
FirstName,
LastName,
Gender,
Email,
Phone,
AddressLine1,
City,
StateProvinceName,
PostalCode,
CountryRegionName,
Title,
HireDate,
BirthDate
FROM
[HumanResources].[vEmployeeDepartmentHistory]
INNER JOIN [Person].[Address] ON [HumanResources].[vEmployeeDepartmentHistory].[AddressID] = [Person].[Address].[AddressID]
INNER JOIN [Person].[StateProvince] ON [Person].[Address].[StateProvinceID] = [Person].[StateProvince].[StateProvinceID]
INNER JOIN [Person].[CountryRegion] ON [Person].[StateProvince].[CountryRegionCode] = [Person].[CountryRegion].[CountryRegionCode]
INNER JOIN [HumanResources].[Employee] ON [HumanResources].[vEmployeeDepartmentHistory].[EmployeeID] = [HumanResources].[Employee].[EmployeeID]
```
在视图中查询数据与在表中查询数据相同。例如,如果你要查询上面的视图,你可以使用SELECT语句:
```
SELECT * FROM [dbo].[vw_EmployeeDetails]
```
这将返回视图中的所有列和行。
阅读全文