Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Snowflake Table Insert: How to Add Data Effectively

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert values into a Snowflake table, use the INSERT INTO statement for small, manual data entries or the COPY INTO command for bulk loading large datasets from files. For a single row, the syntax is INSERT INTO table_name (column1, column2) VALUES (value1, value2);. For large-scale data ingestion, the most efficient method is uploading files to a stage and using the COPY INTO command to move that data into the target table.

What You'll Need

  • A Snowflake account with active login credentials.
  • A Virtual Warehouse that is started or capable of auto-resuming to provide the necessary compute power.
  • An existing database and schema where the target table resides.
  • A table already created with defined columns and data types.
  • A role (such as SYSADMIN or a custom role) that has the INSERT privilege on the target table.
  • For bulk loading: Data files (CSV, JSON, Parquet, etc.) and access to a Snowflake Stage (Internal or External).

Step-by-Step

  1. Set Your Session Context
    Before running any data manipulation commands, you must ensure your session is pointed at the correct environment. If you are using the Snowsight web interface, you can select these from the dropdown menus, or you can execute the following SQL commands. Note that labels and availability may differ slightly depending on your account version or region:
    USE ROLE my_role;
    USE WAREHOUSE my_warehouse;
    USE DATABASE my_database;
    USE SCHEMA my_schema;
  2. Insert a Single Row of Data
    Use the INSERT INTO statement to add a single record. It is best practice to explicitly list the columns you are populating to avoid errors if the table structure changes in the future.
    INSERT INTO employees (employee_id, first_name, last_name, hire_date)
      VALUES (101, 'Jane', 'Doe', '2023-05-15');
    In this example, the strings 'Jane', 'Doe', and the date are enclosed in single quotes, which is required for these data types in Snowflake.
  3. Insert Multiple Rows in One Statement
    To improve efficiency when adding a few dozen rows, you can provide multiple sets of values separated by commas. This reduces the number of individual transactions the system must process.
    INSERT INTO employees (employee_id, first_name, last_name, hire_date)
      VALUES 
      (102, 'John', 'Smith', '2023-06-01'),
      (103, 'Alice', 'Johnson', '2023-06-10'),
      (104, 'Bob', 'Brown', '2023-06-15');
  4. Insert Data from Another Table
    If you need to move data from a staging table or a temporary table into a permanent one, use the INSERT INTO ... SELECT pattern. This allows you to filter or transform the data using SQL during the process.
    INSERT INTO employees (employee_id, first_name, last_name, hire_date)
      SELECT id, fname, lname, start_date
      FROM staging_employees
      WHERE status = 'Active';
    This method is highly efficient for internal data movement within Snowflake.
  5. Perform a Bulk Load Using COPY INTO
    For thousands or millions of rows, INSERT statements are too slow and costly. Instead, use the COPY INTO command. This process typically involves three sub-steps:
    • Upload the file: Use the PUT command (via SnowSQL) or the "Load Data" button in the Snowsight UI to move a file from your local machine to a Snowflake Internal Stage (e.g., @my_internal_stage).
    • Define a File Format: Create a file format object to tell Snowflake how to read the file (e.g., delimiter, header skipping).
      CREATE OR REPLACE FILE FORMAT my_csv_format
            TYPE = 'CSV'
            FIELD_DELIMITER = ','
            SKIP_HEADER = 1;
    • Execute the Load: Run the COPY INTO command to move the data from the stage into the table.
      COPY INTO employees
            FROM @my_internal_stage/employees_data.csv
            FILE_FORMAT = (FORMAT_NAME = 'my_csv_format');
  6. Verify the Insertion
    Once the command completes, run a simple query to ensure the data is present and correctly aligned with the columns.
    SELECT * FROM employees LIMIT 10;

Common Mistakes

  • Omitting the Warehouse: Users often forget that INSERT and COPY operations require compute power. If no warehouse is active or selected, the query will fail with an error stating that no warehouse is specified.
  • Incorrect Quote Usage: Using double quotes for string values instead of single quotes. In Snowflake, double quotes are used for identifiers (like table or column names that have spaces or are case-sensitive), while single quotes are used for literal string values.
  • Data Type Mismatch: Trying to insert a string into a numeric column or a malformed date string into a DATE column. Snowflake is strict about data types during INSERT operations.
  • Case Sensitivity Issues: By default, Snowflake converts unquoted identifiers to uppercase. If you created your table with double quotes (e.g., CREATE TABLE "Employees"...), you must use double quotes in your INSERT statement as well.
  • Loading Files without a File Format: Attempting to use COPY INTO without specifying a file format, leading to errors where Snowflake cannot determine where one column ends and the next begins.

Tips

  • Use the Snowsight UI for Small Loads: For non-technical users or very small files, the "Load Data" wizard in the Snowsight web interface simplifies the PUT and COPY INTO process into a few clicks.
  • Optimize Bulk Loads: When using COPY INTO, split very large files into smaller chunks (100-250 MB compressed). This allows Snowflake to parallelize the load across all nodes in your warehouse, significantly increasing speed.
  • Handle Errors Gracefully: Use the ON_ERROR = 'CONTINUE'` option in your COPY INTO statement. This prevents the entire load from failing if only a few rows in a million-row file have formatting errors.
  • Purge Files After Loading: Add PURGE = TRUE to your COPY INTO command to automatically delete the data files from the stage after they have been successfully loaded, keeping your storage clean.
  • Use Staging Tables: Instead of inserting directly into a production table, insert data into a temporary staging table first. This allows you to validate the data quality before performing a final INSERT INTO ... SELECT into the production table.

Troubleshooting

If your data is not appearing in the table or you receive an error, follow these steps in order:

1. Check for Permission Errors: If you see an "Insufficient privileges" error, verify that your current role has the INSERT privilege on the table. You may need to ask an administrator to run GRANT INSERT ON TABLE table_name TO ROLE your_role;.

2. Validate Warehouse Status: Ensure your warehouse is not suspended or that you have the rights to resume it. If the warehouse cannot start, the query will hang or fail.

3. Inspect Data Type Mismatches: If you receive a "Numeric value not recognized" or "Date not recognized" error, check the source data for hidden characters, trailing spaces, or incorrect date formats (Snowflake defaults to YYYY-MM-DD). Use a SELECT statement on a small sample of the file to identify the problematic row.

4. Review COPY History: If a COPY INTO command fails, use the VALIDATE function or query the LOAD_HISTORY view to see exactly which line of the file caused the error and why.

5. Verify Stage Path: If Snowflake cannot find the file, double-check the path in your FROM @stage_name/path/ clause. Remember that paths are case-sensitive.

Frequently Asked Questions

What is the difference between INSERT and COPY INTO?

INSERT is used for small, ad-hoc additions of data and is processed as a DML operation. COPY INTO is a bulk-loading command designed to move large volumes of data from files into tables efficiently using the warehouse's parallel processing capabilities.

Can I insert values into a Snowflake View?

No, you cannot directly insert values into a standard view because a view is a stored query, not a physical table. You must insert the data into the underlying base table that the view references.

How do I insert a NULL value?

To insert a NULL value, simply use the keyword NULL without quotes in your VALUES list. For example: INSERT INTO employees (id, phone) VALUES (105, NULL);.

Is there a limit to how many rows I can insert in one statement?

While there is no hard row limit for a single INSERT statement, there is a limit on the total size of the SQL text sent to the server. For very large sets of data, you should always switch to the COPY INTO method to avoid hitting request size limits and to reduce costs.

Does Snowflake support UPSERT (Update or Insert)?

Snowflake does not have a single UPSERT command. To achieve this, you typically use the MERGE statement, which allows you to join a source table with a target table and specify whether to update an existing row or insert a new one based on a matching condition.