Creates a view on the database.
CREATE [ OR REPLACE ] VIEW [ owner.]view-name [ ( column-name, ... ) ] AS select-statement [ WITH CHECK OPTION ]
OR REPLACE clause Specifying OR REPLACE (CREATE OR REPLACE VIEW) creates a new view, or replaces an existing view with the same name. Existing permissions are preserved when you use the OR REPLACE clause, but INSTEAD OF triggers on the view are dropped.
AS clause The SELECT statement on which the view is based. The SELECT statement must not refer to local temporary tables. Also, the SELECT statement can have a GROUP BY, HAVING, WINDOW, or ORDER BY clause, and can contain UNION, EXCEPT, or INTERSECT or a common table expression. However, you can affect the results of a view definition by using a SELECT with an ORDER BY clause in combination with the FIRST or TOP clauses.
WITH CHECK OPTION clause The WITH CHECK OPTION clause rejects any updates and inserts to the view that do not meet the criteria of the view as defined by its SELECT statement.
Views are used to give a different perspective on the data, even though it is not stored that way. The CREATE VIEW statement creates a view with the given name. You can create a view owned by another user by specifying the owner. You must have DBA authority to create a view for another user.
A view name can be used in place of a table name in SELECT, DELETE, UPDATE, and INSERT statements. Views, however, do not physically exist in the database as tables. They are derived each time they are used. The view is derived as the result of the SELECT statement specified in the CREATE VIEW statement. Table names used in a view should be qualified by the user ID of the table owner. Otherwise, a different user ID might not be able to find the table or might get the wrong table.
Views can be updated unless the SELECT statement defining the view contains a GROUP BY clause, a WINDOW clause, an aggregate function, or involves a set operator (UNION, INTERSECT, EXCEPT). An update to the view causes the underlying table(s) to be updated.
The columns in the view are given the names specified in the column-name list. If the column name list is not specified, the view columns are given names from the select list items. All items in the select list must have unique names. To use the names from the select list items, each item must be a simple column name or have an alias-name specified. See SELECT statement.
Typically, a view references tables and views (and their respective attributes) that are defined in the catalog. However, a view can also reference SQL variables. In this case, when a query that references the view is executed, the value of the SQL variable is used. Views that reference SQL variables are called parameterized views since the variables act as parameters to the execution of the view.
Parameterized views offer an alternative to embedding the body of an equivalent SELECT block in a query as a derived table in the query's FROM clause. Parameterized views can be especially useful for queries embedded in stored procedures where the SQL variables referenced in the view are input parameters to the procedure.
It is not necessary for the SQL variable to exist when the CREATE VIEW statement is executed. However, if the SQL variable is not defined when a query that refers to the view is executed, an error is returned indicating that the column could be found.
Must have RESOURCE authority and SELECT permission on the tables in the view definition.
Automatic commit.
SQL/2008 CREATE VIEW is a core feature of the SQL/2008 standard, but some features of a view's embedded SELECT statement are optional language features. The ability to specify an ORDER BY clause with the top-level SELECT statement in the view definition is optional SQL/2008 language feature F852. The ability to restrict the result set of a view using SELECT TOP or LIMIT is optional SQL/2008 language feature F859 (though the SQL/2008 standard uses the FETCH clause for this purpose). Specifying WITH CHECK OPTION on a view that is not simply updatable - for example, the view's SELECT statement contains a derived table involving aggregation or DISTINCT, or a set operator (INTERSECT, EXCEPT or UNION) - is optional SQL/2008 language feature T111.
Some features of CREATE VIEW are vendor extensions. Parameterized views are a vendor extension, as is the optional OR REPLACE syntax.
The following example creates a view showing information for male employees only. This view has the same column names as the base table.
CREATE VIEW MaleEmployees AS SELECT * FROM Employees WHERE Sex = 'M'; |
The following example creates a view showing employees and the departments they belong to.
CREATE VIEW EmployeesAndDepartments AS SELECT Surname, GivenName, DepartmentName FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; |
The following example replaces the EmployeesAndDepartments view created in the previous example. After replacing the view, the view shows the city, state, and country location for each employee:
CREATE OR REPLACE VIEW EmployeesAndDepartments AS SELECT Surname, GivenName, City, State, Country FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; |
The following example creates a parameterized view based on the variables var1 and var2, which are neither attributes of the Employees nor Departments tables:
CREATE VIEW EmployeesByState AS SELECT Surname, GivenName, DepartmentName FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID WHERE Employees.State = var1 and Employees.Status = var2; |
Variables can appear in the view's SELECT statement in any context where a variable is a permitted expression. For example, the following parameterized view utilizes the parameter var1 as the pattern for a LIKE predicate:
CREATE VIEW ProductsByDescription AS SELECT * FROM Products WHERE Products.Description LIKE var1; |
To use this view, the variable var1 must be defined before the query referencing the view is executed. For example, the following could be placed in a procedure, function, or a batch statement:
BEGIN DECLARE var1 CHAR(20); SET var1 = '%cap%'; SELECT * FROM ProductsByDescription END |
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |