I am trying to use a parametrized query multiple times with package:ROracle
.
library(DBI)
conn <- dbConnect(ROracle::Oracle(), ...)
statement <- "select * from bigtable where name = :1"
name <- c("Bob", "Alice", "Carol")
I expected the query to be executed for each row of the data frame (i.e. as for to the param
argument in DBI::dbGetQuery()
), but it is not the case:
dbGetQuery(conn, statement, data.frame(name))
# Error in .oci.GetQuery(conn, statement, data = data, prefetch = prefetch, :
# bind data has too many rows
I can loop over the names, but I think it may be less efficient (I am new to RDBMS but I read about optimization/caching):
lapply(name, function(i) dbGetQuery(conn, statement, data.frame(i)))
How can it be done efficiently?
There are some related questions that does not solve the problem:
- In this (old, unanswered) question, OP uses now-depreceated functions in
package:ROracle
. - In this question, the problem was finally "solved" using a loop, which is inefficient (see above).