Machine Learning Services Configuring R Services in SQL Server

Machine Learning Services Configuring R Services in SQL Server

Machine Learning Services - Configuring R Services in SQL Server

SQLShack

SQL Server training Español

Machine Learning Services – Configuring R Services in SQL Server

October 25, 2018 by Ben Richardson The R language is one of the most popular languages for data science, machine learning services and computational statistics. There are several IDEs that allow seamless R development. Owing to the growing popularity of the R language, R services have been included by Microsoft in SQL Server 2016 onwards. In this article, we will briefly review how we can integrate R with SQL Server 2017. We will see the installation process and will also execute the basic R commands in SQL Server 2017.

Environment Setup

To run R scripts in SQL Server, you have to install Machine Learning Services in SQL Server, which can be done in two different ways. You can install machine learning services to an existing installation of SQL Server or you can configure to install these services with a fresh installation of SQL Server. In this article we will see the second approach where we will download a new version of SQL Server 2017 with machine learning services enabled. To do so, follow these steps: Go to the SQL Server 2017 download link, and select the Developer version of SQL Server for downloading as shown below: Once the download is complete, open the “downloaded” executable file. You should see the following options: Machine Learning Services is an optional feature which is not installed by default in SQL Server Management Studio. To install these services manually, click the Custom installation from the three options you see in the above screenshot. A new window will appear where you have to specify the installation path. Specify the installation path and click “Install” button. The download will take some time before the installation window appears. From the window that appears select “Installation” option from the left. You will see several options on the right. Select the first one which reads “New SQL Server stand-alone installation or add features to an existing installation”. This is shown in the following figure: Select free “Developer” edition from the window that appears and click “Next” button. Accept the License Agreement and click “Next” button again. Walk through each step until you reach the “Feature Selection” window. From the feature selection window select “Database Engine Services.” Under the “Database Engine Service” option, you should see “Machine Learning Services (In Database)” option, which further contains R and Python options. Select both R and Python options as shown below: Click “Next” button. Give name to your SQL Server instance in the window that appears. You can also use default name and then click “Next” button. Walk through each step until you reach the “Database Engine Configuration” option as shown below: Here you can click “Add Current User” button to add yourself as the database administrator. Click “Next” button. A window will appear prompting you to give consent to install “Microsoft R Open” as shown below: Click “Accept” button and then click “Next” button. Repeat Step 9 to give consent for installing Python Services. “Feature Configuration Rules” window will appear. Click “Next” button. Finally in the “Ready to Install” window, click “Install” button as shown below: Depending upon your processor speed and the internet, the installation process can take some time. Once the installation is complete, you should see the following window: If you see the above window, the installation is successful.

Enabling Machine Learning Services

In the previous section, we installed the machine learning services required to run R scripts in SQL Server. However, the services are not enabled by default. To enable the machine learning services, go to SQL Server Management Studio. If you have not already installed SQL Server Management Studio, you can download it from this link. In the SQL Server Management Studio, open a new query window and type the following script: 12 EXEC sp_configure 'external scripts enabled', 1 RECONFIGURE WITH OVERRIDE The script above enables execution of any external scripts in SQL Server. If the above script executes successfully, you should see the following message. Configuration option ‘external scripts enabled’ changed from 0 to 1. Run the RECONFIGURE statement to install. Before the R scripts can be executed, we need to restart the SQL Server. To do so, open the SQL Server Configuration Manager from the windows start menu. From the options on the left, select “SQL Server Services”. You will see list of all the SQL Server Instances, running on your system as shown below: Right Click the SQL Server Instance that you installed along with machine learning services and click “Restart”.

Executing R Scripts

We have installed and enabled the services that are required to run R scripts in SQL Server. Now is the time to run our R script in SQL Server. Execute the following script: 1234 EXEC sp_execute_external_script @language =N'R', @script=N'print("Welcome to R in SQL Server")'GO In the first line, we call the “sp_execute_external_script” store procedure; as a parameter we pass it the “language” that the script belongs to and the actual “script”. Notice we passed N‘R’ as language. In the script we simply print a message on the screen. In the console window, you should see the following output when the above script is executed: STDOUT message(s) from external script:
[1] “Welcome to R in SQL Server” If the corresponding services are installed, the process for running any external script remains the same.

Executing Python Scripts

During the installation of machine learning services, we also selected Python. Let’s modify our script to see how Python can be executed inside SQL Server. Execute the following script: 1234 EXEC sp_execute_external_script @language =N'Python', @script=N'print("Welcome to Python in SQL Server")'GO You can see, the only thing we changed here is the language and the text inside the string (which is optional). The output looks like this: STDOUT message(s) from external script:
Welcome to Python in SQL Server

Conclusion

In this article, we saw how we can configure SQL Server in order to run R scripts along with the changes we need to make during installation for enabling machine learning services that are required to run R in SQL Server. Finally we ran a simple R script to print the text on screen. By running a Python script, we also proved that the process of running external scripts in SQL server is more or less the same for both of these languages.

Other great articles from Ben

Importing and Working with CSV Files in SQL Server Machine Learning Services – Configuring R Services in SQL Server
Author Recent Posts Ben RichardsonBen Richardson runs Acuity Training a leading provider of SQL training the UK. It offers a full range of SQL training from introductory courses through to advanced administration and data warehouse training – see here for more details. Acuity has offices in London and Guildford, Surrey. He also blogs occasionally on Acuity’s blog

View all posts by Ben Richardson Latest posts by Ben Richardson (see all) Working with the SQL MIN function in SQL Server - May 12, 2022 SQL percentage calculation examples in SQL Server - January 19, 2022 Working with Power BI report themes - February 25, 2021

Related posts

How to integrate SQL Server and Azure Machine Learning How to use JSON data in Azure Machine Learning How to develop a K-Means model on Azure Machine Learning Studio Python in SQL Server: The Basics SQL Server Management Studio tutorial – Configuring the environment 6,401 Views

Follow us

Popular

SQL Convert Date functions and formats SQL Variables: Basics and usage SQL PARTITION BY Clause overview Different ways to SQL delete duplicate rows from a SQL Table How to UPDATE from a SELECT statement in SQL Server SQL Server functions for converting a String to a Date SELECT INTO TEMP TABLE statement in SQL Server SQL WHILE loop with simple examples How to backup and restore MySQL databases using the mysqldump command CASE statement in SQL Overview of SQL RANK functions Understanding the SQL MERGE statement INSERT INTO SELECT statement overview and examples SQL multiple joins for beginners with examples Understanding the SQL Decimal data type DELETE CASCADE and UPDATE CASCADE in SQL Server foreign key SQL Not Equal Operator introduction and examples SQL CROSS JOIN with examples The Table Variable in SQL Server SQL Server table hints – WITH (NOLOCK) best practices

Trending

SQL Server Transaction Log Backup, Truncate and Shrink Operations Six different methods to copy tables between databases in SQL Server How to implement error handling in SQL Server Working with the SQL Server command line (sqlcmd) Methods to avoid the SQL divide by zero error Query optimization techniques in SQL Server: tips and tricks How to create and configure a linked server in SQL Server Management Studio SQL replace: How to replace ASCII special characters in SQL Server How to identify slow running queries in SQL Server SQL varchar data type deep dive How to implement array-like functionality in SQL Server All about locking in SQL Server SQL Server stored procedures for beginners Database table partitioning in SQL Server How to drop temp tables in SQL Server How to determine free space and file size for SQL Server databases Using PowerShell to split a string into an array KILL SPID command in SQL Server How to install SQL Server Express edition SQL Union overview, usage and examples

Solutions

Read a SQL Server transaction logSQL Server database auditing techniquesHow to recover SQL Server data from accidental UPDATE and DELETE operationsHow to quickly search for SQL database data and objectsSynchronize SQL Server databases in different remote sourcesRecover SQL data from a dropped table without backupsHow to restore specific table(s) from a SQL Server database backupRecover deleted SQL data from transaction logsHow to recover SQL Server data from accidental updates without backupsAutomatically compare and synchronize SQL Server dataOpen LDF file and view LDF file contentQuickly convert SQL code to language-specific client codeHow to recover a single table from a SQL Server database backupRecover data lost due to a TRUNCATE operation without backupsHow to recover SQL Server data from accidental DELETE, TRUNCATE and DROP operationsReverting your SQL Server database back to a specific point in timeHow to create SSIS package documentationMigrate a SQL Server database to a newer version of SQL ServerHow to restore a SQL Server database backup to an older version of SQL Server

Categories and tips

►Auditing and compliance (50) Auditing (40) Data classification (1) Data masking (9) Azure (295) Azure Data Studio (46) Backup and restore (108) ►Business Intelligence (482) Analysis Services (SSAS) (47) Biml (10) Data Mining (14) Data Quality Services (4) Data Tools (SSDT) (13) Data Warehouse (16) Excel (20) General (39) Integration Services (SSIS) (125) Master Data Services (6) OLAP cube (15) PowerBI (95) Reporting Services (SSRS) (67) Data science (21) ►Database design (233) Clustering (16) Common Table Expressions (CTE) (11) Concurrency (1) Constraints (8) Data types (11) FILESTREAM (22) General database design (104) Partitioning (13) Relationships and dependencies (12) Temporal tables (12) Views (16) ►Database development (418) Comparison (4) Continuous delivery (CD) (5) Continuous integration (CI) (11) Development (146) Functions (106) Hyper-V (1) Search (10) Source Control (15) SQL unit testing (23) Stored procedures (34) String Concatenation (2) Synonyms (1) Team Explorer (2) Testing (35) Visual Studio (14) DBAtools (35) DevOps (23) DevSecOps (2) Documentation (22) ETL (76) ►Features (213) Adaptive query processing (11) Bulk insert (16) Database mail (10) DBCC (7) Experimentation Assistant (DEA) (3) High Availability (36) Query store (10) Replication (40) Transaction log (59) Transparent Data Encryption (TDE) (21) Importing, exporting (51) Installation, setup and configuration (121) Jobs (42) ▼Languages and coding (686) Cursors (9) DDL (9) DML (6) JSON (17) PowerShell (77) Python (37) R (16) SQL commands (196) SQLCMD (7) String functions (21) T-SQL (275) XML (15) Lists (12) Machine learning (37) Maintenance (99) Migration (50) Miscellaneous (1) ►Performance tuning (869) Alerting (8) Always On Availability Groups (82) Buffer Pool Extension (BPE) (9) Columnstore index (9) Deadlocks (16) Execution plans (125) In-Memory OLTP (22) Indexes (79) Latches (5) Locking (10) Monitoring (100) Performance (196) Performance counters (28) Performance Testing (9) Query analysis (121) Reports (20) SSAS monitoring (3) SSIS monitoring (10) SSRS monitoring (4) Wait types (11) ►Professional development (68) Professional development (27) Project management (9) SQL interview questions (32) Recovery (33) Security (84) Server management (24) SQL Azure (271) SQL Server Management Studio (SSMS) (90) SQL Server on Linux (21) ►SQL Server versions (177) SQL Server 2012 (6) SQL Server 2016 (63) SQL Server 2017 (49) SQL Server 2019 (57) SQL Server 2022 (2) ►Technologies (334) AWS (45) AWS RDS (56) Azure Cosmos DB (28) Containers (12) Docker (9) Graph database (13) Kerberos (2) Kubernetes (1) Linux (44) LocalDB (2) MySQL (49) Oracle (10) PolyBase (10) PostgreSQL (36) SharePoint (4) Ubuntu (13) Uncategorized (4) Utilities (21) Helpers and best practices BI performance counters SQL code smells rules SQL Server wait types © 2022 Quest Software Inc. ALL RIGHTS RESERVED. GDPR Terms of Use Privacy
Share:
0 comments

Comments (0)

Leave a Comment

Minimum 10 characters required

* All fields are required. Comments are moderated before appearing.

No comments yet. Be the first to comment!