I recently worked through a strange error (with help from a couple sharp cookies at Microsoft) and wanted to throw together a quick blog post in case anybody else sees it.
I have SQL Server R Services set up, and in the process of running a fairly complex stored procedure, got the following error message:
Msg 39004, Level 16, State 22, Line 0
A ‘R’ script error occurred during execution of ‘sp_execute_external_script’ with HRESULT 0x80004005.
There were two Stack Exchange posts, but not much else. In one of them, Loretta Parks says to look at permissions. I knew that wasn’t the issue because I could run this procedure sometimes, but not all the time, and when running the R code itself, everything worked as expected. The other basically became “Well, I’ll do it outside of SQL Server R Services”
In my case, the issue was around arithmetic overflow. Here’s a quick repro:
DECLARE @TestAccuracy DECIMAL(5, 2); EXEC sp_execute_external_script @language = N'R', @script = N'TestAccuracy <- 15000.07', @params = N'@TestAccuracy DECIMAL(5,2) OUTPUT', @TestAccuracy = @TestAccuracy OUTPUT;
This script is quite simple: it assigns a value of 15000.07 to a variable named TestAccuracy, and then tries to output the TestAccuracy variable in R to a SQL Server variable defined as DECIMAL(5,2). The largest value a DECIMAL(5,2) can hold is 999.99, so this translation fails.
I’ve reported this to the right group and I’m hopeful that they will be able to provide a more meaningful error message in a future release. But in the meantime, if you get error 0x80004005, this is a potential cause.