我在 Windows 7 下运行以下版本的 R。
R version 3.0.1 (2013-05-16) -- "Good Sport"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
使用库(RODBC),
channel <- odbcConnect(dsn = "database")
ProcExec <- "execute LENGTHCOMP(2004)"
# the procedure returns nothing, but creates a series of Oracle tables needed later in the R function
sqlQuery(channel = channel, query = ProcExec, believeNRows = FALSE)
odbcClose(channel)
返回错误:
[1] "42000 0 [Oracle][ODBC]Syntax error or access violation."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'execute LENGTHCOMP(2004)'" .
ROracle 库(2013 年 8 月)指出
dbCallProc-方法 调用 SQL 存储过程 描述 尚未实现。
他们的其他 R 包是否提供了调用 Oracle 上存储的 PL/SQL 过程的功能?或者有没有办法让 sqlQuery 从 RODBC 包中执行此操作?
最佳答案
使用 ROracle,我成功地创建了一个 PLSQL 函数来包装过程调用:
create or replace function Wrapper_func (Sql_in in varchar2)
return number
authid current_user
as
pragma autonomous_transaction;
begin
execute immediate Sql_in;
return 0;
exception
when others then
return sqlcode;
end Wrapper_func;
这是我从 R 调用的:
sqldata<-dbGetQuery(con, "select wrapper_func ('begin proc_t(137); end;') from dual")
如果过程调用成功,该函数返回 0;如果过程调用失败,则返回错误代码。该过程需要以 current_user 身份运行,因为它将执行为其提供的任何 sql。
正确的方法可能是使用与过程调用的签名相匹配的特定函数调用来包装所有过程调用。这些函数不需要是 current_user,因为它们只传递参数。
关于r - 如何从 R 调用存储在 Oracle 数据库上的 PL/SQL 过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393906/