Using R with RODBCext and RODBC to execute a SQL stored procedure

﹥>﹥吖頭↗ 提交于 2019-12-13 00:49:26

问题


I am using the RODBCext package and the RODBC package to execute a stored procedure in SQL server 2008 using R. If I use this code the stored procedure works.

query <- "EXEC [dbo].[usp_SchoolMerge] @Number = ?, 
                                       @Name = ?, 
                                       @Type = ?, 
                                       @Comments = ?, 
                                       @DualEnrollment =?, 
                                       @CEP = ?, 
                                       @DistrictGuidId = ?,
                                       @ImportName = ?,
                                       @ImportID = ?"

query <- gsub("[[:space:]]+", " ", query)

con2 <- odbcConnect("database", uid="userid", pwd="password")

for(i in 1:nrow(us11_12_00_school)) {

  sqlExecute(con2, query, us11_12_00_school[i,])

}

odbcClose(con2)

If I try to use the vectorized form explained here under 2.3.2 Using parameterized queries.

query <- "EXEC [dbo].[usp_SchoolMerge] @Number = ?, 
                                       @Name = ?, 
                                       @Type = ?, 
                                       @Comments = ?, 
                                       @DualEnrollment =?, 
                                       @CEP = ?, 
                                       @DistrictGuidId = ?,
                                       @ImportName = ?,
                                       @ImportID = ?"

query <- gsub("[[:space:]]+", " ", query)

con2 <- odbcConnect("database", uid="userid", pwd="password")

sqlExecute(con2, query, us11_12_00_school)

odbcClose(con2)

I get this error in R.

Error in sqlExecute(con2, query, us11_12_00_school) : 
  24000 0 [Microsoft][ODBC SQL Server Driver]Invalid cursor state
[RODBCext] Error: SQLExecute failed

If I use a data frame with only one row the vectorized code works. Anyone else had this problem? Any ideas?


回答1:


Seems like you want to use only the first column of the us11_12_00_school - which you pass in the loop-version of the function with us11_12_00_school[i,]. In the vectorised version though, you pass in the whole dataframe!

I havent tested it out, but i guess passing the dataframe as us11_12_00_school[1,] in the vectorised version would give you the expected results?




回答2:


I use the code below, and seems to work well. Hopefully it helps you too.

require(RODBC)
require(RODBCext)

ExecuteQuery <- function(query, arguments){

  dbhandle <- odbcDriverConnect('driver={SQL Server Native Client 11.0};server=SereverName;database=DbName;trusted_connection=yes')
  if(missing(arguments))
    result <- sqlExecute(dbhandle, query = query, fetch = TRUE)  
  else
    result <- sqlExecute(dbhandle, query =query, data=arguments, TRUE)  
  close(dbhandle)
  return(result)
}

CallProcWithSomeParams <- function(param1, param2){

  ExecuteQuery('exec dbo.procName ?, ?', data.frame(param1, param2))
}


来源:https://stackoverflow.com/questions/30898712/using-r-with-rodbcext-and-rodbc-to-execute-a-sql-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!