Skip to content

CREATE VIEW

Creates a view: a select query stored in the database under a name.

Access SQL. ACE accepts this statement as written here.

CREATE VIEW view_name [ ( column_name [ , … ] ) ]
AS select_statement

The name of the view. No table, view or procedure in the database may already have it.

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.”

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 without TOP.
  • INTO. A make-table query is stored with CREATE PROCEDURE.
  • UNION at its top level (“Unions not allowed in a subquery.”). Store a union query with CREATE PROCEDURE.
  • TRANSFORM … PIVOT. Store a crosstab with CREATE PROCEDURE.
  • TOP … WITH TIES, or a TOP whose count is not a literal integer.

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.

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 UKCustomers

Join the view to a table:

SELECT c.CompanyName, COUNT(*) AS Orders
FROM UKCustomers AS c INNER JOIN Orders AS o ON o.CustomerID = c.CustomerID
GROUP BY c.CompanyName

Store 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 OrderID
SELECT TOP 3 * FROM OrderTotals ORDER BY Total DESC

Name the columns with a list instead:

CREATE VIEW ShipperNames (Id, Name) AS
SELECT ShipperID, CompanyName FROM Shippers
SELECT Id, Name FROM ShipperNames

Store 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 = False
SELECT * FROM ProductList WHERE CategoryName = 'Seafood'