【发布时间】: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 -> libraryScaleR 中的错误。检查输出以获取更多信息。错误 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