In this post we are going to cover COPY INTO in Snowflake, the statement that loads a staged file into a table.
COPY INTO reads files from a stage, parses them with a file format, and puts the rows into a table. It is the load command, and it is the one you will put inside a job and run on a schedule.
There is one thing about it that nobody says out loud, and it is the reason this post exists. COPY INTO keeps a record of which files it has already loaded into which table, and it uses that record to refuse to load them again.
We will start with the simplest load there is and build it up step by step. Every query below was run on a real Snowflake account, and every result you see is the output it gave back.
Episode 126 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's set up a stage, a format and an empty table
- Let's run the simplest COPY INTO there is
- Now let's run the identical statement again
- So where is that decision written down?
- Let's make it load them again with FORCE
- What happens when we TRUNCATE and load again?
- Let's name the files we want with FILES and PATTERN
- What happens when a named file is not on the stage?
- Can we write the format inside the COPY itself?
- Can COPY INTO transform on the way in?
- One last thing: the four ways COPY INTO refuses
- And let's put everything back
Let's set up a stage, a format and an empty table
Before we can load anything we need three things. A stage with files on it, a file format that knows how to read them, and a table to put the rows into.
The script below makes all three. The format has SKIP_HEADER = 1 on it, because both of our files start with a header row.
-- Sample setup for this post.
-- A stage to hold the files, a format that skips the header row,
-- and an empty table to load into.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
CREATE OR REPLACE STAGE STAGE_T126;
CREATE OR REPLACE FILE FORMAT CSV_T126
TYPE = CSV
SKIP_HEADER = 1
FIELD_OPTIONALLY_ENCLOSED_BY = '"';
CREATE OR REPLACE TABLE TARGET_T126 (
SALE_ID INT,
CITY VARCHAR(20),
AMOUNT NUMBER(10,2)
);
-- Two small CSV files, each with a header row, put onto the stage.
-- Point these at a folder of your own.
PUT 'file://C:/your/folder/t126_a.csv' @STAGE_T126/in/ AUTO_COMPRESS = FALSE;
PUT 'file://C:/your/folder/t126_b.csv' @STAGE_T126/in/ AUTO_COMPRESS = FALSE;Let's check that the files actually arrived on the stage.
LIST @STAGE_T126/in/;| name | size | md5 | last_modified |
|---|---|---|---|
| stage_t126/in/t126_a.csv | 80 | 89692ada31ba06d0362cb82c290eea6e | Thu, 1 Oct 2026 14:21:31 GMT |
| stage_t126/in/t126_b.csv | 64 | d603f92d1bcfe8a56a94ae4f5ee948ad | Thu, 1 Oct 2026 14:21:32 GMT |
Both files are there, under the in/ folder we put them in.
Now let's read them before we load anything, so we know what should turn up in the table. METADATA$FILENAME tells us which file each row came out of.
SELECT METADATA$FILENAME AS WHICH_FILE, $1 AS SALE_ID, $2 AS CITY, $3 AS AMOUNT
FROM @STAGE_T126/in/ (FILE_FORMAT => 'CSV_T126')
ORDER BY WHICH_FILE, $1;| WHICH_FILE | SALE_ID | CITY | AMOUNT |
|---|---|---|---|
| in/t126_a.csv | 1 | Toronto | 100.00 |
| in/t126_a.csv | 2 | Boston | 250.50 |
| in/t126_a.csv | 3 | Zurich | 75.25 |
| in/t126_b.csv | 4 | London | 310.00 |
| in/t126_b.csv | 5 | Austin | 45.00 |
Five rows across the two files. Three of them are in one file and two are in the other. Toronto, Boston, Zurich, London and Austin.
And the table has nothing in it yet.
SELECT COUNT(*) AS ROWS_IN_THE_TABLE FROM TARGET_T126;| ROWS_IN_THE_TABLE |
|---|
| 0 |
Zero. That is our starting point.
Let's run the simplest COPY INTO there is
The simplest form is three lines. COPY INTO and the table, then FROM and the stage path, then the format.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_b.csv | LOADED | 2 | 2 | 1 | 0 | NULL | NULL | NULL | NULL |
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
Have a look at the shape of that answer. Ten columns, and a row for each file it looked at.
The columns worth knowing are file, status, rows_parsed and rows_loaded. Both files say LOADED. One parsed three rows and loaded three. The other parsed two and loaded two.
Then there are error_limit and errors_seen. Hold on to those two names. They are the whole subject of the next post.
The two rows do not come back in a fixed order. This statement ran twice in this episode, and the two files swapped places between the two runs. If you need the report in an order, put an ORDER BY on a RESULT_SCAN over it.
Now let's look at the table itself.
SELECT SALE_ID AS SALE_ID, CITY AS CITY, AMOUNT AS AMOUNT
FROM TARGET_T126
ORDER BY SALE_ID;| SALE_ID | CITY | AMOUNT |
|---|---|---|
| 1 | Toronto | 100.00 |
| 2 | Boston | 250.50 |
| 3 | Zurich | 75.25 |
| 4 | London | 310.00 |
| 5 | Austin | 45.00 |
Five rows, exactly what the files said. And counted up:
SELECT COUNT(*) AS ROWS_IN_THE_TABLE,
SUM(AMOUNT) AS TOTAL_AMOUNT
FROM TARGET_T126;| ROWS_IN_THE_TABLE | TOTAL_AMOUNT |
|---|---|
| 5 | 780.75 |
Five rows and a total of 780.75. That number is the one to remember, because we are about to change it by accident.
Now let's run the identical statement again
This is the point of the post. Nothing has changed. Same files, same stage, same format, character for character. Let's run it a second time.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');| status |
|---|
| Copy executed with 0 files processed. |
Look at the shape of that. Not ten columns. One.
Copy executed with 0 files processed. It did not load nothing because the files were empty. It refused to load them twice.
SELECT COUNT(*) AS ROWS_IN_THE_TABLE,
SUM(AMOUNT) AS TOTAL_AMOUNT
FROM TARGET_T126;| ROWS_IN_THE_TABLE | TOTAL_AMOUNT |
|---|---|
| 5 | 780.75 |
Five rows and 780.75, untouched. This is the behaviour that makes a load job safe to re-run, and it is worth saying plainly: COPY INTO is not an INSERT that happens to read a file.
So where is that decision written down?
The record lives in INFORMATION_SCHEMA.LOAD_HISTORY. I did not know the column names, so let's ask the catalog for them first.
SELECT COLUMN_NAME AS THE_COLUMN, DATA_TYPE AS THE_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'INFORMATION_SCHEMA'
AND TABLE_NAME = 'LOAD_HISTORY'
ORDER BY ORDINAL_POSITION;| THE_COLUMN | THE_TYPE |
|---|---|
| SCHEMA_NAME | TEXT |
| FILE_NAME | TEXT |
| TABLE_NAME | TEXT |
| LAST_LOAD_TIME | TIMESTAMP_LTZ |
| STATUS | TEXT |
| ROW_COUNT | NUMBER |
| ROW_PARSED | NUMBER |
| FIRST_ERROR_MESSAGE | TEXT |
| FIRST_ERROR_LINE_NUMBER | NUMBER |
| FIRST_ERROR_CHARACTER_POSITION | NUMBER |
| FIRST_ERROR_COL_NAME | TEXT |
| ERROR_COUNT | NUMBER |
| ERROR_LIMIT | NUMBER |
Thirteen columns. A file name, a table name, a status, a row count, a parsed count, an error count and an error limit.
Now let's query it properly for our table.
SELECT FILE_NAME AS THE_FILE,
ROW_COUNT AS ROWS_LOADED,
ROW_PARSED AS ROWS_PARSED,
ERROR_COUNT AS ERRORS,
STATUS AS THE_STATUS
FROM INFORMATION_SCHEMA.LOAD_HISTORY
WHERE TABLE_NAME = 'TARGET_T126'
ORDER BY FILE_NAME;| THE_FILE | ROWS_LOADED | ROWS_PARSED | ERRORS | THE_STATUS |
|---|---|---|---|---|
| stages/180a428f-28a7-4684-a1c9-9de3d5e5ca77/in/t126_a.csv | 3 | 3 | 0 | LOADED |
| stages/180a428f-28a7-4684-a1c9-9de3d5e5ca77/in/t126_b.csv | 2 | 2 | 0 | LOADED |
Two rows, one per file. Three loaded and three parsed, two loaded and two parsed, both LOADED, no errors. That is the record the second copy read.
SELECT COUNT(*) AS LOAD_HISTORY_ROWS_FOR_THIS_TABLE
FROM INFORMATION_SCHEMA.LOAD_HISTORY
WHERE TABLE_NAME = 'TARGET_T126';| LOAD_HISTORY_ROWS_FOR_THIS_TABLE |
|---|
| 2 |
Have a look at the file name in that grid. It does not say STAGE_T126 anywhere. It starts with stages/ and then a long internal id. So if you are building a report over LOAD_HISTORY, you cannot match that string to a stage by its name.
Let's make it load them again with FORCE
There is an option that tells COPY INTO to ignore the record. It is FORCE = TRUE.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
| stage_t126/in/t126_b.csv | LOADED | 2 | 2 | 1 | 0 | NULL | NULL | NULL | NULL |
Both files loaded again. Three and three, two and two, exactly as the first time. Now look at what that did to the table.
SELECT COUNT(*) AS ROWS_IN_THE_TABLE,
COUNT(DISTINCT SALE_ID) AS DISTINCT_SALE_IDS,
SUM(AMOUNT) AS TOTAL_AMOUNT
FROM TARGET_T126;| ROWS_IN_THE_TABLE | DISTINCT_SALE_IDS | TOTAL_AMOUNT |
|---|---|---|
| 10 | 5 | 1561.50 |
Ten rows. Five distinct sale ids. And a total of 1561.50, where it was 780.75. Exactly doubled.
SELECT SALE_ID AS SALE_ID,
COUNT(*) AS HOW_MANY_COPIES
FROM TARGET_T126
GROUP BY SALE_ID
HAVING COUNT(*) > 1
ORDER BY SALE_ID;| SALE_ID | HOW_MANY_COPIES |
|---|---|
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
Every id in the table is in there twice. Five rows in that grid and every one of them says two copies.
This is the whole reason the load record exists. FORCE = TRUE is you switching it off, and the grid above is what switching it off costs. Use it when you have deliberately truncated and want a reload, not as a habit.
What happens when we TRUNCATE and load again?
Let's empty the table out and then run the same copy with no FORCE at all.
TRUNCATE TABLE TARGET_T126;| status |
|---|
| Statement executed successfully. |
I expected 0 files processed again here, because the files on the stage have not changed. The record should still know about them.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_b.csv | LOADED | 2 | 2 | 1 | 0 | NULL | NULL | NULL | NULL |
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
Both files loaded. Three and three, two and two.
SELECT COUNT(*) AS ROWS_IN_THE_TABLE FROM TARGET_T126;| ROWS_IN_THE_TABLE |
|---|
| 5 |
Five rows back in the table, with no FORCE anywhere in that statement.
So TRUNCATE TABLE forgets the files as well as the rows. That is the reset button, and I did not know it was one.
Let's name the files we want with FILES and PATTERN
We do not have to take everything under the prefix. FILES takes a list of names.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILES = ('t126_a.csv')
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
One row in the report instead of two, and three rows loaded. Only the file we named was looked at.
PATTERN is the other way of narrowing it down, and it is the same regex idea we used to read a stage in Episode 124.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
PATTERN = '.*t126_b.*'
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_b.csv | LOADED | 2 | 2 | 1 | 0 | NULL | NULL | NULL | NULL |
The other file this time, with two rows loaded.
Can we use both at once?
Let's point FILES at one file and PATTERN at a different one in the same statement. I thought this would be refused.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILES = ('t126_a.csv')
PATTERN = '.*t126_b.*'
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
It is not refused. One row came back, and it is the file FILES named. FILES wins and PATTERN is ignored, with nothing said about it.
What happens when a named file is not on the stage?
Let's put a name in that list that is not on the stage at all. I expected this to quietly load nothing.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILES = ('t126_does_not_exist.csv')
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;091016 (22000): Remote file '@stage_t126/in/t126_does_not_exist.csv' was not found. If you are running a copy command, please make sure files are not deleted when they are being loaded or files are not being loaded into two different tables concurrently with auto purge option.
A refusal, with the full path in it. 091016, remote file not found.
That is the opposite of what a PATTERN does. A pattern that matches nothing says nothing, which is what we saw reading a stage in Episode 124. A name in FILES that is missing is an error.
PATTERN that matches nothing: silence.FILES with a name that is not there: 091016.
If you want to be told when an expected file did not turn up, name it in FILES.
Can we write the format inside the COPY itself?
Every load so far has used FORMAT_NAME = 'CSV_T126'. We can spell the options out inside the COPY instead. Let's start from an empty table.
TRUNCATE TABLE TARGET_T126;| status |
|---|
| Statement executed successfully. |
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (TYPE = CSV SKIP_HEADER = 1 FIELD_OPTIONALLY_ENCLOSED_BY = '"')
FORCE = TRUE;| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
| stage_t126/in/t126_b.csv | LOADED | 2 | 2 | 1 | 0 | NULL | NULL | NULL | NULL |
Both files loaded, same counts.
SELECT COUNT(*) AS ROWS_IN_THE_TABLE,
SUM(AMOUNT) AS TOTAL_AMOUNT
FROM TARGET_T126;| ROWS_IN_THE_TABLE | TOTAL_AMOUNT |
|---|---|
| 5 | 780.75 |
Five rows and 780.75. The same load. So a named format is a convenience, not a necessity.
And with no format at all?
The stage has a format of its own, which we set up back in Episode 122. Let's drop the FILE_FORMAT clause and see what the stage's own one does with these files.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FORCE = TRUE;100038 (22018): Numeric value 'sale_id' is not recognized File 'in/t126_b.csv', line 1, character 1 Row 1, column "TARGET_T126"["SALE_ID":1] If you would like to continue loading when an error is encountered, use other values such as 'SKIP_FILE' or 'CONTINUE' for the ON_ERROR option. For more information on loading options, please run 'info loading_data' in a SQL client.
Numeric value 'sale_id' is not recognized, at line one, character one.
That is the header row hitting the SALE_ID INT column. SKIP_HEADER lived in the format we just dropped, so the header is now data.
Can COPY INTO transform on the way in?
Up to a point. Let's start with a column list, which says where the fields in the file should go.
TRUNCATE TABLE TARGET_T126;| status |
|---|
| Statement executed successfully. |
COPY INTO TARGET_T126 (SALE_ID, CITY)
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_b.csv | LOADED | 2 | 2 | 1 | 0 | NULL | NULL | NULL | NULL |
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
Accepted, and both files loaded with the same counts as before. The file has three fields and we only named two columns, and Snowflake was happy with that.
Now a real transform
Let's put a SELECT over the stage instead: cast the id, upper case the city, double the amount.
TRUNCATE TABLE TARGET_T126;| status |
|---|
| Statement executed successfully. |
COPY INTO TARGET_T126 (SALE_ID, CITY, AMOUNT)
FROM (
SELECT $1::INT,
UPPER($2::VARCHAR),
$3::NUMBER(10,2) * 2
FROM @STAGE_T126/in/ (FILE_FORMAT => 'CSV_T126')
)
FORCE = TRUE;002300 (0A000): SQL Compilation error: Function 'UPPER' not supported within a COPY
Function 'UPPER' not supported within a COPY. That is not a syntax error. It is a flat statement that the function is not allowed in this position.
SELECT SALE_ID AS SALE_ID, CITY AS CITY, AMOUNT AS AMOUNT
FROM TARGET_T126
ORDER BY SALE_ID;| SALE_ID | CITY | AMOUNT |
|---|
Nothing in the table, which is what a compilation error should leave behind.
So a transforming COPY is real, but it is a narrow subset of what you can write in a SELECT. Not any expression you like.
But this one works, and it is the one worth having
METADATA$FILENAME is allowed in there. Let's make a table with a CAME_FROM column and load the file name into it.
CREATE OR REPLACE TABLE AUDITED_T126 (
SALE_ID INT,
CITY VARCHAR(20),
AMOUNT NUMBER(10,2),
CAME_FROM VARCHAR(200)
);| status |
|---|
| Table AUDITED_T126 successfully created. |
COPY INTO AUDITED_T126 (SALE_ID, CITY, AMOUNT, CAME_FROM)
FROM (
SELECT $1::INT,
$2::VARCHAR,
$3::NUMBER(10,2),
METADATA$FILENAME
FROM @STAGE_T126/in/ (FILE_FORMAT => 'CSV_T126')
);| file | status | rows_parsed | rows_loaded | error_limit | errors_seen | first_error | first_error_line | first_error_character | first_error_column_name |
|---|---|---|---|---|---|---|---|---|---|
| stage_t126/in/t126_b.csv | LOADED | 2 | 2 | 1 | 0 | NULL | NULL | NULL | NULL |
| stage_t126/in/t126_a.csv | LOADED | 3 | 3 | 1 | 0 | NULL | NULL | NULL | NULL |
Both files loaded. And now every row knows where it came from.
SELECT CAME_FROM AS CAME_FROM,
COUNT(*) AS ROWS_FROM_THAT_FILE,
SUM(AMOUNT) AS TOTAL_FROM_THAT_FILE
FROM AUDITED_T126
GROUP BY CAME_FROM
ORDER BY CAME_FROM;| CAME_FROM | ROWS_FROM_THAT_FILE | TOTAL_FROM_THAT_FILE |
|---|---|---|
| in/t126_a.csv | 3 | 425.75 |
| in/t126_b.csv | 2 | 355.00 |
Three rows and 425.75 from one file, two rows and 355.00 from the other. Those two totals add up to the 780.75 we started with.
That is the audit column everybody wishes they had added, and you can add it on the way in.
One last thing: the four ways COPY INTO refuses
These are worth seeing once so you recognise them later. First, a table that does not exist.
COPY INTO NOT_A_TABLE_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');001757 (42601): SQL compilation error: Table 'NOT_A_TABLE_T126' does not exist
Then a stage that does not exist.
COPY INTO TARGET_T126
FROM @STAGE_T126_NOPE/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');002003 (02000): SQL compilation error: Stage 'DEMO_DB.PUBLIC.STAGE_T126_NOPE' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on STAGE DEMO_DB.PUBLIC.STAGE_T126_NOPE.
Then a file with three fields going into the four column AUDITED_T126 table we made above.
COPY INTO AUDITED_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;100080 (22000): Number of columns in file (3) does not match that of the corresponding table (4), use file format option error_on_column_count_mismatch=false to ignore this error File 'in/t126_b.csv', line 3, character 1 Row 1 starts at line 2, column "AUDITED_T126"["AMOUNT":3] If you would like to continue loading when an error is encountered, use other values such as 'SKIP_FILE' or 'CONTINUE' for the ON_ERROR option. For more information on loading options, please run 'info loading_data' in a SQL client.
The error names both numbers and tells you the file format option that would let it through.
And a format name that is not there.
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'NO_SUCH_FORMAT_T126')
FORCE = TRUE;002003 (02000): SQL compilation error: File format 'NO_SUCH_FORMAT_T126' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on FILE FORMAT NO_SUCH_FORMAT_T126.
Two different problems, both 002003. The text is what tells them apart, so read it rather than the code.
And let's put everything back
Everything in this post was made by the script at the top, so everything in it can go.
-- Put the schema back the way we found it.
REMOVE @STAGE_T126/;
DROP STAGE IF EXISTS STAGE_T126;
DROP STAGE IF EXISTS STAGE_T126_NOPE;
DROP FILE FORMAT IF EXISTS CSV_T126;
DROP TABLE IF EXISTS TARGET_T126;
DROP TABLE IF EXISTS AUDITED_T126;
DROP TABLE IF EXISTS NOT_A_TABLE_T126;LIST @STAGE_T126/;| name | size | md5 | last_modified |
|---|
Nothing left on the stage. And a last check that the schema is how we found it.
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE RIGHT(TABLE_NAME, 5) = '_T126') AS PROBE_TABLES_LEFT,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.STAGES
WHERE STAGE_NAME LIKE '%_T126%') AS PROBE_STAGES_LEFT,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.FILE_FORMATS
WHERE RIGHT(FILE_FORMAT_NAME, 5) = '_T126') AS PROBE_FORMATS_LEFT,
(SELECT COUNT(*) FROM CUSTOMERS) AS DEMO_DB_IS_READABLE;| THE_DATABASE | THE_SCHEMA | PROBE_TABLES_LEFT | PROBE_STAGES_LEFT | PROBE_FORMATS_LEFT | DEMO_DB_IS_READABLE |
|---|---|---|---|---|---|
| DEMO_DB | PUBLIC | 0 | 0 | 0 | 10 |
No probe tables, no probe stages, no probe formats, and the database still reads.
Key points
COPY INTOkeeps a record of which files it loaded into which table, and refuses to load the same file twice.- The first load returns ten columns and a row per file. The second, identical load returns one column:
Copy executed with 0 files processed. - That record is in
INFORMATION_SCHEMA.LOAD_HISTORY, which has thirteen columns. LOAD_HISTORY.FILE_NAMEcarries the stage's internal id, not the name you gave the stage, so you cannot join it to a stage by name.FORCE = TRUEignores the record. On this table that took it from780.75to1561.50, with every id in there twice.TRUNCATE TABLEclears the load record as well as the rows, so the same load with noFORCEruns again.FILESandPATTERNtogether is not refused.FILESwins.- A
PATTERNthat matches nothing is silent. AFILESname that is missing is091016. - A named file format is a convenience. With no format at all, the stage's own one takes over, and here the header row met an
INTcolumn. - A column list with no
SELECTis accepted.UPPER()inside aCOPYis not:Function 'UPPER' not supported within a COPY. METADATA$FILENAMEis allowed, which is how you get an audit column that records which file each row came from.
Episode 127 — ON_ERROR. What happens when the data itself is wrong. The default is that nothing loads, not even the clean file in the same statement, and every other setting is a decision about how much bad data you will accept quietly.
Every query and every result grid on this page was captured from a live Snowflake account (version 10.35.101) running the demo script for this episode. The sales data is made up for teaching. The two PUT statements in the setup script take a path on your own machine, so the folder is written as C:/your/folder and their captured grids are not shown. Cells reading NULL in the COPY report are real nulls. Nothing on this page is timed and no performance claims are made anywhere in this series. The load behaviour follows the Snowflake COPY INTO <table> documentation.



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.