Skip to content

Latest commit

 

History

History
113 lines (74 loc) · 3.67 KB

File metadata and controls

113 lines (74 loc) · 3.67 KB
title Notebooks with SQL Kernel in Azure Data Studio
description This tutorial shows how you can create and run a SQL Server notebook.
author markingmyname
ms.author maghan
ms.reviewer mikeray, alayu
ms.topic tutorial
ms.prod azure-data-studio
ms.technology azure-data-studio
ms.custom
ms.date 07/01/2020

Create and run a SQL Server notebook

[!INCLUDESQL Server 2019]

This tutorial demonstrates how to create and run a notebook in Azure Data Studio using SQL Server.

Prerequisites

Create a notebook

The following steps show how to create a notebook file in Azure Data Studio:

  1. In Azure Data Studio, connect to your SQL Server.

  2. Select under the Connections in the Servers window. Then select New Notebook.

    Open notebook

  3. Wait for the Kernel and the target context (Attach to) to be populated. Confirm that the Kernel is set to SQL, and set Attach to for your SQL Server (in this example it's localhost).

    Set Kernel and Attach to

You can save the notebook using the Save or Save as... command from the File menu.

To open a notebook, you can use the Open file... command in the File menu, select Open file on the Welcome page, or use the File: Open command from the command palette.

Change the SQL connection

To change the SQL connection for a notebook:

  1. Select the Attach to menu from the notebook toolbar and then select Change Connection.

    Click the Attach to menu in the notebook toolbar

  2. Now you can either select a recent connection server or enter new connection details to connect.

    Select a server from the Attach to menu

Run a code cell

You can create cells containing SQL code that you can run in place by clicking the Run cell button (the round black arrow) to the left of the cell. The results are shown in the notebook after the cell finishes running.

For example:

  1. Add a new code cell by selecting the +Code command in the toolbar.

    Notebook toolbar

  2. Copy and paste the following example into the cell and click Run cell. This example creates a new database.

    USE master
    GO
    
    -- Drop the database if it already exists
    IF  EXISTS (
            SELECT name
            FROM sys.databases
            WHERE name = N'TestNotebookDB'
       )
    DROP DATABASE TestNotebookDB
    GO
    
    -- Create the database
    CREATE DATABASE TestNotebookDB
    GO

    Run notebook cell

Save the result

If you run a script that returns a result, you can save that result in different formats using the toolbar displayed above the result.

  • Save As CSV
  • Save As Excel
  • Save As JSON
  • Save As XML

For example, the following code returns the result of PI.

SELECT PI() AS PI;
GO

Run notebook cell

Next steps

Learn more about notebooks: