‘COPY INTO’ Snowflake
A feature that enables us to load and unload the data from snowflake’s existing tables into the snowflake stages.

You can use the snowflake copy command to load the data from the local system by internal stage or from the cloud storage like s3 by external stage.
If you are reading this article I assume that you are already aware of the Snowflake stage concept, Cloud storage S3, how to integrate cloud storage with snowflake, and Snowflake storage integration.
Check out my other articles to get some understanding of these topics.
Export Data From Snowflake To S3
Load CSV from Local to Snowflake
Based on the above articles create a storage integration in snowflake( In my case it is AYUSH_SI)
I have two file load1.csv and load2.csv that contains two columns NAME and MARKS.


Now upload the load1.csv file to the S3 bucket
(In my case it is s3://dev-ayush-test/test/) and create an external stage to view the data from the loaded CSV file directly into the snowflake.
CREATE OR REPLACE DATABASE AYUSH_DEV;
CREATE OR REPLACE SCHEMA AYUSH_DEV.RAW;
CREATE STAGE impact_stage
STORAGE_INTEGRATION = AYUSH_SI
URL = 's3://dev-ayush-test/test/';
SHOW STAGES;
--Access S3 data
SELECT $1 FROM @test_stage;
We can see that the data has a header and the columns are separated by semicolons so we need to tell the snowflake about our file structure. We can do it by creating a file format in the snowflake and afterward passing the file format in the stage definition.
CREATE OR REPLACE FILE FORMAT csv_semicolon
TYPE = CSV
FIELD_DELIMITER = ';'
SKIP_HEADER = 1
NULL_IF = ('NULL', 'null')
EMPTY_FIELD_AS_NULL = true;
CREATE STAGE impact_stage
STORAGE_INTEGRATION = AYUSH_SI
URL = 's3://dev-ayush-test/test/'
FILE_FORMAT = csv_semicolon;Now create a table to load the data from the s3 location and run the copy command to load the data into the newly created table.
CREATE OR REPLACE TABLE RAW.STUDENT
(
NAME VARCHAR,
MARKS INTEGER
);
COPY INTO RAW.STUDENT
FROM @test_stage;Let’s run the same copy command again and see what happens.

We received a status saying 0 files have been processed.
So, did you notice that the COPY INTO is smart enough to know which files it already loaded and doesn’t load the same file, twice?
Snowflake is designed like this to help you. Without any special effort on your part, you have a process that doesn’t double-load records. In other words, it automatically enables you to keep your processes IDEMPOTENT.
But, what if, for some crazy reason, you wanted to double-load your files?
You could add a FORCE=TRUE; as the last line of your COPY INTO statement and then you would double the number of rows in your table.
Then, what if you wanted to start over and load just one copy of each file?
You could TRUNCATE TABLE; set FORCE=FALSE, and run your COPY INTO again.
The COPY INTO is brilliant, which makes it useful and efficient!!
What next?
Use your learning to load the data from S3 to snowflake and try other parameters e.g TYPE=JSON etc in the COPY INTO.
One hint same copy command is used to unload the data from snowflake tables to S3 buckets try it out by yourself.
If you think that this article is informative and helped you with what you are looking then give a clap and follow my medium account(
datageeks.medium.com ).
Feel free to write in the comments or email me at [email protected] in case of any questions about this topic.
By signing up as a member (https://datageeks.medium.com/membership), you can read every story and help the authors on Medium.





