CREATE EXTERNAL TABLE AS SELECT

CREATE EXTERNAL TABLE AS SELECT#

Exports the result of a query to a file (CETAS). Querona runs the SELECT and streams its rows straight to a file in a file data source connection; the rows are not copied into a table or cache first.

For a step-by-step walkthrough, see Export query results to files.

Important

File export is disabled by default. Until an administrator enables it (see File export settings), the statement fails with an “Export to files … is disabled on this instance” error.

Syntax#

CREATE EXTERNAL TABLE table ( column definition , ) accepted - ignored WITH ( LOCATION = 'file path' DATA_SOURCE = data source FILE_FORMAT = file format , ) AS select statementselect statement

LOCATION, DATA_SOURCE and FILE_FORMAT are all required and may appear in any order, each once. A missing option is reported as error 46505 and a repeated one as error 46547.

Arguments#

table

The name of the export, [ database_name. [ schema_name ] . | schema_name. ] table_name. The name determines where the statement is authorized - the export requires the right to create a table in that database and schema - but no table is created: the statement leaves only the file behind, and sys.external_tables does not list it.

column definition

Accepted for SQL Server compatibility and ignored. The file takes its column names and types from the SELECT.

LOCATION = 'file path'

The path of the file to write, relative to the location of the data source. Subfolders in the path are created as needed, for example 'year=2026/q2/top-customers.csv'. The path always names a single file: a path that ends with / or \ is refused. A path that would leave the data source - a .. segment, an absolute drive path, a UNC path or a URI - is refused before any file is opened.

DATA_SOURCE = data source

The name of a file connection to write through (see Create a connection). The connection is the boundary of every export: it fixes the base folder, and exports can write only inside it. A name that does not match a connection is reported as error 46501; a connection that is not file-backed, such as a SQL Server connection, is refused.

FILE_FORMAT = file format

A format created in the target table’s database with CREATE EXTERNAL FILE FORMAT, which is looked up first and writes what SQL Server writes for its declaration, or one of the built-in formats. The built-ins need no declaration and are not listed in sys.external_file_formats:

Name

Output

csv

Comma-separated text with a header row.

tsv

Tab-separated text with a header row.

parquet

A Parquet file compressed with Snappy. A few types need a note; see Type fidelity in Export query results to files.

A name that is neither is reported as error 46501. A user-defined format of type ORC or DELTA can be created but not written to, and is refused with error 11560.

AS select statement

The query whose result is exported (see SELECT statement). The file contains exactly the columns the query returns, in the order given by its ORDER BY, if any.

Note

CREATE EXTERNAL TABLE without AS SELECT - an external table over existing files - is not supported and is refused when it runs; so is DROP EXTERNAL TABLE. To read files, use OPENROWSET or a file connection. See Feature support.

Remarks#

The file is written to a temporary file next to the target and moved into place only when the export succeeds. Re-running an export to the same LOCATION therefore replaces the file, and an export that fails leaves an existing file unchanged.

The file is written by the Querona service account, which needs operating-system write permission to the location of the data source.

Permissions#

Requires all of:

  • the permission to create a table in the target database and schema - the same permission CREATE TABLE requires, so run the statement in a user database, not master;

  • ALTER permission on the data source connection, which CONTROL on the connection and CONTROL SERVER imply; and

  • ALTER ANY EXTERNAL FILE FORMAT in the target table’s database, whichever format the statement names - a built-in one included - as SQL Server requires; ALTER or CONTROL on the database implies it. Without it the statement is refused with error 15247 after the check that file export is enabled and before any file is opened.

See Access rights.

Examples#

Export a query to a CSV file in the SalesExport file connection:

CREATE EXTERNAL TABLE dbo.TopCustomers
WITH (
    LOCATION    = 'top-customers.csv',
    DATA_SOURCE = SalesExport,
    FILE_FORMAT = csv
)
AS
SELECT customer_id, name, total_spent
FROM   sales.customers
WHERE  total_spent > 10000
ORDER BY total_spent DESC;

Export to a Parquet file in a dated subfolder:

CREATE EXTERNAL TABLE dbo.SalesByRegion
WITH (LOCATION = 'year=2026/sales-by-region.parquet', DATA_SOURCE = SalesExport, FILE_FORMAT = parquet)
AS
SELECT r.region_name, COUNT(*) AS orders, SUM(o.amount) AS revenue
FROM   sales.orders  AS o
JOIN   sales.regions AS r ON r.region_id = o.region_id
GROUP BY r.region_name;