Snowflake SQL - Dynamically Create Table based on File Upload - Error

Hey abretten,

Welcome to to the community!

There are 2 things I would try
Previous Context

  1. [Try First] Once the file is uploaded, iterate over the columns and create a string like you were doing previously but instead of using the column names directly in your sql statement, create variables and reference those.
    Example:
-- Set the table name and columns dynamically
SET table_name = 'YOUR_TABLE_NAME_HERE';
SET columns = 'col_1 STRING, col_2 STRING, col_3 INT';  -- Define columns as a string

-- Use EXECUTE IMMEDIATE to create or replace the table dynamically
EXECUTE IMMEDIATE 
$
CREATE OR REPLACE TABLE IDENTIFIER($table_name) ($columns);
$;

  1. [Not Recommended] If the first option doesn't work, try Disabling prepared statements on your Snowflake Resource. This is not recommended depending on the sensitivity of your data due to the loss of protection of sql injection, but can be used to verify if the dynamic statement works.

Give those a try and keep us updated! :pray:

1 Like