在R中SQL查询中传递多个参数

huangapple go评论64阅读模式
英文:

Pass multiple parameters in R SQL query

问题

我正在尝试在R SQL查询中多次传递相同的参数。查询如下:

table_x <- c(‘Rain’, ‘Cloudy’)

df <- dbGetQuery(conn, "select schemaname, tablename, dateupdated, test_result from weather 
Where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
and dateupdated = sysdate
and conditions in (?,?)",
params= table_x)

如果我删除第二个conditions子句,查询就可以工作。否则会报错:

> 查询需要4个参数;提供了2个

我尝试创建另一个向量,并将其与第一个向量一起传递,但仍然收到相同的错误。有什么建议吗?

谢谢

英文:

I am trying to pass same parameters multiple times in R SQL query. The query is:

table_x <- c(‘Rain’, ‘Cloudy’)

df <- dbGetQuery(conn, "select schemaname, tablename, dateupdated, test_result from weather 
Where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
and dateupdated = sysdate
and conditions in (?,?)",
params= table_x)

The query works if I remove the 2nd conditions clause. Otherwise it complains

> Query requires 4 parameters; 2 supplied

I tried creating another vector and pass that through along with 1st vector but still getting same error. Any suggestions.

Thanks

答案1

得分: 1

对于查询中找到的每个 ?,您必须在 params= 中有一个参数。顺序很重要,所以 params=c(table_x, table_x) 应该有效。

df <- dbGetQuery(conn, "
  select schemaname, tablename, dateupdated, test_result
  from weather 
  where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
    and dateupdated = sysdate
    and conditions in (?,?)",
  params = c(table_x, table_x))
英文:

For every ? found in the query, you must have a parameter in params=. Order is important, so params=c(table_x, table_x) should work.

df &lt;- dbGetQuery(conn, &quot;
  select schemaname, tablename, dateupdated, test_result
  from weather 
  where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
    and dateupdated = sysdate
    and conditions in (?,?)&quot;,
  params = c(table_x, table_x))

huangapple
  • 本文由 发表于 2023年7月28日 00:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定