You are on page 1of 3

Selecting Data Through Views Selecting Data Through Views Saved queries that can be used as data sources

s Protects data from errors Useful when used with simplified ad-hoc queries Use views to flatten complex joins You can use view in simplifying aggregate queries View is non other than a save SQL SELECT statements or subqueries Design your views as generic as possible It is not advisable to add ORDER BY in view since the outer query may provide its own ORDER BY clause Views cannot point to a tempdb table Syntax
CREATE VIEW viewName AS selectStatement

Declaring a View
CREATE VIEW dbo.vEventList AS SELECT dbo.CustomerType.Name AS 'Customer', dbo.Customer.LastName, dbo.Customer.FirstName, dbo.Customer.NickName, dbo.Event_mm_Customer.ConfirmDate, dbo.Event.Code, dbo.Event.DateBegin, dbo.Tour.Name AS 'Tour', dbo.BaseCamp.Name, dbo.Event.Comment FROM dbo.Tour INNER JOIN dbo.Event ON dbo.Tour.TourId = dbo.Event.TourId INNER JOIN dbo.Event_mm_Customer ON dbo.Event.EventId = dbo.Event_mm_Customer.EventId INNER JOIN dbo.Customer ON dbo.Event_mm_Customer.CustomerId = dbo.Customer.CustomerId LEFT OUTER JOIN dbo.CustomerType ON dbo.Customer.CustomerTypeId = dbo.CustomerType.CustomerTypeId INNER JOIN dbo.BaseCamp ON dbo.Tour.BaseCampId = dbo.BaseCamp.BaseCampId GO

Executing a View
SELECT * FROM dbo.vEventList WHERE (Code = '01-016')

How to Check if View is Existing


IF EXISTS (SELECT * FROM SysObjects WHERE Name = 'vMyEvent') DROP VIEW dbo.vMyEvent

Selecting Data Through Views Preventing Disappearing Rows You cannot view a record that you inserted in the view if it doesnt qualified in WHERE clause of the View WITH CHECK OPTION in View enforces the WHERE clause to check whether you are inserting a valid data or not
ALTER VIEW vCapeHatterasTour AS SELECT [Name], BaseCampId FROM dbo.Tour WHERE BaseCampId = 2 WITH CHECK OPTION -- The View prevents in inserting this record INSERT INTO vCapeHatterasTour([Name], BaseCampId) VALUES('Blue Ridge Parkway Hike', 1)

Protecting from Schema Changes View always depend on schema Your View will be out of sync if you dont update your View based on the changes you made to your schema Schema Binding Schema prevents you from changing the structure of a table if a View is referencing to that schema
IF EXISTS (SELECT * FROM SysObjects WHERE [Name] = 'Test') DROP TABLE dbo.Test CREATE TABLE Test( [Name] NVARCHAR(20) ) -- Prevents any changes in the schema which columns defined in the view CREATE VIEW dbo.vTest WITH SCHEMABINDING AS SELECT [Name] FROM dbo.Test

Correcting schema/view out of sync SP_REFRESHVIEW nameOfView refreshes the metadata of your views according to the changes you made in your schema You cannot use the SP_REFRESHVIEW stored procedure to temp table
USE CHA2 SP_REFRESHVIEW 'EventList'

Selecting Data Through Views Nested Views


CREATE VIEW dbo.vBaseCampCount AS SELECT dbo.vEventList.[Name] AS 'BaseCamp', COUNT(*) AS 'Attendees' FROM dbo.vEventList GROUP BY dbo.vEventList.[Name]

You might also like