Lesson 4 of 4 Specialized Tables

External Tables and Data Virtualization

This lesson explains how T-SQL queries Parquet, CSV and Delta files in Azure Storage in place with external data sources, external tables and OPENROWSET.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the credential, data source, file format and external table objects.
  • Distinguish between OPENROWSET and external tables.
  • Identify the platform support and limitations of data virtualization.

Objects involved

Querying external files uses up to four objects. A database scoped credential holds how to authenticate, such as IDENTITY = 'Managed Identity' or a shared access signature. An external data source names the location, for example abs://[email protected]/sales/ or an adls:// path, with the credential. An external file format describes Parquet, delimited text or Delta. An external table declares typed columns over a file path, which can include wildcards.

CREATE DATABASE SCOPED CREDENTIAL LakeCredential WITH IDENTITY = 'Managed Identity';

CREATE EXTERNAL DATA SOURCE SalesLake
WITH (LOCATION = 'abs://[email protected]/', CREDENTIAL = LakeCredential);

CREATE EXTERNAL FILE FORMAT ParquetFormat WITH (FORMAT_TYPE = PARQUET);

CREATE EXTERNAL TABLE ext.WebClicks
(
    ClickTime  datetime2,
    ProductID  int,
    SessionID  varchar(40)
)
WITH (LOCATION = 'clicks/year=*/month=*/*.parquet',
      DATA_SOURCE = SalesLake, FILE_FORMAT = ParquetFormat);

The following diagram shows the four objects in the order they depend on each other.

The objects behind an external table. A database scoped credential holds how to authenticate, such as a managed identity. An external data source names the storage location and uses the credential. An external file format describes Parquet, delimited text or Delta. The external table declares typed columns over a file path, which can include wildcards, so T-SQL can query the files without copying them.
The files stay where they are.
Put in order

Building an external table

Exam skills 1.1.2

Put the objects for querying Parquet files in the order they must be created.

Log in to try this activity Interactive activities are free with an account.

OPENROWSET for ad hoc queries

OPENROWSET(BULK 'path', DATA_SOURCE = 'name', FORMAT = 'parquet') queries files without creating an external table, which suits exploration. Parquet schemas are inferred, while CSV files need a WITH clause listing columns; an explicit WITH clause with precise types also improves performance for Parquet. The filepath() and filename() functions return parts of the path, which allows filtering on folder partitions such as year and month so only matching files are read.

Platform support and limits

Data virtualization is available in SQL Server 2022 and later through PolyBase, in Azure SQL Managed Instance, and in Azure SQL Database, where it is in preview and read-only. In Azure SQL Database, external tables do not support statistics, CREATE EXTERNAL TABLE AS SELECT, Row-Level Security or Dynamic Data Masking, so sensitive external data needs other controls. The managed identity of the logical server needs a role such as Storage Blob Data Reader on the storage account. In Microsoft Fabric, lakehouse tables are queried through the SQL analytics endpoint instead.

True or false

Data virtualization limits

Exam skills 1.1.2

Decide whether each statement about data virtualization in Azure SQL Database is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

Joining lake data with orders

Clickstream data lands every hour as Parquet files in Azure Data Lake Storage, partitioned in folders by year and month. Analysts want to join recent clicks with orders in an Azure SQL database without copying the files, and storage keys must not be stored in the database. What should the developer create?

Grant the logical server's managed identity Storage Blob Data Reader on the storage account, create a database scoped credential with IDENTITY = 'Managed Identity', an external data source for the container, a Parquet file format and an external table whose location uses wildcards for the year and month folders. Analysts then join the external table with the orders table, and filters on the partition folders limit the files read. A SAS token would work but is a stored secret, and loading the files with an ETL job contradicts the requirement not to copy them.

Check yourself

Which file format requires a WITH clause listing columns in OPENROWSET?

Show answer

CSV, because its schema cannot be inferred; Parquet schemas can be inferred, although an explicit WITH clause improves performance.

Exam Tips

Remember for the exam

  • Query files in place: external table or OPENROWSET over an external data source.
  • No stored keys: a credential with IDENTITY = 'Managed Identity' and a storage data role.
  • Ad hoc exploration: OPENROWSET; repeated queries: an external table.
  • Azure SQL Database data virtualization is read-only and in preview.