Skip to main content

Create View

Create a view: a Magpie table that stores a query instead of data, and runs it each time the view is read. Run it in a SQL paragraph, or in a Magpie paragraph as sql(CREATE VIEW …), where it runs immediately and leaves result unchanged.

A view is the same kind of table as one created with Create Table From SQL. To save a query's result as data instead, use Create Table As Select. See Creating Tables and Views with SQL for when to use this statement or Create Table From SQL.

Syntax​

CREATE [OR REPLACE] [TEMPORARY | TEMP] VIEW <view> AS <query>

Parameters​

view​

String — The view to create, as view or schema.view, in the current repository. Defaults to the current schema.

query​

Query — The query that defines the view. It can read only tables in the current repository. $variables in the query are resolved when a session first reads the view, and keep that value for the rest of the session.

Options​

or replace​

If the view already exists, replace its query. The existing table must be a view. Without this option, creating a view that exists is an error.

temporary​

Create the view for the current session only. A permanent view can't read a temporary view.

Examples​

CREATE VIEW sales.open_orders AS
SELECT order_id, customer_id, amount
FROM sales.orders
WHERE status = 'open'
CREATE OR REPLACE TEMPORARY VIEW big_orders AS
SELECT * FROM sales.orders WHERE amount > 1000

Errors​

MessageCause
CREATE OR REPLACE VIEW cannot replace "<table>": it is not a view (only tables with SQL persistence are views). Use CREATE OR REPLACE TABLE <table> AS SELECT ..., or drop the table first with 'drop table <table>'.The existing table holds data.
CREATE VIEW does not support a repository-qualified target …The view names a repository. Run use repository and name the view without it.
Error processing SQL statement: … Table or stream with name "<view>" already exists in schema.A table or view with that name already exists, and OR REPLACE wasn't given.
Permanent tables cannot reference temporary tables …A permanent view's query reads a temporary view or table.

Spark clauses such as GLOBAL TEMPORARY and a column list after the view name aren't supported and fail with a parse error.

Security​

Requires Create permission on the schema, and Read permission on every table the query reads. OR REPLACE also requires Write permission on the existing view.