Skip to content

Latest commit

 

History

History
123 lines (84 loc) · 4.89 KB

sql-machine-learning-azure-data-studio.md

File metadata and controls

123 lines (84 loc) · 4.89 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic monikerRange
Azure Data Studio notebooks (Python, R)
Learn how to run Python and R scripts in a notebook in Azure Data Studio with SQL Server Machine Learning Services.
VanMSFT
vanto
03/09/2020
sql
machine-learning-services
how-to
>=sql-server-2017||>=sql-server-linux-ver15

Run Python and R scripts in Azure Data Studio notebooks with SQL Server Machine Learning Services

[!INCLUDE SQL Server 2017 and later]

Learn how to run Python and R scripts in Azure Data Studio notebooks with SQL Server Machine Learning Services. Azure Data Studio is a cross-platform database tool.

Prerequisites

Create a SQL notebook

Important

Machine Learning Services runs as part of SQL Server. Therefore, you need to use a SQL kernel and not a Python kernel.

You can use Machine Learning Services in Azure Data Studio with a SQL notebook. To create a new notebook, follow these steps:

  1. Click File and New Notebook to create a new notebook. The notebook will by default use the SQL kernel.

  2. Click Attach To and Change Connection.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebook change connection

  3. Connect to an existing or new SQL Server. You can either:

    1. Choose an existing connection under Recent Connections or Saved Connections.

    2. Create a new connection under Connection Details. Fill out the connection details to your SQL Server and database.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebook connection details

Run Python or R scripts

SQL Notebooks consist of code and text cells. Code cells are used to run Python or R scripts via the stored procedure sp_execute_external_scripts. Text cells can be used to document your code in the notebook.

Run a Python script

Follow these steps to run a Python script:

  1. Click + Code to add a code cell.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebooks add code block

  2. Enter the following script in the code cell:

    EXECUTE sp_execute_external_script @language = N'Python'
        , @script = N'
    a = 1
    b = 2
    c = a/b
    d = a*b
    print(c, d)
    '
  3. Click Run cell (the round black arrow) or press F5 to run the single cell.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebooks run Python code

  4. The result will be shown under the code cell.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebook Python code output

Run an R script

Follow these steps to run an R script:

  1. Click + Code to add a code cell.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebooks add code block

  2. Enter the following script in the code cell:

    EXECUTE sp_execute_external_script @language = N'R'
        , @script = N'
    a <- 1
    b <- 2
    c <- a/b
    d <- a*b
    print(c(c, d))
    '
  3. Click Run cell (the round black arrow) or press F5 to run the single cell.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebooks run R code

  4. The result will be shown under the code cell.

    [!div class="mx-imgBorder"] Azure Data Studio SQL Notebook R code output

Next steps