Pass R variable to RODBC's sqlQuery with multiple entries?

后端 未结 2 1217
萌比男神i
萌比男神i 2020-12-06 22:38

I\'m in the process of learning R, to wave SAS goodbye, I\'m still new to this and I somehow have difficulties finding exactly what I\'m looking for.

But for this sp

2条回答
  •  臣服心动
    2020-12-06 23:30

    Look into the collapse argument in the paste() documentation. Try replacing b with paste(b, collapse = ", "), as shown below.

    Edit As Joshua points out, sqlQuery returns a data.frame, not a vector. So, instead of paste(b, collapse = ", "), you could use paste(b[[1]], collapse = ", ").

    library(RODBC)
    channel <- odbcConnect("test")
    b <- sqlQuery(channel,
      "select top 1 Noinscr
       FROM table
       where PrixVente > 100
       order by datevente desc")
    
    sqlQuery(channel,
       ## note paste(b[[1]], collapse = ", ") in line below
       paste("insert into TestTable (UniqueID) Values (", paste(b[[1]], collapse = ", "),")", sep = "")
    

提交回复
热议问题