Sub topic 3
Your first COPY INTO statement
This chapter assumes you know basic SQL (CREATE TABLE, SELECT, INSERT) and have used Delta tables in Databricks. You also need a SQL warehouse or cluster and a Unity Catalog volume or external location that holds a few files. Granting access to that location was covered in the previous chapter. Here the focus is the statement itself: its parts, what happens when it runs, and the errors you are most likely to hit.
What the statement does
COPY INTO reads files from a source location and appends their rows to a Delta table. It records which files it has loaded, so a second run skips them. The next chapters cover file formats, file selection, and idempotency in detail. This chapter covers only the basic load.
The command has three required parts and a few optional ones.
| Part | Required | Purpose |
|---|---|---|
| `COPY INTO table_name` | Yes | The target Delta table, named with its three-level Unity Catalog name |
| `FROM 'location'` | Yes | The folder (or a file path) that holds the source files |
| `FILEFORMAT = format` | Yes | The format of the source files, such as `CSV`, `JSON`, or `PARQUET` |
| `FORMAT_OPTIONS (...)` | No | Parser settings for the format, such as `header` or `delimiter` |
| `COPY_OPTIONS (...)` | No | Settings for the load itself, such as `mergeSchema` or `force` |
| `PATTERN` or `FILES` | No | Limits which files in the location are read |
The clauses must appear in this order: FROM, FILEFORMAT, then PATTERN or FILES, then FORMAT_OPTIONS, then COPY_OPTIONS. The VALIDATE clause, covered in a later chapter, goes after FILEFORMAT and the file selection clauses.
A minimal load
Start with a target table that already has a schema.
CREATE TABLE IF NOT EXISTS main.sales.orders (
order_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');The path /Volumes/main/sales/landing/orders/ follows the pattern /Volumes/<catalog>/<schema>/<volume>/<path>. The header option tells the CSV parser that the first row of each file holds column names.
When the statement finishes, it returns a single row with these columns:
| Column | Meaning |
|---|---|
| `num_affected_rows` | Rows read from the source files |
| `num_inserted_rows` | Rows written to the table |
| `num_skipped_corrupt_files` | Files skipped because they were corrupt (non-zero only if `ignoreCorruptFiles` is set) |
On the first run, num_affected_rows and num_inserted_rows match. Run the same statement again with no new files and both values are 0, because every file is already recorded as loaded.
How the statement runs
Here is the sequence when you execute a COPY INTO.
Three points follow from this flow.
- The file list is built on every run. For a folder with a very large number of files, listing takes time even when nothing is loaded.
- The record of loaded files is kept with the target table, not with the source folder. Loading the same folder into a second table loads every file again for that table.
- The load is an append.
COPY INTOnever updates or deletes existing rows. If a source file contains a row that is already in the table with different values, you get two rows.
Letting the table take its schema from the files
You do not have to write the schema first. If you create an empty table with no columns, COPY INTO can infer the columns from the files and add them using mergeSchema.
CREATE TABLE IF NOT EXISTS main.sales.orders_raw;
COPY INTO main.sales.orders_raw
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true', 'inferSchema' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');Two options do the work here:
'inferSchema' = 'true'inFORMAT_OPTIONSmakes the CSV reader sample the data and guess column types such asINTorTIMESTAMP. Without it, every CSV column is read asSTRING.'mergeSchema' = 'true'inCOPY_OPTIONSlets the load add the inferred columns to the table.
This approach suits exploration and raw landing tables. For tables that downstream jobs depend on, define the schema yourself. Inference can pick a different type when a new batch of files looks different, for example a column that was all integers and now has a decimal value.
Source paths
The FROM clause accepts several kinds of location.
| Location | Example |
|---|---|
| Unity Catalog volume | `/Volumes/main/sales/landing/orders/` |
| External location path in cloud storage | `s3://my-bucket/landing/orders/` |
| Single file | `/Volumes/main/sales/landing/orders/2026-12-01.csv` |
For a cloud storage path, you need a Unity Catalog external location that covers it, and you need READ FILES on that location. For a volume, you need READ VOLUME. You also need MODIFY on the target table and USE CATALOG and USE SCHEMA on its parents. A missing grant is the most common reason a first statement fails.
COPY INTO reads files in the folder and its subfolders. If your layout has date folders such as orders/2026/12/04/, a single statement pointed at orders/ finds them all.
Common errors on a first run
| Symptom | Likely cause | Fix |
|---|---|---|
| Permission or access denied on the path | Missing `READ VOLUME` or `READ FILES`, or no external location covers the path | Ask for the grant, or check that the path is inside an external location |
| Table or view not found | The target table does not exist, or the name is missing its catalog or schema | Create the table first, or use the full three-level name |
| Column names appear as `_c0`, `_c1`, and so on | `header` is not set, so the first row is read as data | Add `'header' = 'true'` |
| All columns are `STRING` in a table built from CSV | `inferSchema` is not set | Add `'inferSchema' = 'true'`, or define column types in the table |
| Result shows `0` rows on the first run | The folder is empty, the path is wrong, or the files are not in the declared format | List the folder and confirm the path and `FILEFORMAT` |
| Result shows `0` rows on a later run | Every file was already loaded | This is expected behavior |
| Error that the schema does not match | Source columns differ from the table columns | Align the columns, use a `SELECT` source, or enable `mergeSchema` |
Checking the first load
After the statement runs, confirm three things.
SELECT COUNT(*) FROM main.sales.orders;
SELECT * FROM main.sales.orders LIMIT 10;
DESCRIBE HISTORY main.sales.orders;The count should equal num_inserted_rows from the result row on a first load. The sample shows whether types and column order look right. DESCRIBE HISTORY shows the Delta commit made by the load, including its operation name, so you can tell it apart from other writes to the table.
Exercises
Exercise
You run a COPY INTO statement for the first time and it returns numinsertedrows = 1200. You run the identical statement again a minute later, and no files have been added to the folder. What do you expect the second result to show, and why?
Show answer
It shows 0 for numaffectedrows and numinsertedrows. COPY INTO records each file it loaded for the target table. On the second run it lists the folder, finds that every file is already recorded, and skips them all.
Exercise
A colleague loads CSV files with FILEFORMAT = CSV and no FORMAT_OPTIONS into a table with a header row in each file. List two problems you expect, and the option that fixes the first one.
Show answer
The header line is read as a data row, which can fail when it is parsed into a typed column or load as bad data. Without inferSchema, columns from a schema-less load also come in as STRING. Adding 'header' = 'true' to FORMAT_OPTIONS fixes the header problem. Adding 'inferSchema' = 'true' addresses the typing problem, or you can define the column types in the target table.