I want to use dbplyr
syntax to do some JOIN
/ FILTER
operations on some tables and store the results back to the Database without collecting it first.
From what I read compute(..., temporary = FALSE, ...)
should be doing that, however I struggle of how to provide the fully qualified name (that is database.schema.table_name
) to the table where I want to store
I know about DBI::Id
and dbplyr::in_schema
but I do not know how use them properly. A try with sql
did at least what I wanted (created the table) but resulted in a (spurios?) error.
What do I need to do?
Some NoReprex
library(DBI)
library(dbplyr)
con <- dbConnect(odbc::odbc(), "myserver")
## do __not__ collect the data
my_frame <- con %>%
tbl(Id(catalog = "mydb", schema = "dbo", table = "mytable")) %>%
inner_join(con %>% tbl(Id(catalog = "mydb", schema = "dbo",
table = "yetanothertable")),
"id")
compute(my_frame,
# Id(catalog = "mydb", schema = "dbo", table = "mynewtable"), # (1)
# in_schema("dbo", "mynewtable"), # (2),
sql("mydb.dbo.mynewtable"), # (3)
FALSE)
I get different error depending on th variant I am using
# (1)
## Error in h(simpleError(msg, call)) :
## error in evaluating the argument 'conn' in selecting a method for function
## 'dbQuoteIdentifier': argument "con" is missing, with no default
# (2)
## Error in escape(x$schema, con = con) :
## argument "con" is missing, with no default
# (3)
## Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ')'.
## [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared.
## <SQL> 'SELECT *
## FROM (my.fully_qualified.name) "q02"
## WHERE (0 = 1)'
P.S.: I really want to be able to save the table with the fully qualified name, that is including the database name (though it is the same in this simplified example). So a dbConnect(..., database = <somedb>)
won't solve my problem in the long run.
P.P.S: I am looking for a compute
solution. I know I could construct the SQL
myself, but I am really interested to see whether I can use the dbplyr
abstraction layer for that.