2

I'm trying to convert a scaleR script into T-SQL (with external_script execution) for in-database R analytics in SQL Server. The scaleR script that I'm working with is on the DeepDive Data Science Tutorial on Fraud data by MSDN.

All my data is in SQL Server now (from the tutorial), and all I want to do is query this table to get summary using the rxSummary scaleR function (in-database).
This is my attempt:

exec sp_execute_external_script 
@language = N'R', 
@script = N' 
sumOut <- rxSummary( 
formula = ~gender + balance + numTrans + numIntlTrans + creditLine, 
data = ccFraud 
) 
', 
@input_data_1 = N'select * from [DeepDive].[db_datareader].[ccFraudSmall]', 
@input_data_1_name = N'ccFraud', 
@output_data_1_name = N'summary' 
with result sets ((summary varchar(max) not null));

But this throws an error:

STDOUT message(s) from external script: 
Rows Read: 10000, Total Rows Processed: 10000, Total Chunk Time: Less than .001 seconds 
Computation time: 0.000 seconds. 
Msg 11536, Level 16, State 1, Line 5 
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.

Any idea how to deal with this?

Am I missing a step?

1 Answer 1

1

In your example, there is a mismatch between the R script and SQL parameter definitions. The output_data_1_name parameter value specifies the name of the R dataframe that is being returned from R to SQL Server. But there is no value assigned to summary in the R script. Hence you are getting a 0 results error. See sp_execute_external_script document for description and example.

If you are just trying to display the summary as messages, then you can add print(sumOut) to the R script.

If you want to return data to SQL Server, you would need to build an R dataframe and assign to the output_1_data.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that worked to display the summary results. But when I want to save Summary to SQL Server, I tried adding summary <- data.frame(sumOut) to the R Script, it throws an error that rxSummary cannot be coerced to a data frame.
Are you trying to store the rxSummary object as a binary in SQL Server? Try using summary <- serialize(sumOut, NULL);
This is my code now: @script = N' sumOut <- rxSummary( formula = ~gender + balance + numTrans + numIntlTrans + creditLine, data = ccFraud ) print(sumOut) summary <- serialize(sumOut, NULL); ', @input_data_1 = N'select * from [DeepDive].[db_datareader].[ccFraud10]', @input_data_1_name = N'ccFraud', @output_data_1_name = N'summary' with result sets ((summary varbinary(max) not null)); Shows this error: The output dataset must be of type data frame; consider using data.frame() to convert it.
Also still shows this error: 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.
Refer to the Train and Save a Model using T-SQL tutorial for an example of how to store a serialized R object to SQL Server table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.