Pass R Vector to Sql query
Asked Answered
B

4

7

I'm using RODBC package to access my sql database in R. I haven't been able to find any useful information on how to pass a vector from R to sql as a vector.

    id <- ('00003', '00100')
    query <- sqlQuery(channel = channel, query = "select *
                  from prod_cust_vw.store_dim
                  where store_num in id")

I would like to pass the id vector to sql rather than hard coding it.

Batt answered 15/12, 2017 at 14:16 Comment(1)
Use the paste or sprintf i.e.e paste("select * from prod_cust_vw.store_dim where store_num in", id)Atlantes
B
-1

The new dbplyr package has the best answer for this. It allows any R object to be used, and automatically converts it to SQL

https://db.rstudio.com/dplyr/

Batt answered 18/6, 2018 at 15:22 Comment(0)
T
17

1) sprintf Convert id into a string suitable for inclusion in the SQL statement and then insert it into the sql string using sprintf See ?sprintf .

id <- c('00003', '00100')
idString <- toString(sprintf("'%s'", id))  # "'00003', '00100'"
sql_fmt <- "select * from prod_cust_vw.store_dim where store_num in (%s)"
sql <- sprintf(sql_fmt, idString)
sql
## [1] "select * from prod_cust_vw.store_dim where store_num in ('00003', '00100')"

2) fn$ or use fn$ from the gsubfn package. Prefacing sqlQuery (or any R function) with fn$ causes the actual arguments to be scanned and the $variables replaced with their contents (where the variable names should only contain letters and numbers in order for it to distinguish between them and other strings). See ?fn .

library(gsubfn)

fn$sqlQuery(channel = channel, query = "select *
       from prod_cust_vw.store_dim
       where store_num in ($idString)")
Tayib answered 15/12, 2017 at 14:26 Comment(0)
P
0

I do it like this.

library("RODBC")


dbhandle <- odbcDriverConnect('driver={SQL Server};server=Your_Server_Name;database=Your_Database_Name;trusted_connection=true')

currTableSQL<-paste("SELECT * FROM Your_Table",sep="")

currTableDF<-sqlQuery(dbhandle,currTableSQL)
Piccalilli answered 21/12, 2017 at 21:32 Comment(0)
B
-1

The new dbplyr package has the best answer for this. It allows any R object to be used, and automatically converts it to SQL

https://db.rstudio.com/dplyr/

Batt answered 18/6, 2018 at 15:22 Comment(0)
D
-1

I want to try and figure out how to do this using R Notebook SQL chunk, but have not been able to figure it out. I had lots of trouble with other methods. This works for me.

library(RODBC)
myconn<-odbcConnect(dsn = "Data Source Name", 
                uid = rstudioapi::askForPassword("User ID"), 
                pwd = rstudioapi::askForPassword("Database password"))

id<-('00003', '00100') # vector of ID's

id<-paste0(personid, collapse = ", ") #create string for entry into query

query<-paste("select *
              from prod_cust_vw.store_dim
              where store_num in (", id,")", sep = "") #query -- use "paste". I have not tried with "paste0")

data<-sqlQuery(myconn,query)    #obtain the data by applying the query through the connection.
Dissentient answered 14/3, 2019 at 20:40 Comment(2)
not clear where the "personid" object came from and what it contains :/Respirable
Multiple problems in this solution. 1. id is not a vector defined because it should be id <- c(...) 2. personid should be replaced with id 3. id<-paste0(personid, collapse = ", ") will introduce ',' between two numbers 4. The output doesn't match the desired output.Unspeakable

© 2022 - 2024 — McMap. All rights reserved.