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?