I'm using dbplyr to access a complex warehouse in snowflake, which contains multiple databases. I have write access to one of them, and read access to the rest. Sample structure
WH_a
- schema_a
- table_aa
- schema_b
- table_ba
WH_b
- schema_c
- table_ca
Following dbplyr documentation, I set the working database and schema to be "WH_a.schema_a":
dbGetQuery(conn, "USE DATABASE WH_a")
dbGetQuery(conn, "USE SCHEMA schema_a")
and try to create table references. Simple table referencing works fine in the same schema:
aa <- tbl(conn, "table_aa")
if I want to reference a table in a different schema (same WH_a), I can make use of in_schema() function with no problem:
ba <- tbl(conn, in_schema("schema_b", "table_ba"))
However, I run into problems when I try to reference a table in a different warehouse.
ca <- tbl(conn, in_schema("WH_b.schema_c", "table_ca"))
nanodbc/nanodbc.cpp:1374: 00000: SQL compilation error:
Schema 'WH_a."WH_b.schema_c"' does not exist or not authorized.
Looks like the in_schema call inherits the current database and is not able to go one level above. I've been all over the docs, but most examples refer to much simpler databases where this is not an issue. Testing various combinations of setting and unsetting different schemas/warehouses did not result in success... Eventually I did find work around by passing a straight sql statement
ca <- tbl(conn, sql("SELECT * FROM WH_b.schema_c.table_ca"))
However, this creates very ugly (and possibly inefficient) SQL code, where the select statement is inserted in brackets instead of just a table name. It is much harder to read and just does feel like the right thing to do in the long run
Is there a simpler/more efficient solution to this?
Many thanks