2

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

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

我想写一个 RODBC sqlQuery,其中有一个子句说明类似

WHERE idname IN IDlist

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

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

4

2 回答 2

8

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

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

显然,您需要为此添加更多内容以构建您的确切陈述

于 2012-10-09T01:31:31.900 回答
0

我通过结合此处此处的提示并分批运行,为类似问题提出了解决方案。大致代码如下(从隔离机器重新输入):

#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
}
于 2016-07-01T17:20:43.343 回答