Creates or replaces a view, which is an alternative way to look at the data in one or more tables.
create [or replace] view [owner.]view_name [(column_name[, column_name]...)] as select [distinct] select_statement [with check option]
Only an existing view can be replaced. The object name and ID remain the same.
A column is derived from an arithmetic expression, function, string concatenation, or constant
Two or more columns have the same name (usually because of a join)
You want to give a column in a view a different name than the column from which it is derived (see Example 3)
Column names can also be assigned in the select statement (see Example 4). If no column names are specified, the view columns acquire the same names as the columns in the select statement.
If the original definition of the view did not specify distinct clause, you can change this parameter so the new view cannot contain duplicate rows.
The columns specified in the target list of the select_statement of the replaced view can be changed to drop or add columns.
Each row that is inserted or updated through the view must meet the selection criteria of the view.
All views derived from the “base” view must satisfy its check option. Each row inserted or updated through the derived view must remain visible through the base view.
create view titles_view as select title, type, price, pubdate from titles
create view "new view" ("column 1", "column 2") as select col1, col2 from "old view"
create view accounts (title, advance, amt_due) as select title, advance, price * total_sales from titles where price > $5
create view cities (authorname, acity, publishername, pcity) as select au_lname, authors.city, pub_name, publishers.city from authors, publishers where authors.city = publishers.city
create view cities2 as select authorname = au_lname, acity = authors.city, publishername = pub_name, pcity = publishers.city from authors, publishers where authors.city = publishers.city
create view author_codes as select distinct au_id from titleauthor
create view price_list (price) as select distinct price from titles
create view stores_cal as select * from stores where state = "CA" with check option
create view stores_cal30 as select * from stores_cal where payterms = "Net 30"
create view stores_cal30_check as select * from stores_cal where payterms = "Net 30" with check option
create view psych_titles as select * from (select * from titles where type = "psychology") dt_psych
create view Current_Product_List as select ProductID, ProductName from Products where Discontinued = “No” select object_id("Current_Product_List") ----------- 889051172
This next command adds the Category column to Current_Product_list using the or replace clause. The object ID of the view remains the same:
create or replace view Current_Product_List as select ProductID, ProductName, Category from Products where Discontinued = “No” select object_id("Current_Product_List") ----------- 889051172
create table T1(C1 int, C2 int) create table T2(C1 int, C2 int) create view V1 as select * from T1 create view V2 as select * from V1 create function foo1 returns int as begin declare @number int select @number = C1 from V2 end return @number select object_id("V1") ----------- 985051514
create or replace V1 as select * from T2 select * from V2 select dbo.foo1() select object_id("V1") ----------- 985051514
The replaced version of V1 references T2 instead of T1. Both V2 and foo1 will be recompiled. select * from v2 recompiles V2, but not foo1, which is recompiled when the UDF is invoked.
You can use views as security mechanisms by granting permission on a view, but not on its underlying tables.
You can use sp_rename to rename a view.
When you query through a view, the SAP ASE server checks to make sure that all the database objects referenced anywhere in the statement exist, that they are valid in the context of the statement, and that data update commands do not violate data integrity rules. If any of these checks fail, you see an error message. If the checks are successful, create view “translates” the view into an action on the underlying tables.
For more information about views, see the Transact-SQL Users Guide.
To create a report of the tables or views on which a view depends, and of objects that depend on a view, execute sp_depends.
To display the text of a view, which is stored in syscomments, execute sp_helptext with the view name as the parameter.
Identifiers in Reference Manual: Building Blocks
sp_depends, sp_help, sp_helptext, sp_rename in Reference Manual: Procedures
ANSI SQL – Compliance level: Entry-level compliant.
The use of more than one distinct keyword and the use of “column_heading = column_name” in the select list are Transact-SQL extensions.
When you create a view, the SAP ASE server makes no permission checks on objects, such as tables and views, that are referenced by the view. Therefore, you can create successfully a view even if you do not have access to its objects. All permission checks occur when a user invokes the view.
If the view and its objects are not owned by the same user, the invoker must have been granted direct access to the objects. For example, if the view performs a select from a table the invoker cannot access, the select statement fails.
If the view and its objects are owned by the same user, special rules apply. The invoker automatically has implicit permission to access the view’s objects even though the invoker could not access them directly. Without having to grant users direct access to your tables, you can give them restricted access with a view. In this way, a view can be a security mechanism. For example, invokers of the view might be able to access only certain rows and columns of your table. A detailed description of the rules for implicit permissions is discussed in the System Administration Guide.
If a column in the table is encrypted, you must have decrypt permission to select from the view. If the view and its objects are not owned by the same user, you must have decrypt permission on the encrypted column in the table to select from the view. If the view and its objects are owned by the same user, it is sufficient to grant decrypt permission to the user who must select from the view on the view column that corresponds to the encrypted column in the table.
Any user who impersonates the view owner through an alias or setuser cannot replace the view.
Setting | Description |
---|---|
Enabled | With granular permission enabled, you must have the create view privilege to create a view. To create a view for another user, you must have the create any view privilege. You must be the view owner to replace the view. |
Disabled | With granular permissions disabled, you must be the database owner, a user with sa_role, or have the create view privilege to create a view. To create a view for another user, you must have sa_role. You must be the view owner to replace the view. |
Values in event and extrainfo columns of sysaudits are:
Information | Values |
---|---|
Event | 16 |
Audit option | create |
Command or access audited | create view |
Information in extrainfo |
|