Sub topic 13
Running COPY INTO on a schedule and reading its results
This chapter assumes you can write a basic COPY INTO statement and know it skips files it has already loaded. It covers how to run the statement on a timer and how to check that each run did what you expected.
Why a schedule works
COPY INTO tracks which files it has loaded into the target table. A scheduled job can therefore run the same statement every time. Each run loads only the files that arrived since the last run. If nothing new has arrived, the run succeeds and loads nothing.
Scheduling the statement
You have two common options:
- A job with a SQL task. Put the statement in a SQL file or saved query. Add it as a task in a Databricks job, and set a cron or interval trigger. Run it on a SQL warehouse.
- A job with a notebook task. Use this when
COPY INTOis one step among several, such as a load followed by a cleanup query. The statement runs through a SQL cell orspark.sql.
Keep the statement identical on every run. Do not add a date to the path unless you want to change which files are in scope. The tracking of loaded files is what prevents duplicates.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');Reading the result
COPY INTO returns a one-row result table. The columns are:
| Column | Meaning |
|---|---|
| `num_affected_rows` | Rows touched by this run |
| `num_inserted_rows` | Rows written to the table |
| `num_skipped_corrupt_files` | Files skipped because they could not be read |
A run that finds no new files returns zeros. That result is normal and does not mean something broke. A nonzero num_skipped_corrupt_files deserves a look even though the run succeeded.
Checking the table history
The job's output shows one run. The table's Delta history shows all of them. Each load that writes data adds a COPY INTO entry.
SELECT version, timestamp, operation, operationMetrics
FROM (DESCRIBE HISTORY main.sales.orders)
WHERE operation = 'COPY INTO'
ORDER BY version DESC
LIMIT 10;Use this to confirm that loads happen at the expected cadence. A gap between timestamps can mean the schedule stopped, or that no files arrived. Compare it with the source folder to tell which.
Making failures visible
Set the job to send a notification when a run fails. This catches errors such as a missing permission, a deleted volume, or a file that no longer matches the table schema. A failed run changes nothing in the table for the files that did not load, so the next scheduled run retries them.
You can also add a check task after the load. It runs a query and fails the job if the result looks wrong:
SELECT count(*) AS rows_today
FROM main.sales.orders
WHERE order_ts >= current_date();Alert on a result of zero on days when you expect data.
Common errors
- Reading zeros as failure. Zeros mean there was nothing new to load.
- Scheduling more often than files arrive. This wastes warehouse time but does no harm.
- Changing the source path between runs. Files at the new path are treated as new files.
Exercise
Your nightly COPY INTO job succeeded, but numinsertedrows is 0 and numskippedcorrupt_files is 0. Name two possible explanations and one way to tell them apart.
Show answer
Either no new files arrived since the last run, or every file in the source was already loaded. Both look the same in the result. To tell them apart from a problem such as a wrong path, list the source folder and check whether it contains files newer than the last COPY INTO entry in DESCRIBE HISTORY for the table.