I am facing a strange issue when trying to write a table to a Snowflake database. I cannot create a table with name same as the table in another schema.
library(DBI)
df <- data.frame(col1 = rnorm(5), col2 = rnorm(5))
con1 <- DBI::dbConnect(odbc::odbc(), Server = server,
port = 443, Driver = "SnowflakeDSIIDriver", database = "db",
Warehouse = "warehouse", role = "role", schema = "schema1",
authenticator = "oauth", token = token)
# This creates the table named TEMP with 5 rows
DBI::dbWriteTable(conn = con1, 'TEMP', value = df, append = TRUE) #works
# Repeat the same thing to append 5 more rows
DBI::dbWriteTable(conn = con1, 'TEMP', value = df, append = TRUE) #works
# Now let's change the schema
con1 <- DBI::dbConnect(odbc::odbc(), Server = server,
port = 443, Driver = "SnowflakeDSIIDriver", database = "db",
Warehouse = "warehouse", role = "role", schema = "schema2",
authenticator = "oauth", token = token)
# Try to write it to TEMP table in schema2
DBI::dbWriteTable(conn = con1, 'TEMP', value = df, append = TRUE) #error
Error in new_result(connection@ptr, statement, immediate) : nanodbc/nanodbc.cpp:1412: 42S02: SQL compilation error: Table 'TEMP' does not exist or not authorized.
# Change the table name to TEMP1
DBI::dbWriteTable(conn = con1, 'TEMP1', value = df, append = TRUE) #works
I have tried to search for this a lot but everything directs me to the issue with writing to non-default schema.
Issue Writing to Non-Default Schema when Table Does Not Have "_" In Name
https://github.com/r-dbi/DBI/issues/181
https://github.com/r-dbi/odbc/issues/197
The solution that they suggest is to use the Id function to differentiate schema and table. I tried that but I still get the same error.
table_id <- Id(schema="schema2", table="TEMP")
DBI::dbWriteTable(conn = con1, table_id, value = df, append = TRUE)
Error in new_result(connection@ptr, statement, immediate) : nanodbc/nanodbc.cpp:1412: 42S02: SQL compilation error:Table 'db.schema2.TEMP' does not exist or not authorized.
Any suggestion?
Errors like:
and
A thrown from Snowflake and might not be related specifically to R, or at least you can exclude this to move forward with the problem.
I suggest the following approach to troubleshoot this kind of issues.
Activity-Query History. If the username that you use in your R connection is different from the one you use in UI, on the top of the screen change the filter ```USER```` to select the R username.FAILED.Query IDdisplayed in the Details section.Make sure that the
role_name,database_name,schema_name, are the ones you expected.If this is the case, then you can go back to step 4. And copy the SQL text of the query.
Open a new worksheet in UI, set the session context by use commands
INSERTvalues into your schema2.TEMP table. Examples.schema2. You can verify that by Usingand see what your role can do.
schema1you can useshow grants on schema schema1and see the required permissions. If some of the permissions are missed, you can grant this permissions by using GRANT command, e.g.doc reference
If it does not help you can always reach out to snowflake support.