连接到 MS SQL Server 时的 RODBC 临时表问题

发布于 2024-10-12 20:38:29 字数 812 浏览 5 评论 0原文

我在 unix 上运行 R,并使用 RODBC 包连接到 MS SQL 服务器。我可以执行一个查询,该查询返回的结果与包很好,但如果我在 SQL 查询中的某个位置使用临时表,则会返回一个空字符串。浏览完网络后,我认为问题可能在于 RODBC 包是假设最终用户使用标准 SQL(而不是 MS SQL)编写的。我提供了以下代码作为示例。

有趣的是,如果我使用RJDBC包,就不存在临时表问题。然而,RJDBC 包即使导入 80,000 行(10 列)也非常缓慢,并且会经常停止,因此这也不是一个选择。还有其他人遇到过这个问题吗?如果有我没有想到的替代解决方案,我很想听听。

看来我不是唯一一个有这个问题的人,也许这是一个R-Bug? http://r.789695.n4.nabble .com/RODBC-results-from-stored-procedure-td897462.html

谢谢

这是 R 示例:

library(RODBC)
ch <- odbcConnect(insert your server info here)
qry4 <- "create table #tempTable(
    Test int
)
insert into #tempTable
select 2

select * from #tempTable
drop table #tempTable
"
df4 <- sqlQuery(ch, qry4)

I am running R on unix and I am using the RODBC package to connect to MS SQL server. I can execute a query that returns results fine with the package, but if I use a temporary table somewhere in my SQL query, an empty string is returned to me. After looking over the web, I think the problem might be that the RODBC package was written assuming an end-user was writing in standard SQL (as opposed to MS SQL). I have provided the below code as an example.

Interestingly enough, the temporary table problem does not exist if I use the RJDBC package. However, the RJDBC package is painfully slow with importing even 80,000 rows (10 columns) and will stall out frequently, so that is not an option either. Has anyone else run into this problem? If there are alternate solutions that I haven't thought of, I'd love to hear them.

It seems I am not the only one with this problem, perhaps this is an R-Bug?
http://r.789695.n4.nabble.com/RODBC-results-from-stored-procedure-td897462.html

Thanks

Here is the R example:

library(RODBC)
ch <- odbcConnect(insert your server info here)
qry4 <- "create table #tempTable(
    Test int
)
insert into #tempTable
select 2

select * from #tempTable
drop table #tempTable
"
df4 <- sqlQuery(ch, qry4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨与橙与城 2024-10-19 20:38:29

RODBC 驱动程序似乎认为,当 SQL Server 返回任意行数时,整个语句就完成了。因此,您需要在调用的语句或存储过程的开头设置 nocount on。

set nocount on

这使我能够使用在 R 中使用临时表的存储过程。

The RODBC driver seems to think that when SQL Server returns any count of rows that the entire statement is complete. So you need to set nocount on at the beginning of your statement or stored procedure that is called.

set nocount on

This allowed me to use a stored procedure that was using temporary table in R.

只怪假的太真实 2024-10-19 20:38:29

问题似乎出在您的 SQL 语法中,而不是 R 或 RODBC 包固有的任何内容。我相当确定您需要使用 go 命令分隔 SQL 语句,以确保第一个语句在第二个、第三个等之前完成执行。或者,您可以将它们分成四个不同的语句,如下所示。这在我的机器上有效:

library(RODBC)
ch <- odbcConnect("details")

qry1 <- "create table #temptable (test int)"
qry2 <- "insert into #temptable(test) values(2)"
qry3 <- "select * from #temptable"
qry4 <- "drop table #temptable"

sqlQuery(ch, qry1)
sqlQuery(ch, qry2)
doesItWork <- sqlQuery(ch, qry3)
sqlQuery(ch, qry4)

输出

> doesItWork
  test
1    2

编辑

将所有查询转换为列表对象并迭代它们可以在将来节省一些编码。例如:

queryList <- list(qry1, qry2, qry3, qry4)
sqlOutput <- lapply(queryList, function(x) sqlQuery(ch, x))

这将生成一些您可能不关心的无关输出,但可以使用 sqlOutput[[3]] 提取您感兴趣的结果,其中 3 表示感兴趣的查询。

The problem appears to be in your SQL syntax, not anything inherent with R or the RODBC package. I'm fairly certain you need to separate your SQL statements with the go command to make sure that the first statement finished executing before the second, and the third, and so on. Alternatively, you could break them up into four different statements as I did below. This works on my machine:

library(RODBC)
ch <- odbcConnect("details")

qry1 <- "create table #temptable (test int)"
qry2 <- "insert into #temptable(test) values(2)"
qry3 <- "select * from #temptable"
qry4 <- "drop table #temptable"

sqlQuery(ch, qry1)
sqlQuery(ch, qry2)
doesItWork <- sqlQuery(ch, qry3)
sqlQuery(ch, qry4)

And the output

> doesItWork
  test
1    2

EDIT

Turning all of your queries into a list object and iterating through them could save you some coding in the future. For example:

queryList <- list(qry1, qry2, qry3, qry4)
sqlOutput <- lapply(queryList, function(x) sqlQuery(ch, x))

This will generate some extraneous output that you may not care about, but the results you are interested in can be pulled with sqlOutput[[3]] where 3 represents the query of interest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文