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

时间:2012-10-09 01:15:11

标签: sql r rodbc

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

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

我想用一个陈述类似

的子句写一个RODBC sqlQuery
WHERE idname IN IDlist

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

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

2 个答案:

答案 0 :(得分:8)

您始终可以使用paste

构建语句
IDlist <- c(23, 232, 434, 35445)
paste("WHERE idname IN (", paste(IDlist, collapse = ", "), ")")
#[1] "WHERE idname IN ( 23, 232, 434, 35445 )"

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

答案 1 :(得分:0)

我通过组合提示herehere并批量运行,将类似问题的解决方案放在一起。接下来是近似代码(从隔离的机器重新输入):

#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
}