Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql RODBC:执行包含多个语句的查询_Sql_R_Rodbc - Fatal编程技术网

Sql RODBC:执行包含多个语句的查询

Sql RODBC:执行包含多个语句的查询,sql,r,rodbc,Sql,R,Rodbc,我有一个返回varbinary输出参数的存储过程。我想从R调用它并捕获返回变量。我试过: qq <- "declare @mm varbinary(max); exec spTrain_df_to_op @mm_outer = @mm output; select @mm as model;" conStr <- "Driver={SQL Server};Server=.;Database=airline_new;Trusted_Connection=TRUE;" dbhandle

我有一个返回varbinary输出参数的存储过程。我想从R调用它并捕获返回变量。我试过:

qq <- "declare @mm varbinary(max); exec spTrain_df_to_op @mm_outer = @mm output; select @mm as model;"
conStr <- "Driver={SQL Server};Server=.;Database=airline_new;Trusted_Connection=TRUE;"
dbhandle <- odbcDriverConnect(conStr, rows_at_time = 1)
result <- sqlQuery(dbhandle, qq)
我还尝试将查询设置为

qq = paste0("SET NOCOUNT ON; declare @mm varbinary(max); ",
           "exec spTrain_df_to_op @mm_outer = @mm output; ",
           "SET NOCOUNT OFF; select @mm as model;")
仍然生成字符(0)


从查询中创建存储过程不是一个选项。

诚然,我没有像在您的用例中那样在PL/SQL中尝试过这一点,但我认为它应该可以工作,如果不是的话,希望它对其他人运行存储在单个脚本中的多个查询有用。

您应该能够通过首先将脚本拆分为单独的查询来实现这一点。如果使用分号作为分隔符(即设置了多少个查询,以及第二次尝试是如何构造的),则可以将查询拆分为查询向量,并通过循环分别运行每个查询。如果某些查询有您希望在以后访问的结果,您可以返回它们并在执行时将其存储在列表中

library(RODBC)

# an example SQL Script containing multiple queries, seperated by semi-colon
example_script <- 
"select sysdate from dual;
commit;
select sysdate from dual;"

# split the string into a character vector of queries to run
split_queries <- strsplit(example_script, ";",)[[1]]

#prepeare a list to store results
list_of_results <- list() 


ch <- odbcConnect("XX",uid="XX", pwd="XX")

# loop through and run the queries, storing results (if any) in the list
for (i in 1:length(split_queries)) {

  list_of_results[[i]] <- sqlQuery(ch,
                                   split_queries[i],
                                   errors = FALSE)

  Sys.sleep(2)

}

odbcClose(ch)

# show list of results, of course these elements could be anything from strings to data.frames
list_of_results

[[1]]
              SYSDATE
1 2018-03-28 17:15:26

[[2]]
[1] -2

[[3]]
              SYSDATE
1 2018-03-28 17:15:30
库(RODBC)
#包含多个查询的示例SQL脚本,用分号分隔

示例\u脚本诚然,我没有像在您的用例中那样在PL/SQL中尝试过这一点,但我认为它应该可以工作,如果不希望它对其他人运行存储在单个脚本中的多个查询有用的话。

您应该能够通过首先将脚本拆分为单独的查询来实现这一点。如果使用分号作为分隔符(即设置了多少个查询,以及第二次尝试是如何构造的),则可以将查询拆分为查询向量,并通过循环分别运行每个查询。如果某些查询有您希望在以后访问的结果,您可以返回它们并在执行时将其存储在列表中

library(RODBC)

# an example SQL Script containing multiple queries, seperated by semi-colon
example_script <- 
"select sysdate from dual;
commit;
select sysdate from dual;"

# split the string into a character vector of queries to run
split_queries <- strsplit(example_script, ";",)[[1]]

#prepeare a list to store results
list_of_results <- list() 


ch <- odbcConnect("XX",uid="XX", pwd="XX")

# loop through and run the queries, storing results (if any) in the list
for (i in 1:length(split_queries)) {

  list_of_results[[i]] <- sqlQuery(ch,
                                   split_queries[i],
                                   errors = FALSE)

  Sys.sleep(2)

}

odbcClose(ch)

# show list of results, of course these elements could be anything from strings to data.frames
list_of_results

[[1]]
              SYSDATE
1 2018-03-28 17:15:26

[[2]]
[1] -2

[[3]]
              SYSDATE
1 2018-03-28 17:15:30
库(RODBC)
#包含多个查询的示例SQL脚本,用分号分隔

qq的东西是一个打字错误,对不起。您是否建议在存储过程的末尾添加一个select语句,以选择所有输出参数?问题是存储过程还返回一个不是输出参数的结果集。有时我需要结果集,有时需要输出参数。它只返回字符(0)作为结果SQL Server中相应的行输出了什么?相同的查询通过SSMS正常工作,并在列模型下返回一个varbinary作为表中的单个条目。qq是一个输入错误。您是否建议在存储过程的末尾添加一个select语句,以选择所有输出参数?问题是存储过程还返回一个不是输出参数的结果集。有时我需要结果集,有时需要输出参数。它只返回字符(0)作为结果SQL Server中的相应行输出了什么?同一查询通过SSMS正常工作,并在列模型下的表中作为单个条目返回varbinary。
qq = paste0("SET NOCOUNT ON; declare @mm varbinary(max); ",
           "exec spTrain_df_to_op @mm_outer = @mm output; ",
           "SET NOCOUNT OFF; select @mm as model;")
library(RODBC)

# an example SQL Script containing multiple queries, seperated by semi-colon
example_script <- 
"select sysdate from dual;
commit;
select sysdate from dual;"

# split the string into a character vector of queries to run
split_queries <- strsplit(example_script, ";",)[[1]]

#prepeare a list to store results
list_of_results <- list() 


ch <- odbcConnect("XX",uid="XX", pwd="XX")

# loop through and run the queries, storing results (if any) in the list
for (i in 1:length(split_queries)) {

  list_of_results[[i]] <- sqlQuery(ch,
                                   split_queries[i],
                                   errors = FALSE)

  Sys.sleep(2)

}

odbcClose(ch)

# show list of results, of course these elements could be anything from strings to data.frames
list_of_results

[[1]]
              SYSDATE
1 2018-03-28 17:15:26

[[2]]
[1] -2

[[3]]
              SYSDATE
1 2018-03-28 17:15:30