Streaming ingestion gets the headlines, but most Snowflake accounts still load the majority of their data from files: nightly extracts, vendor drops, and backfills. COPY INTO <table> is the workhorse for that, and a few settings decide whether it is boring and reliable or a source of silent data loss. This tutorial covers a loading pattern we use on client engagements.
1. Stage and file format
Create named objects instead of inline options so loads are repeatable and reviewable.
CREATE OR REPLACE FILE FORMAT ff_csv
TYPE = CSV
PARSE_HEADER = TRUE
FIELD_OPTIONALLY_ENCLOSED_BY = '"'
NULL_IF = ('', 'NULL')
ERROR_ON_COLUMN_COUNT_MISMATCH = FALSE;
CREATE OR REPLACE STAGE raw.orders_stage
URL = 's3://my-bucket/orders/'
STORAGE_INTEGRATION = s3_int
FILE_FORMAT = ff_csv;
Use a storage integration rather than embedding keys. Check what is visible with LIST @raw.orders_stage;.
2. Let INFER_SCHEMA draft the table
With PARSE_HEADER = TRUE (or Parquet/ORC/Avro), Snowflake can derive column names and types from the files.
CREATE OR REPLACE TABLE raw.orders
USING TEMPLATE (
SELECT ARRAY_AGG(OBJECT_CONSTRUCT(*))
FROM TABLE(INFER_SCHEMA(
LOCATION => '@raw.orders_stage',
FILE_FORMAT => 'ff_csv'))
);
Treat the result as a first draft. Inference samples files, so a column that is integer in the sample may contain text later. Review types, and widen where in doubt. For production, commit the final DDL to source control rather than re-inferring on each run.
3. Load with MATCH_BY_COLUMN_NAME
Positional loading breaks the moment a vendor reorders columns. Match by name instead:
COPY INTO raw.orders
FROM @raw.orders_stage
MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE
ON_ERROR = ABORT_STATEMENT;
4. Schema evolution
If upstream adds columns, let the table follow:
ALTER TABLE raw.orders SET ENABLE_SCHEMA_EVOLUTION = TRUE;
The loading role needs the EVOLVE SCHEMA privilege (or ownership) on the table. Snowflake can add new columns and drop NOT NULL constraints during a load that uses MATCH_BY_COLUMN_NAME. It will not change existing column types, so type drift still fails loudly, which is what you want.
5. Choose ON_ERROR deliberately
| Setting | Behaviour | Use when |
|---|---|---|
ABORT_STATEMENT (default for COPY) | Stops on first error, nothing from the statement loads | Curated, contract-driven feeds |
SKIP_FILE | Skips any file with an error | Files are atomic units |
SKIP_FILE_<n>% | Skips file past an error threshold | Tolerating minor noise |
CONTINUE | Loads good rows, drops bad ones | Rarely; only with an audit step |
CONTINUE is the dangerous one: bad rows disappear unless you go looking. If you use it, capture the rejects (step 6).
6. Validate before and after
Dry-run a load without writing data:
COPY INTO raw.orders FROM @raw.orders_stage
MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE
VALIDATION_MODE = RETURN_ERRORS;
After a load that used ON_ERROR = CONTINUE, retrieve rejected rows from the last COPY:
SELECT * FROM TABLE(VALIDATE(raw.orders, JOB_ID => '_last'));
Persist these to a rejects table so they can be reviewed and replayed.
7. Know what was loaded
COPY tracks file load metadata for 64 days, which is how it avoids reloading the same file. Inspect it:
SELECT file_name, status, row_count, row_parsed, first_error_message
FROM TABLE(INFORMATION_SCHEMA.COPY_HISTORY(
TABLE_NAME => 'ORDERS',
START_TIME => DATEADD(day, -1, CURRENT_TIMESTAMP())));
Two consequences: a file that was already loaded is skipped even if you want it again (FORCE = TRUE overrides, and will create duplicates), and a file older than 64 days with no history may be reloaded unexpectedly. Idempotency across that window is your design problem; add a load-batch column such as METADATA$FILENAME and METADATA$FILE_LAST_MODIFIED:
COPY INTO raw.orders_audit (order_id, amount, src_file, src_modified)
FROM (SELECT $1, $2, METADATA$FILENAME, METADATA$FILE_LAST_MODIFIED FROM @raw.orders_stage);
Note that MATCH_BY_COLUMN_NAME cannot be combined with a transformation query, so choose one per load.
8. Performance and cost notes
- Aim for compressed files around 100-250 MB; thousands of tiny files waste per-file overhead.
- Load into an appropriately small warehouse; a bigger one only helps when there are many files to parallelize.
- Use
PATTERNor path prefixes to limit directory listing on large stages. - When the files arrive continuously, move to Snowpipe (auto-ingest) with the same file format and COPY definition.
A production checklist
- Named file format, stage, and storage integration under version control.
- Table DDL reviewed, not blindly inferred.
MATCH_BY_COLUMN_NAMEplus schema evolution only where upstream change is expected.- Explicit
ON_ERROR, with rejects captured when not aborting. - Row-count reconciliation against the source manifest after each load.
- Alerts on
COPY_HISTORYrows with non-LOADED status.
If you are planning a migration or need help hardening ingestion in your Snowflake account, get in touch with our team.