Views in SQL Server 2000, Part 4
Table of Contents Using viewsYou can use a view to retrieve data, or update data. A view can be used whenever a table could be used, for retrieving data. For updating data, this may not always be true, and it depends on the view. Retrieve dataWe have already seen several examples of retrieving data through a view. I will now compare two queries, one where we use a view, and one without. SELECT TOP 1 * FROM vwProductSales ORDER BY Total DESC
SELECT
TOP 1 Products.ProductName,
SUM(OD.UnitPrice*OD.Quantity*(1-OD.Discount)) AS Total
FROM [Order Details] AS OD
INNER JOIN Products ON OD.ProductID = Products.ProductID
GROUP BY Products.ProductName
ORDER BY Total DESC
Now, which one of these two queries do you find most simple to use? The first one? Yeah, though so! Update dataThere are some restrictions for updating data through a view.
Also be aware of columns that cannot contain NULL. If it has no default value, and is not referenced in the view, you will not be able to update the column. Drop viewsDeleting a view is not complicated. It’s as easy as: DROP VIEW vwProductSalesTop10 Alter viewsAlter views is as simple as deleting them. We can try to remove the encryption of the view vwProductSalesEncrypted
ALTER VIEW vwProductSalesEncrypted
AS
SELECT Products.ProductName, SUM(OD.UnitPrice*OD.Quantity*(1-OD.Discount)) AS Total
FROM [Order Details] AS OD
INNER JOIN Products ON OD.ProductID = Products.ProductID
GROUP BY Products.ProductName
GO
Output: [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ Next ] |