Error when Executing SQL Stored procedure with R In RTVS

0

I have created a stored procedure with R in RTVS Visual Studio 2017 and after publishing it to my database, I get this error when I try to execute it:

Msg 39004, Level 16, State 20, Line 4
A 'R' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.

Msg 39019, Level 16, State 1, Line 4
An external script error occurred: Error in library(RODBC) : there is no package called 'RODBC' Calls: source -> withVisible -> eval -> eval -> library

Error in ScaleR. Check the output for more information. Error in eval(expr, envir, enclos) : Error in ScaleR. Check the output for more information. Calls: source -> withVisible -> eval -> eval -> .Call Execution halted

Msg 11536, Level 16, State 1, Procedure SqlSProc, Line 4 [Batch Start Line 2] > EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), but the statement only sent 0 result set(s) at run time.

This is my SqlSProc.R code:

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

SQL query retrieving data for the R stored procedure:

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 Template:

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
asked on Stack Overflow Dec 17, 2017 by Believer • edited Dec 17, 2017 by marc_s

1 Answer

2

Your error message

Error in library(RODBC) : there is no package called 'RODBC'

points to a missing package, which must be installed before you use it:

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

The following errors are - quite probably - bound to the missing package. There is a resultset expected, but the SP returned without a result...

answered on Stack Overflow Dec 17, 2017 by Shnugo

User contributions licensed under CC BY-SA 3.0