![]() |
View Maintenance |
|
The Properties of a View |
|
In Transact-SQL, a view is considered an object. As such, it can be viewed, changed, or deleted. Like any regular object, a view has its own characteristics. To see them in Microsoft SQL Server Management Studio, you can right-click the view and click Properties. A View Properties dialog box would come up. It can give you information such as the name of the database the view belongs to, the date the view was created, etc. |
After a view has been created, either by you or someone else, you may find out that it has an unnecessary column, it needs a missing column, it includes unnecessary records, or some records are missing. Fortunately, you can change the structure or the code of a view. This is referred to as altering a view. You have various options:
The basic formula to programmatically modify a view is: ALTER VIEW ViewName AS SELECT Statement You start the alteration with the ALTER VIEW expression followed by the name of the view. After the name of the view, use the AS keyword to specify that you are ready to show the change. After the AS keyword, you can then define the view as you see fit. For example, you can create a SELECT statement that includes a modification of the existing code or a completely new statement. In the view we created to show a list of men of a table, we included a column for the sex. This column is useless or redundant because we already know that the list includes only men. Here is an example of altering the view to remove (or rather omit) the Sex column of the Persons table: ALTER VIEW dbo.ListOfMen AS SELECT dbo.Persons.FirstName, dbo.Persons.LastName FROM dbo.Sexes INNER JOIN dbo.Persons ON dbo.Sexes.SexID = dbo.Persons.SexID WHERE (dbo.Sexes.Sex = 'Male');
Instead of modifying a view, if you find it altogether useless, you can remove it from its database. You have various options. To delete a view:
The formula to programmatically delete a view is: DROP VIEW ViewName On the right side of the DROP VIEW expression, enter the name of the undesired view and execute the statement. You will not be warned before the interpreter deletes the view. If you are programmatically creating a Windows application, of course you can use a conditional statement to assist the user with deciding whether to continue deleting the view or not. |
|
|
||
| Previous | Copyright © 2008-2010 FunctionX, Inc. | Next |
|
|
||