我想从 R 中的函数调用存储过程。请参阅下面的代码。不幸的是,这段代码只生成一个没有值的数据框。我想用RJDBC
&解决这个DBI
问题,因为RODBC
.
RPT_09_Hourly_Connected_v3<- function(Year, Month="NULL",State = "NULL",Region="NULL", City="NULL", District="NULL", Subdistrict="NULL" ,Address='NULL'){
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "/opt/sqljdbc_3.0/sqljdbc4.jar")
conn <- DBI::dbConnect(drv, "jdbc:sqlserver://***;databaseName=***;user=***;password=***")
sqlText <- paste("exec [dbo].[RPT_09_Hourly_Connected_v3]@Year=",Year,
",@Month=",Month,
",@State=",State,"",
",@Region=",Region,"",
",@City=N'",City,"'",
",@District=",District,"",
",@Subdistrict=",Subdistrict,"",
",@Address=N'",Address,"'",
sep="")
data <- RJDBC::dbGetQuery(conn,sqlText)
}
a<- RPT_09_Hourly_Connected_v3(Year = 2016)
> str(a)
'data.frame': 0 obs. of 9 variables:
$ Regio : chr
$ Stad : chr
$ Stadsdeel : chr
$ Buurtcombinatie: chr
$ Adres : chr
$ Jaar : num
$ Maand : num
$ hourNR : num
$ HoursConnected : num
这在RODBC
崩溃之前对我有用。RODBC
和有什么区别RJDBC
吗?
RPT_09_Hourly_Connected_v3<- function(Year, Month="NULL",State = "NULL",Region="NULL", City="NULL", District="NULL", Subdistrict="NULL" ,Address='NULL'){
dbhandle <- odbcConnect("***;DATABASE=***;UID=***;PWD=***")
data <- sqlQuery(dbhandle,paste("exec [ dbo].[RPT_09_Hourly_Connected_v3]@Year=",Year,
",@Month=",Month,
",@State=",State,"",
",@Region=",Region,"",
",@City=N'",City,"'",
",@District=",District,"",
",@Subdistrict=",Subdistrict,"",
",@Address=N'",Address,"'",
sep=""))
odbcCloseAll()
data
}
如果我在 SQL Server 中手动执行存储过程,它将如下所示:
EXEC @return_value = [dbo].[RPT_09_Hourly_Connected_v3]
@Year = 2016,
@Month = NULL,
@State = NULL,
@Region = NULL,
@City = N'Amsterdam',
@District = NULL,
@Subdistrict = NULL,
@Address = NULL
你能解释什么是错的以及如何解决它吗?