I am having a very difficult time passing a column from a DataFrame as well as a Date parameter into a SQL query in R. I have recently stumbled upon the idea of parametrized queries which looks promising for simplifying what I've been doing.
The setup is the following:
#Column of values I wish to use in the query in my where clause
values <- df$'column'
vallist<-list(values)
library(tidyverse)
library(DBI)
library(glue)
library(openxlsx)
#Function I wish to define with parameters the date and the values from above
Data_Function <- function(database,date,vals){
query <- glue_sql("
SELECT column1,
column2,
dateof
FROM database
WHERE dateof>=?
AND column1 IN ?
")
df0<-dbSendQuery(database,query)
dbBind(df0,date,vals)
dbFetch(df0)
}
Data_Function(db,c('2021-12-01',EB_List))
When I try to run this I get the following error:
Error in result_bind(res@ptr, params, batch_rows) :
nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '@P2'. [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.
It does not seem to like my "AND column1 IN ?" clause. What is the proper way to pass both of these arguments?
Thank you