Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Task 03 - Enable advanced capabilities for modern workloads

Introduction

With Tailspin’s SQL workloads successfully migrated and secured in Azure SQL Managed Instance, the next step is to unlock advanced capabilities that go beyond traditional relational queries. In this task, you will configure the Managed Instance to run in Always‑up‑to‑date mode and upgrade the database to the latest compatibility level, ensuring access to the newest SQL features while preserving backward compatibility for existing applications.

This foundation enables modern features such as vector support, designed for AI‑driven scenarios like semantic search, recommendation systems, and similarity detection. By loading vector data and running similarity queries, Tailspin can modernize its analytics and prepare its systems for next‑generation workloads that integrate machine learning and intelligent search directly into the database.

Description

In this task, you will:

  1. Verify the SQL Managed Instance engine update policy is set to Always-up-to-date, ensuring the engine continuously evolves with the latest SQL capablities.
  2. Upgrade the ToyStore database to the latest compatibility level to align query processing with modern workloads.
  3. Create vector columns in the database schema to store embeddings or feature vectors.
  4. Use Azure SQL MI together with Azure OpenAI to generate embeddings and perform vector similarity queries, validating AI‑driven operations such as nearest‑neighbor search and semantic matching.

Success Criteria

  • SQL Managed Instance update policy is set to always‑up‑to‑date, and the ToyStore database is upgraded to the latest compatibility level.
  • Vector columns are successfully created in the database schema.
  • Embedding data is loaded and stored in the database without errors.
  • Similarity queries execute correctly, returning expected results that demonstrate modern AI‑driven capabilities.

Solution

Expand this section to view the solution

Tailspin’s ToyStore database is now running in Azure, and you are ready to take advantage of the latest SQL Server capabilities. First, configure the SQL Managed Instance update policy to Always‑up‑to‑date. Next, upgrade the ToyStore database to the latest compatibility level. Finally, integrate with Azure OpenAI to generate embeddings and execute semantic similarity queries, demonstrating how Tailspin can extend its workloads with intelligent search and recommendation features.

  1. In the Azure portal, open your lab resource group and select the tailspin<uniqueid>-sqlmi SQL Managed Instance resource.

    The SQL managed instance resource in highlighted in the resource group.

  2. On the SQL Managed Instance blade, choose Maintenance and updates under Settings in the left menu. In the SQL engine updates section, confirm that Always-up-to-date is selected. If another values is shown, switch to Always-up-to-date and select Save.

    The maintenance and updates blade of SQL MI is displayed with the Always-up-to-date update policy selected and highlighted.

    NOTE: The Always‑up‑to‑date policy ensures your Managed Instance automatically receives the latest SQL engine improvements for performance, security, and compliance. This eliminates manual patching and downtime planning. In this lab, the setting was enabled during deployment so you can immediately focus on modern workload features.

  3. To upgrade the ToyStore database compatilibity level, go to the Overview blade of the SQL MI blade, and copy the Host value from the Essentials panel.

    In the Essentials panel of the SQL MI overview blade, the Host property is highlighted.

  4. Return to the SQL VM Bastion session, and on the SQL VM. From the Windows start menu, launch Microsoft SQL Server Management Studio (SSMS), located under the Microsoft SQL Server Tools 20 folder.

    On the SQL VM, Microsoft SQL Server Management Studio 20 in highlighted within the Microsoft SQL Server Tools 20 folder in the Windows Start menu.

  5. In the Connect to Server dialog, enter:

    • Server type: Database Engine
    • Server name: Paste the host name of your SQL Managed Instance
    • Authentication method: Sql Server Authentication
    • Login: demouser
    • Password = demo!pass1234567
    • Enable Remember password and Trust server certificate
    • Select Connect

    The SSMS Connect to Server dialog is displayed with the Trust server certificate checkbox and Connect button highlighted.

  6. Once connected, expand Databases, right-click the ToyStore database, and select New Query in the context menu.

    New Query is highlighted in the right-click context menu for the ToyStore database.

  7. Run the following query to check the current compatibility level:

     SELECT name, compatibility_level  
     FROM sys.databases  
     WHERE [name] = 'ToyStore';
     GO
    

    The query to retrieve the compatibility level of the ToyStore database is highlighted in the query window, the Execute button is highlighted on the toolbar, and the output of the query, showing a compatability level of 100, is highlighted.

  8. Update to the latest compatibity level (160 = SQL Server 2022):

     ALTER DATABASE ToyStore
     SET COMPATIBILITY_LEVEL = 160;
     GO
    

    The alter database query is highlighted in the query window, with the Execute button and commands completed successfully output message highlighted

    NOTE: Compatibility level 160 unlocks SQL Server 2022 query processing features such as intelligent query processing and enhanced cardinality estimation. This ensures modern workloads benefit from the latest optimizations while maintaining backward compatibility.

  9. With the database upgraded, you can now prepare it for modern workload features such as vector‑based queries.

  10. Add a vector column to store embeddings in the [Production.ProductDescription] table:

    USE ToyStore;
    GO
    ALTER TABLE [Production].[ProductDescription]
    ADD [embedding] VECTOR(1536) NULL;
    
  11. Before you can generate embeddings from the database, you must retrieve the endpoint and key of the Azure OpenAI service that you deployed with the ARM template you ran in the setup for this lab. Navigate to your lab resource group in the Azure portal, and select the tailspin<uniqueid>-oai Azure OpenAI resource from the resources list.

    The Azure OpenAI resource is highlighted in the resource list of the lab resource group.

  12. On the Azure OpenAI service blade, select Keys and Endpoint under Resource Management in the left menu, then copy the Endpoint and KEY 1 values for use below.

    The Keys and Endpoint menu item is selected and highlighted, and the copy to clipboard buttons for KEY 1 and Endpoint are highlighted.

  13. Create database credentials to store API key. Replace <your-openai-endpoint> with the name of your Azure OpenAI service and <your-openai-api-key> with the API key for the Azure OpenAI API.

    IF NOT EXISTS(SELECT * FROM sys.symmetric_keys WHERE [name] = '##MS_DatabaseMasterKey##')
    BEGIN
        CREATE MASTER KEY ENCRYPTION BY PASSWORD = N'demo!pass1234567';
    END
    GO
    IF EXISTS(SELECT * FROM sys.[database_scoped_credentials] WHERE NAME = '<your-openai-endpoint>')
    BEGIN
        DROP DATABASE SCOPED CREDENTIAL [<your-openai-endpoint>];
    END
    CREATE DATABASE SCOPED CREDENTIAL [<your-openai-endpoint>]
    WITH IDENTITY = 'HTTPEndpointHeaders', SECRET = '{"api-key": "<your-openai-api-key>"}';
    GO
    
  14. Create a stored procedure to generate embeddings by calling the Azure OpenAI API. Replace <your-openai-endpoint> with your Azure OpenAI resource. Note that there are two (2) <your-openai-endpoint> tokens you must replace.

    CREATE OR ALTER PROCEDURE dbo.get_embedding
        @deployedModelName nvarchar(1000),
        @inputText nvarchar(max),
        @embedding vector(1536) output
    AS
    DECLARE @retval int, @response nvarchar(max);
    DECLARE @payload nvarchar(max) = json_object('input': @inputText);
    DECLARE @oaiEndpoint nvarchar(255) = '<your-openai-endpoint>'
    DECLARE @url nvarchar(1000) =  @oaiEndpoint + 'openai/deployments/' + @deployedModelName + '/embeddings?api-version=2023-05-15'
        
    EXEC @retval = sp_invoke_external_rest_endpoint
        @url = @url,
        @method = 'POST',
        @credential = [<your-openai-endpoint>],
        @payload = @payload,
        @response = @response output;
    
    DECLARE @re vector(1536);
    IF (@retval = 0)
        BEGIN
            SET @re = cast(json_query(@response, '$.result.data[0].embedding') AS vector(1536))
        END
    ELSE
        BEGIN
            DECLARE @msg nvarchar(max) =  
                'Error calling OpenAI API' + char(13) + char(10) + 
                '[HTTP Status: ' + json_value(@response, '$.response.status.http.code') + '] ' +
                json_value(@response, '$.result.error.message');
            THROW 50000, @msg, 1;
        END
    
    SET @embedding = @re;
    
    RETURN @retval
    GO
    

    NOTE: This stored procedure sends text to the Azure OpenAI embeddings API, retrieves the resulting vector, and casts it into the SQL VECTOR data type. It handles both success and error cases, returning the embedding to the caller or raising an exception if the API call fails.

  15. Run the following query to reconfigure the ToyStore database to enable external rest endpoint calls.

    EXEC sp_configure 'external rest endpoint enabled', 1;
    RECONFIGURE;
    
  16. Before you can perform semantic similarity queries against product descriptions stored in the database, you must create embedding representations of the Description field and store those in the new embedding filed. To accomplish this, run the following query to update each row’s embedding column in the [Production].[ProductDescription] table with generated embeddings for the Description column.

    DECLARE @id INT;
    DECLARE @description NVARCHAR(MAX);
    DECLARE @embedding VECTOR(1536);
    DECLARE @deployedModelName NVARCHAR(50) = 'text-embedding-ada-002';
    
    DECLARE cur CURSOR FOR
        SELECT ProductDescriptionId, Description
        FROM Production.ProductDescription
        WHERE embedding IS NULL;  -- only update missing embeddings
    
    OPEN cur;
    FETCH NEXT FROM cur INTO @id, @description;
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        -- Call your embedding procedure
        EXEC dbo.get_embedding
            @deployedModelName = @deployedModelName,
            @inputText = @description,
            @embedding = @embedding OUTPUT;
    
        -- Update the row with the generated embedding
        UPDATE Production.ProductDescription
        SET embedding = @embedding
        WHERE ProductDescriptionId = @id;
    
        FETCH NEXT FROM cur INTO @id, @description;
    END
    
    CLOSE cur;
    DEALLOCATE cur;
    

    NOTE: The ARM template deployed an Azure OpenAI resource with the text-embedding-ada-002 model. For simplicity, the model name is hard‑coded in the script. This script is a one‑time batch update for existing rows. In production, you would automate embedding generation for new inserts or updates (e.g., via triggers, ETL pipelines, or application logic).

  17. Run the following query to confirm that vectors are now stored in the embedding column:

    SELECT TOP 5 ProductDescriptionId, Description, embedding
    FROM Production.ProductDescription;
    

    The above query is highlighted in the query window and the results are highlighted in the results pane.

  18. Now that embeddings are stored, you can query for product descriptions that are semantically similar to a given phrase, such as “affordable high-performance bike”. The following script generates an embedding for the input text and compares it against stored vectors using cosine distance:

    DECLARE @inputText NVARCHAR(50) = 'affordable high-performance bike';
    DECLARE @embedding VECTOR(1536);
    DECLARE @retval int;
    DECLARE @deployedModelName NVARCHAR(50) = 'text-embedding-ada-002';
    
    EXEC @retval = dbo.get_embedding @deployedModelName, @inputText, @embedding OUTPUT;
    
    SELECT TOP(10)
        ProductDescriptionId,
        Description,
        vector_distance('cosine', @embedding, embedding) cosine_distance
    FROM Production.ProductDescription
    ORDER BY cosine_distance;
    

    The results pane is highlighted in SSMS, showing the descriptions most similar to the phrase "affordable high-performance bike."

    NOTE: This query demonstrates how SQL Server can perform semantic search directly inside the database, returning product descriptions most closely aligned with the meaning of the input phrase.