【问题标题】:Error when Executing SQL Stored procedure with R In RTVS在 RTVS 中使用 R 执行 SQL 存储过程时出错
【发布时间】:2017-12-17 09:24:09
【问题描述】:

我在 RTVS Visual Studio 2017 中使用 R 创建了一个存储过程,并将其发布到我的数据库后,当我尝试执行它时出现此错误:

消息 39004,第 16 级,状态 20,第 4 行
执行“sp_execute_external_script”期间发生“R”脚本错误,HRESULT 为 0x80004004。

消息 39019,第 16 层,状态 1,第 4 行
发生外部脚本错误:库中的错误(RODBC):没有名为“RODBC”的包调用:源 -> withVisible -> eval -> eval -> library

ScaleR 中的错​​误。检查输出以获取更多信息。错误 eval(expr,envir,enclos):ScaleR 中的错​​误。检查输出 更多信息。调用:source -> withVisible -> eval -> eval -> .调用执行停止

Msg 11536, Level 16, State 1, Procedure SqlSProc, Line 4 [Batch Start Line 2] > EXECUTE 语句失败,因为它的 WITH RESULT SETS 子句指定了 1 个结果集,但该语句只发送了 0 个结果集( s) 在运行时。

这是我的 SqlSProc.R 代码:

library(RODBC)
channel <- odbcDriverConnect(settings$dbConnection1)
InputDataSet <- sqlQuery(channel, iconv(paste(readLines('c:/users/abdal/source/repos/correlation/correlation/sqlsproc.query.sql', encoding = 'UTF-8', warn = FALSE), collapse = '\n'), from = 'UTF-8', to = 'ASCII', sub = ''))
odbcClose(channel)
InputDataSet$OtherLangDescription <- as.factor(InputDataSet$OtherLangDescription)
orderList <- unique(InputDataSet$OtherLangDescription)
ListId <- lapply(orderList, function(x) subset(InputDataSet, OtherLangDescription == x)$WHWorkOrderHeaderId)
Initial_Tab <- lapply(ListId, function(x) subset(InputDataSet, WHWorkOrderHeaderId %in% x)$OtherLangDescription)
Correlation_Tab <- mapply(function(Product, ID) table(Product) / length(ID),
                        Initial_Tab, ListId)
colnames(Correlation_Tab) <- orderList
cor_per <- round(Correlation_Tab * 100, 2)
OutputDataSet <- data.frame(row = rownames(cor_per)[row(cor_per)], col = colnames(cor_per)[col(cor_per)], corr = c(cor_per))
OutputDataSet <- InputDataSet

为 R 存储过程检索数据的 SQL 查询:

SELECT
    WHWorkOrderHeaderId, OtherLangDescription
FROM   
    Warehouse.WHWorkOrderDetails 
INNER JOIN 
    Warehouse.WHWorkOrderHeader AS WHH ON Warehouse.WHWorkOrderDetails.WHWorkOrderHeaderId = WHH.ID 
INNER JOIN 
    Warehouse.StockItems ON Warehouse.WHWorkOrderDetails.StockItemId = Warehouse.StockItems.Id 
ORDER BY  OtherLangDescription ASC

SQL PROC 模板:

CREATE PROCEDURE [SqlSProc]
AS
BEGIN
EXEC sp_execute_external_script @language = N'R'
    , @script = N'_RCODE_'
    , @input_data_1 = N'_INPUT_QUERY_'
--- Edit this line to handle the output data frame.
   WITH RESULT SETS (([product_1] Nvarchar(MAX),[Product_2] Nvarchar(Max),[Correlation] INT));
END ; 

【问题讨论】:

    标签: sql-server r stored-procedures rtvs


    【解决方案1】:

    您的错误信息

    库中的错误(RODBC):没有名为“RODBC”的包

    指向一个缺失的包,必须在使用前安装:

    install.packages("RODBC")
    library(RODBC)
    [Rest of your R-code]
    

    以下错误 - 很可能 - 绑定到丢失的包。有预期的结果集,但 SP 没有返回结果...

    【讨论】:

    • Msg 39004, Level 16, State 20, Line 2 A 'R' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004. Msg 39019, Level 16, State 1, Line 2 An external script error occurred: Warning in install.packages("RODBC") : 'lib = "C:/Program Files/Microsoft SQL Server/MSSQL13.MSSQLSERVER/R_SERVICES/library"' is not writable Error in install.packages("RODBC") : unable to install packages Calls: source -&gt; withVisible -&gt; eval -&gt; eval -&gt; install.packages
    • @Believer The Error is not writable Error in install.packages("RODBC") 听起来像是一个权利/安全问题。必须允许您的帐户写入给定位置...
    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多