创建视图

CREATE VIEW view_EmployeeInfo
AS   
    SELECT EmployeeID,
        FirstName,
        LastName,
        HireDate  
    FROM Employee
GO

可以像表格一样选择视图中的行:

SELECT FirstName
FROM view_EmployeeInfo

你还可以使用计算列创建视图。我们可以通过添加计算列来修改上面的视图:

CREATE VIEW view_EmployeeReport
AS   
    SELECT EmployeeID,
        FirstName,
        LastName,
        Coalesce(FirstName,'') + ' ' + Coalesce(LastName,'') as FullName,
        HireDate  
    FROM Employee
GO

此视图添加了一个额外的列,当你从中获取 1 行时将显示该列。此附加列中的值将取决于表 Employee 中的字段 FirstNameLastName,并将在更新这些字段时自动更新。