A view is a select query that is held on the server. They are used to focus, simplify, and customize each user’s perception of the database. They can also be used as a security mechanism for giving users persmissions to view only ceratin elements of data in tables rather than all data.
They can also be used to simplify the data the end user sees.
Unlike stored procedures, views can be used in joins, where clauses etc.
As an example, supposing we have a personnel table with the firstName, surname and bank details of all personnel. If you create a view which only selects the FirstName and LastName, The bank details are not shown, Thus you can put select permissions on the view, but NOT the table.
CREATE VIEW vuPersonnel AS
SELECT FirstName, LastName from tblPersonnel
As another example, supposing we have a complex method for storing data such as a integer field that is split as a bit field to store the status of an order. The order can be Sent, Invoiced, Received etc.
The user will not understand that a value of 11 means that the order has been Sent, invoiced and Acknowledged so we can provide a view that we can join on to give the current status of the order eg.
Create View vuOrderState
AS
SELECT OrderId,
(CASE WHEN (MyFlag & 0x0001) <> 0 THEN 1 ELSE 0 END) Sent,
(CASE WHEN (MyFlag & 0x0002) <> 0 THEN 1 ELSE 0 END) Invoiced,
(CASE WHEN (MyFlag & 0x0080) <> 0 THEN 1 ELSE 0 END) received,
(CASE WHEN (MyFlag & 0x0004) <> 0 THEN 1 ELSE 0 END) Acknowledged
.................
FROM tblOrders
An end user Query could look like this (excuse the * )
Select * from tblorders OR
Inner JOIN vuOrderState VOR
ON VOR.OrderId = OR.OrderID
Where VOR.Recevived = 1
and OR.Invoiced = 1
Hope this is usefull and makes sense,
Chris Dukes