提供R向量的查找列表作为RODBC查找的SQL表

Providing lookup list from R vector as SQL table for RODBC lookup

我有一个R矢量中的ID列表。

1
IDlist <- c(23, 232, 434, 35445)

我想写一个RODBC sqlQuery,带有一个陈述类似的子句

1
WHERE idname IN IDlist

我是否必须读取整个表,然后将其合并到R中的idList向量中? 或者我如何将这些值提供给RODBC语句,以便只恢复我感兴趣的记录?

注意:由于列表很长,将单个值粘贴到SQL语句中,如下面的答案所示,将不会这样做。


您总是可以使用paste构造语句

1
2
3
IDlist <- c(23, 232, 434, 35445)
paste("WHERE idname IN (", paste(IDlist, collapse =","),")")
#[1]"WHERE idname IN ( 23, 232, 434, 35445 )"

显然,您需要添加更多内容来构建您的确切语句


我将这里和这里的提示结合起来并分批运行,从而为类似的问题提出了解决方案。 接下来是近似代码(从隔离的机器重新输入):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#assuming you have a list OF IDs you want TO MATCH IN vIDs AND an RODBC connection IN mycon

#queries that don't change
q_create_tmp <-"create table #tmptbl (ID int)"
q_get_records <-"select * from mastertbl as X join #tmptbl as Y on (X.ID = Y.ID)"
q_del_tmp <-"drop table #tmptbl"

#initialize counters and storage
start_row <- 1
batch_size <- 1000
allresults <- data.frame()

while(start_row <= length(vIDs) {
    end_row <- min(length(vIDs), start_row+batch_size-1)
    q_fill_tmp <- sprintf("insert into #tmptbl (ID) values %s", paste(sprintf("(%d)", vIDs[start_row:end_row]), collapse=","))

    q_all <- list(q_create_tmp, q_fill_tmp, q_get_records, q_del_tmp)
    sqlOutput <- lapply(q_all, function(x) sqlQuery(mycon, x))

    allresults <- rbind(allresults, sqlOutput[[3]])
    start_row <- end_row + 1
}