简体   繁体   中英

Query MS SQL using R with criteria from an R data frame

I have rather a large table in MS SQL Server (120 million rows) which I would like to query. I also have a dataframe in R that has unique ID's that I would like to use as part of my query criteria. I am familiar with the dplyr package but not sure if its possible to have the R query execute on the MS SQL server rather than bring all data onto my laptop memory (likely would crash my laptop).

of course, other option is to load the dataframe onto sql as a table which is currently what I am doing but I would prefer not to do this.

depending on what exactly you want to do, you may find value in the RODBCext package.

let's say you want to pull columns from an MS SQL table where IDs are in a vector that you have in R. you might try code like this:

library(RODBC)
library(RODBCext)
library(tidyverse)

dbconnect <- odbcDriverConnect('driver={SQL Server};
                          server=servername;database=dbname;trusted_connection=true')

v1 <- c(34,23,56,87,123,45)

qdf <- data_frame(idlist=v1)

sqlq <- "SELECT * FROM tablename WHERE idcol %in% ( ? )"

qr <- sqlExecute(dbconnect,sqlq,qdf,fetch=TRUE)  

basically you want to put all the info you want to pass to the query into a dataframe. think of it like variables or parameters for your query; for each parameter you want a column in a dataframe. then you write the query as a character string and store it in a variable. you put it all together using the sqlExecute function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM