SELECT DRIVE
Overview
The SELECT DRIVE operation is designed to read from one or more files from a cloud drive, local path, FTP site, or network drive. Reading one file at a time is usually the preferred implementation; however, you can use this operation to read all available files and combine them into a single data set.
The file name specified can include a * wildcard regardless of the drive selected. Depending on the drive selected,
file names may be case-sensitive. For example, you can specify \2025\*.txt against any drive to search
for files that found in the 2025 subfolder; when searching in AWS Directory Buckets or ADLS Hierarchy stores,
use forward slashes instead: /2025/*.txt.
The APPLY TX operation is only supported for XML, JSON, and CSV formats. See the APPLY TX documentation for details. When APPLY TX is used with the CSV format, it overrides any CSV-specific options setting in the SELECT statement.
The inner APPLY PIPELINE operation declared within the SELECT operation allows you to add one or more transformations before capturing the final data set. When using this approach, consider that the pipeline will execute for every paged request. For example, if you use the PAGING option, and 5 pages of data are retrieved, this pipeline will execute five times. If you would like to execute a pipeline transformation after all pages have been retrieved, but before the capture operation, use the APPLY PIPELINE operation after the SELECT operation.
Both the file name and container parameter provided can use variable names; this allows passing file names and folders/containers as parameters from other scripts when using the START operation.
-- this pipeline can be called like this: START 'this pipeline name' PARAMS 'fileName=*.txt&container=qa'; DECLARE @fileName = '*.*'; DECLARE @container = 'test'; SELECT * FROM DRIVE [awss3] (@filename) WITH CONTAINER '@container' FORMAT RAW LIST;
Using both MOVE_TO and INCLUDE_SUBFOLDERS can create an unpredictable outcome if
the archive folder is itself a subdirectory.
TOP, WHERE, ORDER BY, and LIMIT
All SELECT operations share the same last-mile filtering and limiting options. Understanding how
these interact with TOP is important when combining source-side limits with client-side filters.
TOP N
TOP N is aware of the source system and works with an APPLY TX defined on the
SELECT command so reading can stop once the transformation has produced enough rows. It also works
hand-in-hand with an inner APPLY PIPELINE when one is specified, including cases where that pipeline
changes the record count (for example by filtering or expanding rows).
WHERE, ORDER BY, and LIMIT
WHERE, ORDER BY, and LIMIT run after TOP N has been
satisfied (when TOP is specified). Treat them as a last-mile operation on the final in-memory
data set. The WHERE clause works like an APPLY FILTER operation using a SQL-like
filter expression.
If the SELECT retrieved multiple streams of data (for example when HTTP paging is engaged), those
streams are first merged into a single data set before the filter, order, and limit are applied.
In short: use TOP to stop reading early at the source (in coordination with
APPLY TX / inner APPLY PIPELINE). Use WHERE / ORDER BY /
LIMIT to shape the combined result after retrieval is complete.
Syntax
Reads data from one or more files located on a local folder or on cloud or FTP drives.
{ APPEND }
SELECT
{TOP N}
< ... | * >
FROM DRIVE [CONNECTION]
// File name or pattern
( ... )
WITH
FORMAT < 'PARQUET' | 'CSV' | 'XML' | 'JSON' | 'RAW' | 'BYTES' >
{ CONTAINER '...' }
{ UNZIP }
{ WEBHOOK }
-- CSV Options
{ HAS_HEADERS }
{ TRIM_FIELDS }
{ SKIP N }
{ QUOTED_IDENTIFIER '...' }
{ DELIMITERS '...' }
{ FIXED_LENGTHS '...' }
{ COMMENT_TOKENS '...' }
-- RAW or BYTES Option
{ LIST }
{ SINGLE_FILE }
{ NEW_FILES_ONLY }
{ MOVE_TO '...' }
{ INCLUDE_SUBFOLDERS }
{ FILENAME_COL '...' }
{ TRACE INTO [CONNECTION]
TABLE '...'
{ STAGE ON [CONNECTION] '...' CREDENTIALS '...' }
{ ADD_TRACKING_FIELDS }
{ TRUNCATE | TRUNCATE_APPEND }
{ SKIP_PREVIEW }
}
{ APPLY TX '...'
{ WITH RAWCOL '...' }
}
-- Last-mile client-side filter
{ WHERE (...) }
{ ORDER BY (...) }
{ LIMIT N }
;APPEND |
Appends the output of this operation, after APPLY TX if any, to the current data pipeline and adjusts the schema accordingly to accommodate new fields if needed |
TOP |
Client-side operator that limits the number of records after the inner pipeline has executed but before the WHERE clause executes |
FORMAT |
One of the file formats supported: PARQUET, CSV, XML, JSON, RAW, BYTES |
CONTAINER |
Overrides the path to the file when using a local folder or an FTP site, the AWS S3 bucket name, the Azure Container name, or the name of the Google Drive folder |
UNZIP |
Unzip the file first and processes each file with the FORMAT specified |
HAS_HEADERS |
Assumes the file has a header line (CSV only) |
TRIM_FIELDS |
Applies a trim operation to each field (CSV only) |
SKIP |
The number of records to skip before reading the content (CSV only) |
QUOTED_IDENTIFIER |
The character to use identifying data in quotes (ex: '"' ) (CSV only) |
DELIMITERS |
The list of characters to use as field delimiters (ex: ',' or ';|') - leave blank for automatic detection of the delimiter used by the file (CSV only) |
FIXED_LENGTHS |
Assumes the file does not use delimiters between fields and instead has fixed positions as provided and separated by a comma (ex: 0,15,55,200) (CSV only) |
COMMENT_TOKENS |
Comma-separated list of leading characters used to exclude lines (CSV only) |
LIST |
Returns the list of files without their content (RAW and BYTES only) |
SINGLE_FILE |
Indicates that each file should be processed individually as a separate execution (a new executionid will be created) |
NEW_FILES_ONLY |
Keep a high watermark of the last modified date of a file and only return newer files |
MOVE_TO |
The folder or subfolder to move the file into after successfully extracting its content |
INCLUDE_SUBFOLDERS |
Inspects subfolders to search for files (warning: use carefully when MOVE_TO points to a subfolder) |
FILENAME_COL |
Adds a column with the name provided containing the file name each record was extracted from |
TRACE INTO |
Saves the content of each file into a database for further inspection and troubleshooting purposes (at runtime only) |
TABLE |
The table name in which each file content will be stored |
TRUNCATE |
When specified, indicates the trace table should first be truncated if it exists |
TRUNCATE_APPEND |
The target table is first truncated if it exists then appended to when multiple pages of data are being processed (ignore if ON COLUMNS is specified) |
STAGE ON |
Specifies the connection and folder/container to use in order to stage the data first depending on the target database |
CREDENTIALS |
When a stage environment is specified, this option is used to provide the necessary credentials |
ADD_TRACKING_FIELDS |
Adds internal tracking fields including executionId, pipeline name, execution date and more |
SKIP_PREVIEW |
When tracing is enabled, indicates that nothing should be logged during preview operations |
APPLY PIPELINE |
Executes an inline pipeline consisting of one or more components for each file retrieved |
WEBHOOK |
Enables the pipeline to operate as a webhook and accept incoming HTTP push operations |
WHERE |
Applies a client-side filter after all data has been processed (after APPLY PIPELINE if any) |
LIMIT |
Limits the returned data set to the number of rows requested after all other operations are completed |
ORDER BY |
Orders the data set using the list of fields provided (applied before the LIMIT operation); supports the ASC (default) or DESC specfier after the list of columns |
Example 1
-- Retrieves all parquet files in a cloud drive -- and add a column with the name of the file SELECT * FROM DRIVE [adls2.0] (*.parquet) WITH FORMAT 'Parquet' CONTAINER 'logs' FILENAME_COL '_filename' ;
Example 2
-- Retrieves available files from an SFTP site in a -- given directory, expecting CSV files, and moving -- them to an archive folder upon completion -- FTP files are case-sensitive - return all files under the 'data' -- directory containing Weather in their names SELECT * FROM DRIVE [mysftp] (data/*Weather*) WITH FORMAT 'CSV' SINGLE_FILE -- process one file at a time per execution MOVE_TO '/archive' ;
