简体   繁体   English

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

[英]RODBC Temporary Table Issue when connecting to MS SQL Server

I am running R on unix and I am using the RODBC package to connect to MS SQL server. 我在unix上运行R,我使用RODBC包连接到MS SQL服务器。 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. 我可以执行一个使用包返回结果的查询,但如果我在SQL查询的某处使用临时表,则返回一个空字符串给我。 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). 在浏览网页后,我认为问题可能是RODBC软件包是在最终用户使用标准SQL(而不是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. 有趣的是,如果我使用RJDBC包,则不存在临时表问题。 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. 但是,RJDBC软件包输入甚至80,000行(10列)的速度非常慢,并且会经常停顿,所以这也不是一个选择。 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? 看来我不是唯一有这个问题的人,也许这是一个R-Bug? http://r.789695.n4.nabble.com/RODBC-results-from-stored-procedure-td897462.html http://r.789695.n4.nabble.com/RODBC-results-from-stored-procedure-td897462.html

Thanks 谢谢

Here is the R example: 这是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)

The RODBC driver seems to think that when SQL Server returns any count of rows that the entire statement is complete. RODBC驱动程序似乎认为当SQL Server返回整个语句完成的任何行数时。 So you need to set nocount on at the beginning of your statement or stored procedure that is called. 因此,您需要在语句的开头或调用的存储过程中设置nocount。

set nocount on

This allowed me to use a stored procedure that was using temporary table in R. 这允许我使用在R中使用临时表的存储过程。

The problem appears to be in your SQL syntax, not anything inherent with R or the RODBC package. 问题似乎出现在您的SQL语法中,而不是R或RODBC包中固有的任何问题。 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. 我很确定你需要用go命令分离你的SQL语句,以确保第一个语句在第二个语句和第三个语句之前完成执行,依此类推。 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. 这将生成一些您可能不关心的无关输出,但您感兴趣的结果可以使用sqlOutput[[3]]来提取,其中3表示感兴趣的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM