CREATE VIEW

CREATE VIEW#

Creates a view — a virtual table whose contents are defined by a query.

Syntax#

CREATE OR ALTER VIEW view name AS selectselect

Arguments#

OR ALTER

Conditionally alters the view if it already exists; otherwise creates it. When the view already exists, CREATE OR ALTER VIEW follows the same rules and requires the same permission as ALTER VIEW - including the materialized-view guard described there. Without OR ALTER, CREATE VIEW on a name that already exists is rejected.

view name

The name by which the view is identified within the virtual database.

select

A SELECT query that defines the rows and columns the view exposes.

Examples#

Create a view that exposes only the active products:

CREATE VIEW dbo.ActiveProduct AS
SELECT Id, Name, Price
FROM   dbo.Product
WHERE  Discontinued = 0;

Create the view if it does not exist yet, or redefine it in place if it does:

CREATE OR ALTER VIEW dbo.ActiveProduct AS
SELECT Id, Name, Price, Sku
FROM   dbo.Product
WHERE  Discontinued = 0;