Snowflake SQL Tutorial 126 - I Ran The Same COPY INTO Twice And The Second One Did Nothing

Snowflake SQL · Episode 126

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

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
-- 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.

Query
LIST @STAGE_T126/in/;
Result2 rows · 4 columns
namesizemd5last_modified
stage_t126/in/t126_a.csv8089692ada31ba06d0362cb82c290eea6eThu, 1 Oct 2026 14:21:31 GMT
stage_t126/in/t126_b.csv64d603f92d1bcfe8a56a94ae4f5ee948adThu, 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.

Query
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;
Result5 rows · 4 columns
WHICH_FILESALE_IDCITYAMOUNT
in/t126_a.csv1Toronto100.00
in/t126_a.csv2Boston250.50
in/t126_a.csv3Zurich75.25
in/t126_b.csv4London310.00
in/t126_b.csv5Austin45.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.

Query
SELECT COUNT(*) AS ROWS_IN_THE_TABLE FROM TARGET_T126;
Result1 row · 1 column
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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');
Result2 rows · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_b.csvLOADED2210NULLNULLNULLNULL
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL

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.

One thing to watch

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.

Query
SELECT SALE_ID AS SALE_ID, CITY AS CITY, AMOUNT AS AMOUNT
FROM TARGET_T126
ORDER BY SALE_ID;
Result5 rows · 3 columns
SALE_IDCITYAMOUNT
1Toronto100.00
2Boston250.50
3Zurich75.25
4London310.00
5Austin45.00

Five rows, exactly what the files said. And counted up:

Query
SELECT COUNT(*)    AS ROWS_IN_THE_TABLE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM TARGET_T126;
Result1 row · 2 columns
ROWS_IN_THE_TABLETOTAL_AMOUNT
5780.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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');
Result1 row · 1 column
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.

Query
SELECT COUNT(*)    AS ROWS_IN_THE_TABLE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM TARGET_T126;
Result1 row · 2 columns
ROWS_IN_THE_TABLETOTAL_AMOUNT
5780.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.

Query
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;
Result13 rows · 2 columns
THE_COLUMNTHE_TYPE
SCHEMA_NAMETEXT
FILE_NAMETEXT
TABLE_NAMETEXT
LAST_LOAD_TIMETIMESTAMP_LTZ
STATUSTEXT
ROW_COUNTNUMBER
ROW_PARSEDNUMBER
FIRST_ERROR_MESSAGETEXT
FIRST_ERROR_LINE_NUMBERNUMBER
FIRST_ERROR_CHARACTER_POSITIONNUMBER
FIRST_ERROR_COL_NAMETEXT
ERROR_COUNTNUMBER
ERROR_LIMITNUMBER

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.

Query
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;
Result2 rows · 5 columns
THE_FILEROWS_LOADEDROWS_PARSEDERRORSTHE_STATUS
stages/180a428f-28a7-4684-a1c9-9de3d5e5ca77/in/t126_a.csv330LOADED
stages/180a428f-28a7-4684-a1c9-9de3d5e5ca77/in/t126_b.csv220LOADED

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.

Query
SELECT COUNT(*) AS LOAD_HISTORY_ROWS_FOR_THIS_TABLE
FROM INFORMATION_SCHEMA.LOAD_HISTORY
WHERE TABLE_NAME = 'TARGET_T126';
Result1 row · 1 column
LOAD_HISTORY_ROWS_FOR_THIS_TABLE
2
The file name is not the name you gave the stage

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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;
Result2 rows · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL
stage_t126/in/t126_b.csvLOADED2210NULLNULLNULLNULL

Both files loaded again. Three and three, two and two, exactly as the first time. Now look at what that did to the table.

Query
SELECT COUNT(*)                   AS ROWS_IN_THE_TABLE,
       COUNT(DISTINCT SALE_ID)    AS DISTINCT_SALE_IDS,
       SUM(AMOUNT)                AS TOTAL_AMOUNT
FROM TARGET_T126;
Result1 row · 3 columns
ROWS_IN_THE_TABLEDISTINCT_SALE_IDSTOTAL_AMOUNT
1051561.50

Ten rows. Five distinct sale ids. And a total of 1561.50, where it was 780.75. Exactly doubled.

Query
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;
Result5 rows · 2 columns
SALE_IDHOW_MANY_COPIES
12
22
32
42
52

Every id in the table is in there twice. Five rows in that grid and every one of them says two copies.

What FORCE is for

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.

Query
TRUNCATE TABLE TARGET_T126;
Result1 row · 1 column
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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');
Result2 rows · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_b.csvLOADED2210NULLNULLNULLNULL
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL

Both files loaded. Three and three, two and two.

Query
SELECT COUNT(*) AS ROWS_IN_THE_TABLE FROM TARGET_T126;
Result1 row · 1 column
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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILES = ('t126_a.csv')
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;
Result1 row · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL

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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
PATTERN = '.*t126_b.*'
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;
Result1 row · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_b.csvLOADED2210NULLNULLNULLNULL

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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILES = ('t126_a.csv')
PATTERN = '.*t126_b.*'
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;
Result1 row · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL

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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILES = ('t126_does_not_exist.csv')
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;
Query failed — Snowflake returned an error
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.

The difference in one line

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.

Query
TRUNCATE TABLE TARGET_T126;
Result1 row · 1 column
status
Statement executed successfully.
Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (TYPE = CSV SKIP_HEADER = 1 FIELD_OPTIONALLY_ENCLOSED_BY = '"')
FORCE = TRUE;
Result2 rows · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL
stage_t126/in/t126_b.csvLOADED2210NULLNULLNULLNULL

Both files loaded, same counts.

Query
SELECT COUNT(*)    AS ROWS_IN_THE_TABLE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM TARGET_T126;
Result1 row · 2 columns
ROWS_IN_THE_TABLETOTAL_AMOUNT
5780.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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FORCE = TRUE;
Query failed — Snowflake returned an error
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.

Query
TRUNCATE TABLE TARGET_T126;
Result1 row · 1 column
status
Statement executed successfully.
Query
COPY INTO TARGET_T126 (SALE_ID, CITY)
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;
Result2 rows · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_b.csvLOADED2210NULLNULLNULLNULL
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL

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.

Query
TRUNCATE TABLE TARGET_T126;
Result1 row · 1 column
status
Statement executed successfully.
Query
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;
Query failed — Snowflake returned an error
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.

Query
SELECT SALE_ID AS SALE_ID, CITY AS CITY, AMOUNT AS AMOUNT
FROM TARGET_T126
ORDER BY SALE_ID;
Result0 rows · 3 columns
SALE_IDCITYAMOUNT

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.

Query
CREATE OR REPLACE TABLE AUDITED_T126 (
    SALE_ID     INT,
    CITY        VARCHAR(20),
    AMOUNT      NUMBER(10,2),
    CAME_FROM   VARCHAR(200)
);
Result1 row · 1 column
status
Table AUDITED_T126 successfully created.
Query
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')
);
Result2 rows · 10 columns · scroll →
filestatusrows_parsedrows_loadederror_limiterrors_seenfirst_errorfirst_error_linefirst_error_characterfirst_error_column_name
stage_t126/in/t126_b.csvLOADED2210NULLNULLNULLNULL
stage_t126/in/t126_a.csvLOADED3310NULLNULLNULLNULL

Both files loaded. And now every row knows where it came from.

Query
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;
Result2 rows · 3 columns
CAME_FROMROWS_FROM_THAT_FILETOTAL_FROM_THAT_FILE
in/t126_a.csv3425.75
in/t126_b.csv2355.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.

Query
COPY INTO NOT_A_TABLE_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');
Query failed — Snowflake returned an error
001757 (42601): SQL compilation error:
Table 'NOT_A_TABLE_T126' does not exist

Then a stage that does not exist.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126_NOPE/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126');
Query failed — Snowflake returned an error
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.

Query
COPY INTO AUDITED_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'CSV_T126')
FORCE = TRUE;
Query failed — Snowflake returned an error
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.

Query
COPY INTO TARGET_T126
FROM @STAGE_T126/in/
FILE_FORMAT = (FORMAT_NAME = 'NO_SUCH_FORMAT_T126')
FORCE = TRUE;
Query failed — Snowflake returned an error
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.

Cleanup
-- 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;
Query
LIST @STAGE_T126/;
Result0 rows · 4 columns
namesizemd5last_modified

Nothing left on the stage. And a last check that the schema is how we found it.

Query
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;
Result1 row · 6 columns · scroll →
THE_DATABASETHE_SCHEMAPROBE_TABLES_LEFTPROBE_STAGES_LEFTPROBE_FORMATS_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC00010

No probe tables, no probe stages, no probe formats, and the database still reads.

Key points

  • COPY INTO keeps 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_NAME carries the stage's internal id, not the name you gave the stage, so you cannot join it to a stage by name.
  • FORCE = TRUE ignores the record. On this table that took it from 780.75 to 1561.50, with every id in there twice.
  • TRUNCATE TABLE clears the load record as well as the rows, so the same load with no FORCE runs again.
  • FILES and PATTERN together is not refused. FILES wins.
  • A PATTERN that matches nothing is silent. A FILES name that is missing is 091016.
  • 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 INT column.
  • A column list with no SELECT is accepted. UPPER() inside a COPY is not: Function 'UPPER' not supported within a COPY.
  • METADATA$FILENAME is allowed, which is how you get an audit column that records which file each row came from.
Next in this series

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.