How to verify R exists in your SQL instance
Run the code below, sometime you might need to enable external scripting and SQL Server Launchpad service to start.
EXECUTE sp_execute_external_script
@language =N'R',
@script=N'print(version)';
GO
You can see that SQL Server in this case have R version 3.3.3
How to get the list of R packages that are already installed in your SQL Server instance
EXECUTE sp_execute_external_script
@language = N'R'
, @script = N'
OutputDataSet <- data.frame(installed.packages()[,c("Package", "Version", "Depends", "License","LibPath")]);'
WITH result sets((Package NVARCHAR(255), Version NVARCHAR(100), Depends NVARCHAR(4000), License NVARCHAR(1000), LibPath NVARCHAR(2000)));
What are the parameters available can be used to execute sp_execute_external_script
sp_execute_external_script
@language = N'language',
@script = N'script'
[ , @input_data_1 = N'input_data_1' ]
[ , @input_data_1_name = N'input_data_1_name' ]
[ , @output_data_1_name = N'output_data_1_name' ]
[ , @parallel = 0 | 1 ]
[ , @params = N'@parameter_name data_type [ OUT | OUTPUT ] [ ,...n ]' ]
[ , @parameter1 = 'value1' [ OUT | OUTPUT ] [ ,...n ] ]
WITH RESULT SET ()
WITH RESULT SET ()
Let’s looks a simple example:
EXECUTE sp_execute_external_script
@language = N'R'
, @script = N'OutputDataSet <- InputDataSet;'
, @input_data_1 = N'SELECT ''Uma'',''Bale'',38;'
WITH RESULT SETS (([FirstName] NVARCHAR(5),[LastName] NVARCHAR(5),[Age] INT));
@language = N'language'
Indicates the script language. The valid values are R (SQL Server 2016 and later), Python (SQL Server 2017 and later), and Java (SQL Server 2019 preview)
@script = N'script'
External language script specified as a literal or variable input. Script is nvarchar(max). Default input and output variables of sp_execute_external_script: InputDataSet and OutputDataSet
External language script specified as a literal or variable input. Script is nvarchar(max). Default input and output variables of sp_execute_external_script: InputDataSet and OutputDataSet
@input_data_1
specifies the input data used by the external script in the form of a Transact-SQL query. The data type of input_data_1 is nvarchar(max).
specifies the input data used by the external script in the form of a Transact-SQL query. The data type of input_data_1 is nvarchar(max).
@input_data_1_name
Specifies the name of the variable used to represent the query defined by @input_data_1. The data type of the variable in the external script depends on the language. In case of R, the input variable is a data frame. In the case of Python, input must be tabular. input_data_1_name is sysname. Default value is InputDataSet.
Specifies the name of the variable used to represent the query defined by @input_data_1. The data type of the variable in the external script depends on the language. In case of R, the input variable is a data frame. In the case of Python, input must be tabular. input_data_1_name is sysname. Default value is InputDataSet.
@output_data_1_name
Specifies the name of the variable in the external script that contains the data to be returned to SQL Server upon completion of the stored procedure call. The data type of the variable in the external script depends on the language. For R, the output must be a data frame. For Python, the output must be a pandas data frame. output_data_1_name is sysname. Default value is OutputDataSet.
Specifies the name of the variable in the external script that contains the data to be returned to SQL Server upon completion of the stored procedure call. The data type of the variable in the external script depends on the language. For R, the output must be a data frame. For Python, the output must be a pandas data frame. output_data_1_name is sysname. Default value is OutputDataSet.
@parallel
Enable parallel execution of R scripts by setting the @parallel parameter to 1. The default for this parameter is 0 (no parallelism).
Enable parallel execution of R scripts by setting the @parallel parameter to 1. The default for this parameter is 0 (no parallelism).
WITH RESULT SET
By default, result sets returned by this stored procedure are output with unnamed columns. Column names used within a script are local to the scripting environment and are not reflected in the outputted result set. To name result set columns, use the WITH RESULT SET clause of EXECUTE.
By default, result sets returned by this stored procedure are output with unnamed columns. Column names used within a script are local to the scripting environment and are not reflected in the outputted result set. To name result set columns, use the WITH RESULT SET clause of EXECUTE.
EXECUTE sp_execute_external_script
@language = N'R'
, @script = N' SQL_out <- SQL_in * 5;'
, @input_data_1 = N'SELECT 2 as Col;'
, @input_data_1_name = N'SQL_in'
, @output_data_1_name = N'SQL_out'
WITH RESULT SETS (([Output] INT NOT NULL));
Cheers!
Uma
Uma
Please let me know if you’re looking for an author for your site. You have some great posts, and I think I would be a good asset. If you ever want to take some of the load off, I’d like to write some material for your blog in exchange for a link back to mine. Please shoot me an email if interested. Thanks.
ReplyDeleteapple service center chennai | ipod service center in chennai | Apple laptop service center in chennai | apple iphone service center in chennai | apple iphone service center in chennai
What would we have done without your help? Thank you so much for your blog.
ReplyDeleteapple service center chennai | Mac service center in chennai | ipod service center in chennai | apple iphone service center in chennai
A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
ReplyDeleteapple iphone service center in chennai | apple ipad service center in chennai | iWatch service center chennai | iphone repair in chennai | Mobile service center in chennai
Good information.I would like your article.Keep share more articles and pass information.
ReplyDeleteAuthorized ipad service center in Chennai | Authorized apple service center in Chennai | iphone display replacement | Authorized ipad service center in Chennai | Authorized ipod service center in Chennai | Apple laptop service center in chennai | 100% genuine mobile parts | Mobile phone Battery replacement in chennai
more appreciating blog! Great internet site! It looks extremely good! Maintain a good job!| you are rocking man…!
ReplyDeletetrends on youtube today | Politics news in tamilnadu | politics news chennai | latest news in sunnews
more appreciating blog! Great internet site! It looks extremely good! Maintain a good job!| you are rocking man…!
ReplyDeletetrends on youtube today | Politics news in tamilnadu | politics news chennai | latest news in sunnews | tamil news paper | Politics news in tamilnadu | latest tamil news
“Useful post”
ReplyDeletepolitics news chennai | latest news in tamil | politics news chennai | latest news in sunnews | politics speech tamil | NetCab | sivaavishnusvs