Click here to Skip to main content
Email Password   helpLost your password?

Aims of this Article 

What is a View?

A view is simply a query stored as an object in the database. In essence a view can be thought of a virtual table, that in itself does not contain any data or information. A view can return the same data that we could from any SELECT statement. Views are implemented as stored SELECT statements that have virtually no overhead because they don't actually store data. However, a view is not a tool for processing data using T-SQL commands, like a stored procedure is. A view is only able to hold one query at a time. Therefore, a view is more of a query than a stored procedure. Just as with a stored procedure or a query within a Query Editor pane, you can include tables from databases that are running on different servers. Providing the user ID has the necessary security credentials, it is possible to include tables from several databases.

Where Do Views Come From and Why?

Views have been available throughout the history of Microsoft SQL Server. When working on a fully normalised database and querying the data, there is often a requirement to write complex joins, aggregates, functions and plethora of other T_SQL functions to produce the results. To mask this complexity, this statement can be created as a view, and then future queries can be preformed against the view.

Products like Oracle have come out with the concept of a Materialized View that give an additional performance. However, using views that return very large result sets can lead to poor performance, as the result set is not indexed and the entire result must be table scanned if the view is used in a join or a subquery. A T-SQL boost to this problem is by being able to have indexes built on a view. So in the continuing evolution of the SQL Server product line and in response to Oracle's Materialized View, Microsoft SQL Server 2000 has a new feature called the View Index. View Indexes give the product the capability to define an index on a view. Additionally, SQL Server View Indexes are dynamic in that changes to the data in the base tables are automatically reflected in the indexed view. Also the SQL Server query optimizer will try to use an indexed view even if the view is not referenced in the from clause of a T-SQL command. These features are not available in Oracle's Materialized Views.

Advantages of Views

Are There Any Guidelines for Creating Views?

Choose an intuitive name that defines your view, such as dbo.Customer_Order_View, the owner name is optional, but it should be specified to avoid broken ownership chains (assuming the referenced DB objects are owned by DBO). When a view is created, the existence of objects referenced is verified. SELECT permission on the object are required if the owner differs from that of the view. If the view references derived columns, or if columns from multiple tables have the same name, column names must be specified. Column names can be aliased in order to make them user friendly.

Are There Restrictions on Creating a View?

How Do I Create a View?

Views can be created in a variety of ways depending on which version of SQL server you are using:

  1. Using T-SQL statements
  2. Using Enterprise manager
  3. Using SQL 2005 Management studio

How Do I Create a View using T-SQL Syntax?

CREATE VIEW owner.viewname [(Column[,n])]
    select statement
[WITH CHECK OPTION]

Example

CREATE VIEW uk_employee_view
    SELECT * FROM employee WHERE country = 'UK'
WITH CHECK OPTION

What is the WITH CHECK OPTION ?

If a view has been defined with the WITH CHECK OPTION, any modification to the underlying data can result in that data no longer being accessible through the view. This forces all data modification statements executed against the view to adhere to the criteria set within select_statement. When a row is modified through a view, the WITH CHECK OPTION guarantees that the data remains visible through the view after the modification has been committed.

Create a View using SQL 2000 Enterprise Manager

You can create views through the enterprise manager by accessing the view designer. To access the view designer, right click on the views in the appropriate database and select new view. The view designer is actually based on the Query designer with one slight difference - when you select save, you are prompted for a name for the view and the query you have built is stored as the view definition.

After you are in the view designer, you can click on the Add Table icon (it's the one to the far right in the toolbar). Selecting the check boxes for the appropriate columns adds them to the select statement that will be saved as your view definition.

To change the properties of the view, i.e. adding the WITH CHECK option, select the properties icon or right click and select properties from the pop up menu.

Creating a View using SQL2005 Management Studio

In the SQL Management studio, navigate to the database you would like to create the view in. Navigate to the views node in the treeview and right click it. This will bring up a pop menu with New View as an option. From here, the procedure is very similar to the SQL 2000 one.

How Can I Use a View as Security?

As a developer, one of your concerns when building a view is what happens when someone makes changes to the structure of an underlying table used in your view. This causes problems. I will show you how to get around this problem and secure the build of a view so that this sort of thing doesn't happen.

How and Why Would I Encrypt View Definitions?

The most common situation where you will find views encrypted is when using a view
to return specific information from a database. You also don't wish anyone to see how that information was returned, for example not allowing contractor application developers see the structure of a source database. Encrypting the T-SQL code that makes up the view, which would mean that how the information was being returned would not be visible.

Indexing a View

Just as tables can be indexed so can views, and the rules for choosing columns to make indexes are similar to those for a table, however there are some major requirements one needs to meet before one can index a view.

The first index to be created must be a unique clustered index, once this index has been created, additional non clustered indexes on the view can be created. An important note to bear in mind here is that if you drop the clustered index on the view, then all subsequent indexes on the view will be automatically dropped. Also if we drop a view, all the indexes will be dropped too. The view that the index is to be built on must only contain tables and not other views. The tables must all come from one database, and the view must also reside in that database and have been built with the SCHEMABINDING option.

The following options must be set to ON while creating an index on a view. These options need only be set to ON for that session and therefore would precede the CREATE INDEX statement.

SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
/* */
SET NUMERIC_ROUNDABORT OFF
SET IGNORE_DUP_KEY OFF

To be continued...

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalok article
Donsw
12:40 23 Feb '09  
Good intro, You should also show other article or books if someone wants to read more.

cheers,
Donsw
My Recent Article : Optimistic Concurrency with C# using the IOC and DI Design Patterns

GeneralMy vote of 2
Ardavan Sharifi
21:16 30 Jan '09  
because it's is very easy and not complete.
QuestionWhat about parametrized views?
ruben ruvalcaba
5:36 22 May '07  
The article is a great introduction to the use of views, just yesterday I was looking for something like this..very useful, thanks

I just made a View that helps me to join information of many tables, but I want to use parameters inside the view's select so it can filter the inner data when building the view ... is it possible? at this time the only way I found out is creating an stored procedure and forget the view... is it possible to use parameters in a view?

Regards
AnswerRe: What about parametrized views?
cykophysh39
7:54 22 May '07  
Views cannot accept Parameters.
The way to do it is to build your view as wht you require,
Then you can use a Stored Procedure to query the view and the stored procedure can fileter the result set as per your paramenters


Kind Regards,
Gary


My Website || My Blog || My Articles
q+
AnswerRe: What about parametrized views?
Member 3662818
16:19 1 Feb '09  
Eg: Select * from View Where [your filter...]
Generalcopy&paste from some book?
chopeen
23:59 14 May '07  
In Chapter 11, we look at how to group data ...
GeneralRe: copy&paste from some book?
cykophysh39
3:38 15 May '07  
explain?



Kind Regards,
Gary


My Website || My Blog || My Articles

GeneralRe: copy&paste from some book?
cykophysh39
4:16 15 May '07  
lol , i see now thats was something I using as a reference material,

I will remove it now

Thanks


Kind Regards,
Gary


My Website || My Blog || My Articles

Generalnice one
Nirosh
12:27 30 Apr '07  
Hi,

The content of the article is rich to give the base introduction of the table view.. but the article format is very poor.. I guess that is why you are having a lower rating..

Any way I will give my 5 hoping to see a update ASAP..






L.W.C. Nirosh.
Colombo,
Sri Lanka.

GeneralRe: nice one
cykophysh39
12:58 30 Apr '07  
Hi Thanks for your feedback.Much Appreciated.
How do you think the article format can be improved?


Kind Regards,
Gary


My Website || My Blog || My Articles

GeneralRe: nice one
Nirosh
4:49 1 May '07  
One thing.. focus on H2 tag.. for last two headning you have used h2 tag but not for any of the above.. the article does not have a main title at all in the cotent page..

Another thing.. the point listed under aims section does not keep a consistent link with the headings of the artcile..

One more.. what that top part of the article is all about?.. no one is using that these days.. also the format of it, is horrible..





L.W.C. Nirosh.
Colombo,
Sri Lanka.

GeneralRe: nice one
cykophysh39
10:02 8 May '07  
Thanks for your input. I have updated the format a bit, I will hopefully update the article content within the next week or so.



Kind Regards,
Gary


My Website || My Blog || My Articles

GeneralProblems with generated Datasets and views in ASP.NET 2.0
Magnus Salgo
0:32 19 Sep '06  
:( I found that when creating Typed datasets with Visualo Studio 2005 it couldn't generate the columns when I used views in stored procedures.... ==> I need to use the tables in the stored procedure ...

Magnus Salgo
GeneralRe: Problems with generated Datasets and views in ASP.NET 2.0
cykophysh39
1:16 19 Sep '06  
Thats intresting I will try that out tonight when I get home and get back to you!!
OMG

"a fool will not learn from a wise man, but a wise man will learn from a fool"

"The only thing worse than failure, is never having tried at all"


Last Updated 30 Jan 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010