'COPY INTO <table> in snowflake throws table does not exist

I am trying to copy data to snowflake from local and I am getting

snowflake.connector.errors.ProgrammingError: 001757 (42601): SQL compilation error: Table 'RAW_DATA' does not exist

the same code is working in Jupiter notebook but it doesn't work in vs code. My role is accountadmin so no issue with the permissions.

Code I am trying to run is this

COPY INTO RAW_DATA file_format=(FIELD_OPTIONALLY_ENCLOSED_BY ='"' skip_header=1)


Solution 1:[1]

I suspect you are using a different Database/schema than the Jupiter notebook

select current_warehouse(), current_database(), current_schema();

for me gives:

CURRENT_WAREHOUSE() CURRENT_DATABASE() CURRENT_SCHEMA()
COMPUTE_WH TEST TMP

and your local will be somewhere different.

The simple way to fix this is to always use fully qualifed names. Thus for me:

COPY INTO test.tmp.raw_data file_format=(FIELD_OPTIONALLY_ENCLOSED_BY ='"' skip_header=1)

Solution 2:[2]

Try replacing 'TABLE' with your table name.

Example:

COPY INTO mytable FROM 's3://mybucket/./../a.csv';

Solution 3:[3]

There is a syntax error. You cannot name use TABLE as a table name. TABLE is a reserved keyword https://docs.snowflake.com/en/sql-reference/reserved-keywords.html

From the docs: https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html

COPY INTO [<namespace>.]<table_name>
     FROM { internalStage | externalStage | externalLocation }
[ FILES = ( '<file_name>' [ , '<file_name>' ] [ , ... ] ) ]
[ PATTERN = '<regex_pattern>' ]
[ FILE_FORMAT = ( { FORMAT_NAME = '[<namespace>.]<file_format_name>' |
                    TYPE = { CSV | JSON | AVRO | ORC | PARQUET | XML } [ formatTypeOptions ] } ) ]
[ copyOptions ]
[ VALIDATION_MODE = RETURN_<n>_ROWS | RETURN_ERRORS | RETURN_ALL_ERRORS ]

So instead of

COPY INTO TABLE FILE_FORMAT = (FIELD_OPTIONALLY_ENCLOSED_BY ='"' skip_header=1)

try

 COPY INTO MYTABLE FILE_FORMAT = (FIELD_OPTIONALLY_ENCLOSED_BY ='"' skip_header=1)

Of course you will need to create MYTABLE first.

Solution 4:[4]

It was not working with

COPY INTO RAW_DATA file_format=(FIELD_OPTIONALLY_ENCLOSED_BY ='"' skip_header=1)

But Then I tried

table_name = "RAW_DATA"
f"COPY INTO {table_name} file_format=(FIELD_OPTIONALLY_ENCLOSED_BY ='"' skip_header=1)"

it started working. Not sure why but this seems working for now

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simeon Pilgrim
Solution 2 ratloser
Solution 3
Solution 4 Aakash aggarwal