In SQL Server a view
represents a virtual table. Just like a real table, a view consists of
rows with columns, and you can retrieve data from a view (sometimes even
update data in a view). The fields in the view’s virtual table are the
fields of one or more real tables in the database. You can use views to
join two tables in your database and present the underlying data as if
the data were coming from a single table, thus simplifying the schema of
your database for users performing ad-hoc reporting.
The sample database Northwind in SQL Server has a number of views
installed by default. One example is the “Current Product List” view,
Products AS Product_List
WHERE (Product_List.Discontinued = 0)
From inside an application we can issue the following SQL query to retrieve a set of records representing active products.
SELECT ProductID, ProductName from [Current Product List]
Now with the above view is created...
When to Use..
You need to have a goal in
mind when creating a view. There are a number of scenarios where you
will want to look for a view as a solution.
- To hide the complexity of the underlying database schema, or customize the data and schema for a set of users.
- To control access to rows and columns of data.
- To aggregate data for performance.