R:如何替换数据库中表的单列

时间:2019-04-01 14:35:39

标签: sql r database

有人知道替代MS SQL Server数据库上表中的单个列的解决方法吗?

重点是,由于内存限制,我想避免将整个表拖入我的R-Session中。代替dbWriteTable(),只能(覆盖)写一个列吗?

还有一个有用的查询,该查询可以通过dbSendQuery()进入数据库,并且考虑了要转移到数据库的新列。

为了使事情更清楚,我添加了一些代码来描述我要搜索的内容:

# pull "someColumn" and "identifier" from "databaseTable"
dplyr::tbl(odbConnection, dbplyr::in_schema("schemaName", "databaseTable")) %>% 
  select(identifier, someColumn) %>% 
  dplyr::collect() -> data2beUpdated

# update "someColumn" accoring to a "lookupTable" in R-session also having the "identifier"
data2beUpdated$newColumn <- lookupTable[match(data2beUpdated$identifier, lookupTable$identifier), "newValues"]

apply(data2beUpdated,
      1,
      FUN = function(x){ ifelse(
        is.na(x["newValues"]),
        x["someColumn"],
        x["newValues"] 
      )#ifelse
        }#fun
      ) -> data2beUpdated$someColumn

# now write back the data in the way I DON'T want to do it since it costs runtime to pull in the entire data table
dplyr::tbl(odbConnection, dbplyr::in_schema("schemaName", "databaseTable")) %>%  
  dplyr::collect() -> df

df$someColumn <- data2beUpdated$someColumn

DBI::dbWriteTable(odbcConnection, "databaseTable", df, overwrite = TRUE)

有什么建议吗?!

0 个答案:

没有答案