将 R 变量传递给 RODBC 的 sqlQuery? [英] Pass R variable to RODBC's sqlQuery?

查看:55
本文介绍了将 R 变量传递给 RODBC 的 sqlQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 R 中定义的变量传递给 RODBC 包中的 sqlQuery 函数?

Is there any way to pass a variable defined within R to the sqlQuery function within the RODBC package?

具体来说,我需要将这样的变量传递给标量/表值函数、存储过程和/或 SELECT 语句的 WHERE 子句.

Specifically, I need to pass such a variable to either a scalar/table-valued function, a stored procedure, and/or perhaps the WHERE clause of a SELECT statement.

例如,让:

x <- 1 ## user-defined

那么,

example <- sqlQuery(myDB,"SELECT * FROM dbo.my_table_fn (x)")

或者...

example2 <- sqlQuery(myDB,"SELECT * FROM dbo.some_random_table AS foo WHERE foo.ID = x")

或者...

example3 <- sqlQuery(myDB,"EXEC dbo.my_stored_proc (x)")

显然,这些都不起作用,但我认为有一些东西可以实现这种功能.

Obviously, none of these work, but I'm thinking that there's something that enables this sort of functionality.

推荐答案

构建您要传递的字符串.所以代替

Build the string you intend to pass. So instead of

example <- sqlQuery(myDB,"SELECT * FROM dbo.my_table_fn (x)")

example <- sqlQuery(myDB, paste("SELECT * FROM dbo.my_table_fn (", 
                                x, ")", sep=""))

会填入x的值.

这篇关于将 R 变量传递给 RODBC 的 sqlQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆