CREATE VIEW
Creates a view: a select query stored in the database under a name.
Access SQL. ACE accepts this statement as written here.
Syntax
Section titled “Syntax”CREATE VIEW view_name [ ( column_name [ , … ] ) ] AS select_statementArguments
Section titled “Arguments”view_name
Section titled “view_name”The name of the view. No table, view or procedure in the database may already have it.
column_name
Section titled “column_name”Names for the view’s columns, in order. Each replaces the name the SELECT gives that column, an AS alias
included, and a * counts as the columns it stands for. The list must name every column: one of the wrong length
fails with “The number of aliases specified shall be the same as the number of output columns.”
select_statement
Section titled “select_statement”The query the view stores. It is a single SELECT, and may use joins, WHERE, DISTINCT, GROUP BY with
aggregates and HAVING, TOP n with a literal n, column aliases, Table.*, and a derived table in FROM
(a UNION inside a derived table included).
A view’s query cannot have:
ORDER BY, with or withoutTOP.INTO. A make-table query is stored withCREATE PROCEDURE.UNIONat its top level (“Unions not allowed in a subquery.”). Store a union query withCREATE PROCEDURE.TRANSFORM … PIVOT. Store a crosstab withCREATE PROCEDURE.TOP … WITH TIES, or aTOPwhose count is not a literal integer.
Remarks
Section titled “Remarks”LibRed stores a view the way Access does, as an entry in MSysObjects and the query broken down into
MSysQueries rows. Access opens the file and runs a view LibRed wrote, and LibRed runs views Access wrote.
Use a view by name in a FROM clause, as you would a table. It can be joined, filtered, grouped, and used in a
subquery. You cannot insert into, update or delete from a view: name the table instead.
The view’s columns are named as its SELECT names them, unless a column_name list names them. Give a column a
name with AS in the select list, or with the list.
The query’s sources must exist when the view is created. A table or view it reads that the database doesn’t
have, in its FROM, a join or a subquery, fails the statement with “The Microsoft Access database engine cannot
find the input table or query ‘…’”, and nothing is stored. So a view cannot read itself.
To change a view, delete it with DROP VIEW and create it again.
Examples
Section titled “Examples”These run against the Northwind sample database, as in the playground.
Create a view of the customers in the UK, then read it:
CREATE VIEW UKCustomers AS SELECT CustomerID, CompanyName, City FROM Customers WHERE Country = 'UK'SELECT * FROM UKCustomersJoin the view to a table:
SELECT c.CompanyName, COUNT(*) AS OrdersFROM UKCustomers AS c INNER JOIN Orders AS o ON o.CustomerID = c.CustomerIDGROUP BY c.CompanyNameStore a grouped query, naming the computed column with AS, and sort it when you read it:
CREATE VIEW OrderTotals AS SELECT OrderID, SUM(UnitPrice * Quantity) AS Total FROM [Order Details] GROUP BY OrderIDSELECT TOP 3 * FROM OrderTotals ORDER BY Total DESCName the columns with a list instead:
CREATE VIEW ShipperNames (Id, Name) AS SELECT ShipperID, CompanyName FROM ShippersSELECT Id, Name FROM ShipperNamesStore a join with a filter:
CREATE VIEW ProductList AS SELECT p.ProductName, c.CategoryName FROM Products AS p INNER JOIN Categories AS c ON c.CategoryID = p.CategoryID WHERE p.Discontinued = FalseSELECT * FROM ProductList WHERE CategoryName = 'Seafood'