Pass R variable to RODBC's sqlQuery with multiple entries? -
i'm in process of learning r, wave sas goodbye, i'm still new , somehow have difficulties finding i'm looking for.
but specific case, read: pass r variable rodbc's sqlquery? , made work myself, long i'm inserting 1 variable in destination table.
here code:
library(rodbc) channel <- odbcconnect("test") b <- sqlquery(channel, "select top 1 noinscr table prixvente > 100 order datevente desc") sqlquery(channel, paste("insert testtable (uniqueid) values (",b,")", sep = "")
when replace top 1
other number, let's top 2
, , run exact same code, following errors:
[1] "42000 195 [microsoft][sql server native client 10.0][sql server] 'c' not recognized built-in function name." [2] "[rodbc] error: not sqlexecdirect 'insert testtable (uniqueid) values (c(8535735, 8449336))'"
i understand because there c generated, assume column when give command: paste(b)
.
so how can "8535735, 8449336"
instead of "c(8535735, 8449336)"
when using paste(b)
? or there way this?
look collapse
argument in paste()
documentation. try replacing b
paste(b, collapse = ", ")
, shown below.
edit joshua points out, sqlquery returns data.frame, not vector. so, instead of paste(b, collapse = ", ")
, use paste(b[[1]], collapse = ", ")
.
library(rodbc) channel <- odbcconnect("test") b <- sqlquery(channel, "select top 1 noinscr table prixvente > 100 order datevente desc") sqlquery(channel, ## note paste(b[[1]], collapse = ", ") in line below paste("insert testtable (uniqueid) values (", paste(b[[1]], collapse = ", "),")", sep = "")
Comments
Post a Comment