如何存储sqlQuery数据框?

时间:2013-02-21 10:02:12

标签: r dataframe rodbc

我迭代一组日期,用于创建一个SQL查询。

  

日期< - c(“2010-06-29”,“2010-07-09”,“2010-07-22”)

fn1 <- function (N) 
{
    queryStart <- "SELECT something FROM somethingelse WHERE table.date ='"
    queryEnd <- "'AND table.something = 'foo'"
    for(i in 1:length(N)) {
        query1 <- paste(queryStart,N[i],queryEnd)
        result <- sqlQuery(con, query1)  //print(sqlQuery(con,query1) works as intended
    }
    return (result)
}
  

FN1(日期)

如何将所有数据放在一个数据框中?有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

尝试这个,我没有测试过它......

fn1 <- function (N) 
{
    queryStart <- "SELECT something FROM somethingelse WHERE table.date ='"
    queryEnd <- "'AND table.something = 'foo'"
    allQueries<-data.frame(matrix(nrow=0,ncol=0));
    for(i in 1:length(N)) {
        query1 <- paste(queryStart,N[i],queryEnd)
        result <- sqlQuery(con, query1)  //print(sqlQuery(con,query1) works as intended
        if (nrow(allQueries) == 0){
            allQueries<-result;
        } 
        else{
            allQueries<-rbind(allQueries,result);
        }
    }
    return (allQueries);
}