Views are saved queries. They are defined with a single SELECT statement.
USE AdventureWorks2012;
GO
CREATE VIEW HumanResources.EmployeeList
AS
SELECT BusinessEntityID,
JobTitle,
HireDate,
VacationHours
FROM HumanResources.Employee;
GO
SELECT * FROM HumanResources.EmployeeList;
GO
A VIEW is simply a virtual table consisting of different columns from one or more tables.
More examples:
USE inventory;
GO
CREATE VIEW CustomersSalesmenOrders_view
(NumberCustomers, AvgOrders, TotalOrders, OrderDate)
AS
SELECT Count(customer_id),
Avg(purch_amt),
Sum(purch_amt),
ord_date
FROM orders
GROUP BY ord_date;
GO
USE inventory;
GO
CREATE VIEW SalesmenNY13_view
AS
SELECT *
FROM salesman
WHERE city = 'New York'
AND commission > 0.13;
GO
Comentários