建立檢視

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,並將在更新這些欄位時自動更新。